Configuration management with Ansible
Terraform made the servers; something must set them up. How Ansible configures many machines over SSH with idempotent tasks, handlers and a dry-run mode.
SSH keys, bastions and remote accessInfrastructure as code: declarative vs imperativeTerraform and OpenTofu: plan, apply and state
Configuration management with Ansible
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Provisioning creates machines and configuration management sets up what is on them: packages, files, users and services. Terraform and Ansible are often used together for the two halves.
- Ansible is agentless. It connects over SSH from a control node, runs small modules on each host listed in an inventory, and needs only Python on the target.
- A playbook is a list of plays, each mapping a group of hosts to an ordered list of tasks, and each task calls a module with the state you want.
- Modules are idempotent and report ok or changed, so running a playbook twice is safe and handlers fire only when something they depend on actually changed.
- --check --diff is Ansible's dry run. It shows what would change on each host without changing it.
Ten blank servers
Terraform has just created ten virtual machines. Each one is a fresh Linux install with nothing on it. Every one needs nginx, a site config, a few users with SSH keys, security updates and a running service. And when the config changes next month, all ten need the new version, identically.
The first attempt is usually a shell loop: for h in $(cat hosts); do ssh $h 'sudo apt-get install -y nginx && ...'; done. It works once. The second run reinstalls and restarts everything. Host 7 was unreachable, so it silently has the old config. Nobody knows which servers differ. This is the imperative problem from declarative vs imperative again, one layer up.
Provisioning versus configuration
It helps to split the work in two. Provisioning creates the machines, networks and load balancers, which is Terraform’s job. Configuration management decides what is on each machine: packages, files, users, services. Tools for the second half include Puppet, Chef, Salt and Ansible, the most widely used today. Plenty of teams run Terraform first and hand its list of new hosts to Ansible.
How Ansible works
Ansible runs from a control node, your laptop or a CI runner. It reads an inventory, a list of hosts in groups such as web and db. For each task, it connects to each host over SSH, copies a small Python program called a module across, runs it, and reads back a JSON result. There is no agent to install on the servers, only SSH access and Python, which most Linux distributions already have. Through a bastion, ProxyJump in your SSH config works for Ansible too.
What you write is a playbook, in YAML:
- name: Web servers run nginx in front of the notes API
hosts: web
become: true
vars:
notes_port: 8080
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
cache_valid_time: 3600
- name: Put the site config in place
ansible.builtin.template:
src: notes.conf.j2
dest: /etc/nginx/conf.d/notes.conf
mode: "0644"
notify: Reload nginx
- name: Make sure nginx is running and starts at boot
ansible.builtin.service:
name: nginx
state: started
enabled: true
handlers:
- name: Reload nginx
ansible.builtin.service:
name: nginx
state: reloaded
A playbook is a list of plays. Each play maps a host group to an ordered list of tasks, and each task calls one module. become: true runs tasks with sudo, so you can log in as an ordinary user. The template module renders a Jinja2 file, where {{ notes_port }} becomes 8080, and copies the result across. The config itself would be the nginx upstream block from earlier.
Ansible is not installed on this Mac, and I had no servers to point it at, so I did not run this playbook. I wrote it from the documentation for the apt, template and service modules and checked its structure by parsing it with Python:
play: Web servers run nginx in front of the notes API (hosts=web, become=True)
task: Install nginx module=ansible.builtin.apt notify=-
task: Put the site config in place module=ansible.builtin.template notify=Reload nginx
task: Make sure nginx is running and starts at boot module=ansible.builtin.service notify=-
handler: Reload nginx
The current ansible-core release on PyPI when I checked was 2.22.0.
Idempotence is the point
Look at how the tasks are phrased. Not “run apt-get install nginx” but state: present. Not “start nginx” but state: started. Each module checks the current state first and acts only if it differs, then reports one of two results per host: ok, meaning already as described, or changed, meaning it fixed something. On a second run against configured servers, every task should come back ok. A task that reports changed on every run is a bug in the playbook.
Handlers build on that. The template task notifies Reload nginx, and the handler runs once at the end of the play, only on hosts where the template actually changed. Ten servers, a config change on all of them, ten reloads. No config change, no reloads.
Before applying to production, run it as a dry run:
ansible-playbook -i inventory.ini site.yml --check --diff --limit web
--check asks every module to report what it would change without changing it, and --diff shows the file differences. It is the closest Ansible has to terraform plan. Some tasks, such as ones that depend on an earlier task’s real result, cannot predict perfectly in check mode, so read it as a strong preview, not a guarantee.
Where it fits now
Ansible is strongest on long-lived servers: databases, bastions, build agents and on-premises fleets. Its importance has shifted as more workloads run in containers, where the image is the configuration. A common pattern now is to use Ansible once, at image build time, to configure a machine image, and then never change running servers. That is the subject of the next lesson.
About the video
“Ansible Playbook Basics - FINALLY Explained Clearly!” is from Alta3 Research, published April 2025, 12 minutes 33 seconds. Its chapters cover why Ansible, what is in a playbook, a birdhouse analogy, tasks and modules and parameters, YAML lists, plays versus playbooks, a real playbook example, become, when conditions, facts, control nodes versus managed nodes, and how Ansible connects to different platforms. It does not cover handlers or check mode by name in its chapters, which this lesson adds. English was judged from the title, description, channel and its English caption tracks. Learn Linux TV’s video in deeper writes a first playbook step by step and runs it with ansible-pull, the pull-based alternative.
Where this fits
Configuring servers in place works, but every change applied to a live server is a chance for drift. Next, the alternative of never changing them at all: immutable infrastructure and golden images.
Check yourself
Why is a loop of ssh host 'apt-get install nginx && cp nginx.conf ...' worse than an Ansible playbook?
The script repeats actions every time and stops or half-applies on the first error with no clear report. Ansible modules check the current state first, change only what differs, and report per host what changed.
What is a handler, and why not just restart nginx as the last task?
A handler is a task that runs only when another task notifies it, and only once at the end of the play. Restarting unconditionally would bounce nginx on every run, even when nothing changed.
What does become: true do?
It runs the tasks with privilege escalation, sudo by default, so they can install packages and write to /etc while you connect as a normal user.
How does Ansible differ from Puppet or Chef in how it reaches servers?
Ansible pushes over SSH with no agent installed. Puppet and Chef traditionally run an agent on each server that pulls its configuration from a central server.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.