Configuration Management with Ansible
Configure fleets of servers over SSH with agentless, idempotent playbooks, roles and inventories.
Intermediate · about 10 hours · difficulty 3/5
Learn first: Infrastructure as Code Principles, SSH, Bastions and VPNs
Ansible is the most common configuration management tool because it needs no agent: it connects over SSH (or WinRM), pushes small modules, runs them and reports what changed.
Core pieces
- Inventory: which hosts exist and how they are grouped (
web,db,prod). Static files or dynamic inventory plugins that query your cloud. - Modules: idempotent units of work such as
apt,dnf,copy,template,user,systemd,lineinfile. - Playbooks: YAML lists of plays, each mapping hosts to tasks.
- Handlers: tasks that run only when notified, for example restart nginx only if its config changed.
- Variables and facts: host, group and role variables, plus facts gathered from each machine.
- Templates: Jinja2 files rendered per host.
- Roles and collections: reusable, shareable packages of tasks, templates and defaults.
Good habits
- Prefer purpose-built modules over
shell; if you must useshell, addcreatesorchanged_whenso reruns stay idempotent. - Run with
--check --diffbefore real changes. - Store secrets with Ansible Vault or a secret manager lookup.
- Lint with ansible-lint and test roles with Molecule.
Where it fits today
Ansible bootstraps VMs, patches fleets, configures network gear and runs ad-hoc operations. In container-first shops it is used less for apps and more for base images and legacy servers.
Good enough to move on: you can write a role that installs and configures nginx from a template, restarts it only on change, runs cleanly twice in a row, and targets hosts from a dynamic cloud inventory.
Key concepts
- Inventories and dynamic inventory
- Playbooks, plays and tasks
- Modules and idempotency
- Handlers
- Variables, facts and Jinja2 templates
- Roles and collections
- Ansible Vault
- ansible-lint and Molecule
Mini project
Write an Ansible role that hardens a fresh Linux VM (users, SSH config, firewall, automatic updates) and installs nginx from a template, then prove idempotency by running it twice and showing zero changes.
Check yourself
Why is using the shell module to run apt-get install a worse choice than the apt module, even if both install the package?
Free resources
- Ansible documentation (Red Hat)
- Ansible getting started (Red Hat)