DevOps with Ansible Will Finally Make Sense After This
Ansible makes servers match a description you write once. We ran it for real on a small shop's three web servers: an inventory, ad-hoc commands, a first playbook that installs nginx and publishes the page, a second run that changes nothing (idempotence), a hand edit found with --check --diff and put right, the shell task that changes something every time, a handler that reloads nginx only when needed, and one variable that updates every server.
Two servers, two answers
You set up your servers by hand, and now no two of them quite match. This is a ski rental shop's page, on its first web server: open at eight. The very same page, on the second server: open at nine.

Customers get whichever server answers. Both servers are completely sure of themselves. Ansible fixes this: you describe how every server should look, once, and it makes them match. And one harmless-looking line in a playbook quietly breaks that promise. We'll find it.
curl -s web1<title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> <p>Open every day, 8:00 to 17:00</p>
curl -s web2<title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> <p>Open every day, 9:00 to 17:00</p>
What Ansible does
Ana runs the shop's three web servers. Ansible runs on her own computer, here her laptop: the control node. It reads two files: an inventory, the list of servers, and a playbook, how they should look. Then it logs in to each server over SSH: nothing to install on them. For each step, it sends a small program called a module, which checks the server, fixes only what's different, and reports back: ok, or changed. That one word is how Ana will know what really happened.

Think of a thermostat. You don't tell it: heat for ten minutes. You set twenty-one degrees. If the room is already twenty-one, it does nothing. If it's cold, it heats until it isn't. A playbook works the same way. Its steps don't say: install nginx, the web server. They say: nginx is installed. That's the difference from a shell script, which runs every command again, every time.

