# Configuration Management with Ansible

> Configure fleets of servers over SSH with agentless, idempotent playbooks, roles and inventories. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/devops/ansible
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/devops/iac-concepts), [SSH, Bastions and VPNs](https://www.internhack.in/roadmaps/tracks/devops/ssh-vpn)

**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 use `shell`, add `creates` or `changed_when` so reruns stay idempotent.
- Run with `--check --diff` before 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](https://docs.ansible.com/) (Red Hat)
- [Ansible getting started](https://docs.ansible.com/ansible/latest/getting_started/index.html) (Red Hat)

Previous: [CloudFormation, CDK and Bicep](https://www.internhack.in/roadmaps/tracks/devops/cloudformation-cdk) · Next: [Chef, Puppet and Salt](https://www.internhack.in/roadmaps/tracks/devops/chef-puppet)

## More in Infrastructure as Code

- [Infrastructure as Code Principles](https://www.internhack.in/roadmaps/tracks/devops/iac-concepts)
- [Terraform and OpenTofu](https://www.internhack.in/roadmaps/tracks/devops/terraform)
- [Pulumi](https://www.internhack.in/roadmaps/tracks/devops/pulumi)
- [CloudFormation, CDK and Bicep](https://www.internhack.in/roadmaps/tracks/devops/cloudformation-cdk)
- [Configuration Management with Ansible](https://www.internhack.in/roadmaps/tracks/devops/ansible)
- [Chef, Puppet and Salt](https://www.internhack.in/roadmaps/tracks/devops/chef-puppet)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