The inventory
Ana's project is a folder on her laptop. The inventory is a short text file: a group called web, with the three servers' names. The first test: ansible web, dash m ping. It isn't a network ping: it logs in to every server in the group and runs a tiny module. Three answers: pong.
lsansible.cfg group_vars inventory.ini site.yml templates
cat inventory.ini[web] web1 web2 web3
ansible web -m pingweb3 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } web2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" } web1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }
Dash a runs any command on every server at once. Which nginx do they have? web1 and web2: version one point twenty-four. web3 has no nginx at all: it's brand new. Notice the other two say CHANGED, though all they did was print a version. With a plain command, Ansible can't tell what it did, so it assumes it changed something.
ansible web -a 'nginx -v'web3 | FAILED | rc=2 >> [Errno 2] No such file or directory: b'nginx' web2 | CHANGED | rc=0 >> nginx version: nginx/1.24.0 (Ubuntu) web1 | CHANGED | rc=0 >> nginx version: nginx/1.24.0 (Ubuntu)
The first playbook
The playbook is a YAML file: plain text with indents. hosts: web, the group. become: true runs the steps as root, with sudo. Then three tasks, each a sentence about how the server should be: nginx is installed, the shop's page is up to date, nginx is running. Under each name sits the module that checks it: apt, Ubuntu's package installer, template, which writes a file, and service, which keeps a program running.
cat site.yml- name: Web servers for the ski shop hosts: web become: true tasks: - name: nginx is installed apt: name: nginx state: present update_cache: true - name: the shop's page is up to date template: src: index.html.j2 dest: /var/www/html/index.html - name: nginx is running service: name: nginx state: started enabled: true
The page comes from a template, with two blanks: the shop's name, and its hours. A variables file fills them in: eight to five. Ansible finds it by its name: the folder group_vars, then a file named after the group, web. Change the hours there, and every server's page follows.
cat templates/index.html.j2<title>{{ shop }}</title> <h1>{{ shop }}</h1> <p>Open every day, {{ hours }}</p>
cat group_vars/web.ymlshop: Powder Peak Ski Rentals hours: "8:00 to 17:00"
ansible-playbook site.yml, with time in front, to measure how long it takes. Read it task by task. nginx is installed: ok on web1 and web2, changed on web3, where apt just installed it. The page: already right on web1, changed on web2, the one showing nine, and on web3. The recap counts it up: zero, one and two changes, in about 10 seconds.
time ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web3] ok: [web2] TASK [nginx is installed] ****************************************************** ok: [web2] ok: [web1] changed: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web1] changed: [web2] changed: [web3] TASK [nginx is running] ******************************************************** ok: [web1] ok: [web2] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 real 0m10.028s user 0m0.744s sys 0m0.215s
That run started with a task Ana didn't write: Gathering Facts. Ansible first asks each server about itself. The setup module, filtered to the system's name, shows a few of web3's facts: Ubuntu, version twenty-four oh four. A playbook can use them as variables.
ansible web3 -m setup -a 'filter=ansible_distribution*'web3 | SUCCESS => { "ansible_facts": { "ansible_distribution": "Ubuntu", "ansible_distribution_file_parsed": true, "ansible_distribution_file_path": "/etc/os-release", "ansible_distribution_file_variety": "Debian", "ansible_distribution_major_version": "24", "ansible_distribution_release": "noble", "ansible_distribution_version": "24.04", "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false }
Run it again
Ana runs the exact same playbook a second time. Every task says ok, on every server: zero changes, in 3.5 seconds. The thermostat was already at twenty-one, so nothing ran that didn't need to.
time ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web3] ok: [web1] ok: [web2] TASK [the shop's page is up to date] ******************************************* ok: [web1] ok: [web3] ok: [web2] TASK [nginx is running] ******************************************************** ok: [web2] ok: [web1] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 real 0m3.496s user 0m0.647s sys 0m0.194s
Ansible's own glossary has a word for this: idempotent. Running it once gives the same result as running it again and again. So running a playbook is safe, and it's how you check that nothing has drifted.

A change made by hand
A week later, Ben logs in to web2 and edits the page by hand: eight thirty. Dash dash check asks every module what it would do, without doing it, and dash dash diff shows the lines. Only web2's page would change: eight thirty out, eight in. And web2 still shows eight thirty: check mode touched nothing.
curl -s web2<title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> <p>Open every day, 8:30 to 17:00</p>
ansible-playbook site.yml --check --diffPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web2] ok: [web1] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web3] ok: [web2] TASK [the shop's page is up to date] ******************************************* ok: [web1] ok: [web3] --- before: /var/www/html/index.html +++ after: /home/ana/.ansible/tmp/ansible-local-487moh5y8hl/tmp8dkruf2_/index.html.j2 @@ -1,3 +1,3 @@ <title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> -<p>Open every day, 8:30 to 17:00</p> +<p>Open every day, 8:00 to 17:00</p> changed: [web2] TASK [nginx is running] ******************************************************** ok: [web2] ok: [web1] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
curl -s web2<title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> <p>Open every day, 8:30 to 17:00</p>
The real run fixes it: changed on web2 alone, and the page says eight again. The playbook is the record of how the servers should be, not whatever someone typed last.
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web3] ok: [web1] changed: [web2] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web1] ok: [web2] PLAY RECAP ********************************************************************* web1 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
curl -s web2<title>Powder Peak Ski Rentals</title> <h1>Powder Peak Ski Rentals</h1> <p>Open every day, 8:00 to 17:00</p>
A line that looks harmless
Now, that harmless-looking line. Ana adds a task to put her phone number on a staff notice, using the shell module: echo, with two arrows, to add a line to the file. The first run: changed, on every server. She runs the playbook once more. Guess first: changed, or ok? Changed, a second time. And the notice now has her number twice. Shell runs its command every time, and Ansible can't check a sentence it didn't write.
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the staff notice has Ana's phone] **************************************** changed: [web2] changed: [web3] changed: [web1] PLAY RECAP ********************************************************************* web1 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web3] ok: [web1] ok: [web2] TASK [nginx is running] ******************************************************** ok: [web2] ok: [web1] ok: [web3] TASK [the staff notice has Ana's phone] **************************************** changed: [web1] changed: [web3] changed: [web2] PLAY RECAP ********************************************************************* web1 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
curl -s web1/staff.txtAna's phone 555-0142 Ana's phone 555-0142
The fix is to describe the file, not the action. The copy module with content says: this file contains exactly this line. The next run is changed once, because the duplicate goes. After that: ok, every time.

The proof: the first run says changed, because it rewrites the file. The next says ok. And the notice has her number exactly once.
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web2] ok: [web1] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web3] ok: [web2] ok: [web1] TASK [nginx is running] ******************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the staff notice has Ana's phone] **************************************** changed: [web1] changed: [web2] changed: [web3] PLAY RECAP ********************************************************************* web1 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the shop's page is up to date] ******************************************* ok: [web2] ok: [web3] ok: [web1] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the staff notice has Ana's phone] **************************************** ok: [web1] ok: [web2] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
curl -s web1/staff.txtAna's phone 555-0142
Restart only when needed
Some changes need a restart. Ana adds a task that writes one nginx setting: name the server that answered. nginx only reads it on a reload. So the task says notify: reload nginx. At the bottom, under handlers, is a task with that name. A handler runs at the end of the play, and only if a task that notifies it changed something.
tail -13 site.ymlcopy: content="Ana's phone 555-0142" dest=/var/www/html/staff.txt - name: nginx says which server answered copy: content: "add_header X-Served-By $hostname;\n" dest: /etc/nginx/conf.d/served-by.conf notify: reload nginx handlers: - name: reload nginx service: name: nginx state: reloaded
First run: the setting is changed, so the handler runs on all three. curl dash capital I asks web2 for the response headers only, and grep keeps the new one: it says who served it. The run after that: the setting is ok, so there's no handler, and no reload at all.
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web2] ok: [web1] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web2] ok: [web3] ok: [web1] TASK [the shop's page is up to date] ******************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the staff notice has Ana's phone] **************************************** ok: [web1] ok: [web2] ok: [web3] TASK [nginx says which server answered] **************************************** changed: [web2] changed: [web1] changed: [web3] RUNNING HANDLER [reload nginx] ************************************************* changed: [web1] changed: [web3] changed: [web2] PLAY RECAP ********************************************************************* web1 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=7 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
curl -sI web2 | grep X-ServedX-Served-By: web2
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web2] ok: [web3] TASK [the shop's page is up to date] ******************************************* ok: [web2] ok: [web3] ok: [web1] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the staff notice has Ana's phone] **************************************** ok: [web1] ok: [web2] ok: [web3] TASK [nginx says which server answered] **************************************** ok: [web1] ok: [web2] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
A server that can't answer
What if a server can't be reached? We stop web3's SSH, as if it were halfway through a reboot. Ansible marks it UNREACHABLE, right at the first task, and carries on with the other two. The recap shows it, so Ana knows exactly which server to run it on again: dash dash limit web3 runs the playbook on that one server.
ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* fatal: [web3]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host web3 port 22: Connection refused", "unreachable": true} ok: [web1] ok: [web2] TASK [nginx is installed] ****************************************************** ok: [web2] ok: [web1] TASK [the shop's page is up to date] ******************************************* ok: [web1] ok: [web2] TASK [nginx is running] ******************************************************** ok: [web1] ok: [web2] TASK [the staff notice has Ana's phone] **************************************** ok: [web1] ok: [web2] TASK [nginx says which server answered] **************************************** ok: [web1] ok: [web2] PLAY RECAP ********************************************************************* web1 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
One change, every server
Winter hours: nine to four. Ana edits one line in the variables file, and runs the playbook. The page changes on all three servers, in 4.0 seconds, and nothing else is touched. Three servers or three hundred, it's the same line.
cat group_vars/web.ymlshop: Powder Peak Ski Rentals hours: "9:00 to 16:00"
time ansible-playbook site.ymlPLAY [Web servers for the ski shop] ******************************************** TASK [Gathering Facts] ********************************************************* ok: [web1] ok: [web2] ok: [web3] TASK [nginx is installed] ****************************************************** ok: [web1] ok: [web3] ok: [web2] TASK [the shop's page is up to date] ******************************************* changed: [web1] changed: [web3] changed: [web2] TASK [nginx is running] ******************************************************** ok: [web3] ok: [web2] ok: [web1] TASK [the staff notice has Ana's phone] **************************************** ok: [web2] ok: [web1] ok: [web3] TASK [nginx says which server answered] **************************************** ok: [web2] ok: [web1] ok: [web3] PLAY RECAP ********************************************************************* web1 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web2 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 web3 : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 real 0m3.972s user 0m0.826s sys 0m0.277s
for w in web1 web2 web3; do curl -s $w | tail -1; done<p>Open every day, 9:00 to 16:00</p> <p>Open every day, 9:00 to 16:00</p> <p>Open every day, 9:00 to 16:00</p>
Where it fits in DevOps
So where does Ansible fit in DevOps? A playbook is plain text, so it lives in Git, next to your code. A change to the servers becomes a change someone can review. Then a pipeline, a service that runs steps for you after each change, can run the same playbook every time, so a new server is built exactly like the old ones, and nobody types commands into production by hand.

Recap
So: an inventory lists your servers. A playbook says how they should be. Ansible logs in over SSH and fixes only what's different, so running it again is safe. And describe states, not actions.

If your servers finally agree with each other, subscribing brings the next tool, run for real.
Every run in this video was real, on four small Linux machines, and the playbook and every command are linked in the description. Thanks for watching Cloud Explainers.
Sources
- Ansible docs, Glossary (Idempotency, Handlers)
- Ansible docs, Introduction to playbooks
- Ansible docs, How to build your inventory
- Ansible docs, Handlers: running operations on change
- Ansible docs, Validating tasks: check mode and diff mode
- Ansible docs, the shell module (not idempotent; always reports changed)
Demos: containers on our own Mac running Ubuntu's own Ansible and nginx; the ski shop and its staff are made up. Every run and result was real. Watch the full video on YouTube.