Infrastructure and Automation · Topic 5 of 8
Interpreting an Ansible Playbook
Read an Ansible playbook and work out what it automates: packages, users, config files, services, variables, handlers and the inventory it targets.
- 5 min
- A playbook is YAML with plays, and a play has
hosts, optionalvarsand a list oftasks. - Each task calls a module, such as
apt,user,copyorservice. state: presentinstalls or creates, andstate: absentremoves.
What it is
An Ansible playbook is a YAML file that lists the tasks to run on a group of hosts.
Each task calls a module, which is a small unit of work such as installing a package or starting a service. Reading a playbook means finding the hosts, then going through the tasks in order.
Ansible is agentless. It connects over SSH, or an API for network devices, so nothing needs to be installed on the managed hosts.
How it works
A playbook holds one or more plays, and each play has hosts, tasks and options.
A sample playbook
---
- name: Set up a web server
hosts: webservers
become: true
vars:
web_package: nginx
tasks:
- name: Install the web server package
ansible.builtin.apt:
name: "{{ web_package }}"
state: present
update_cache: true
- name: Create a service account
ansible.builtin.user:
name: webadmin
shell: /bin/bash
state: present
- name: Copy the site configuration
ansible.builtin.copy:
src: site.conf
dest: /etc/nginx/conf.d/site.conf
mode: "0644"
notify: Restart nginx
- name: Make sure the service is running and enabled
ansible.builtin.service:
name: nginx
state: started
enabled: true
handlers:
- name: Restart nginx
ansible.builtin.service:
name: nginx
state: restarted
Reading it top to bottom
nameis a label for humans. It does not change behaviour.hosts: webserverspicks the group from the inventory that the play runs on.become: trueruns the tasks with elevated privileges, as with sudo.varsdefines variables, and{{ web_package }}inserts the value.- Each task names a module and gives its arguments.
The play therefore installs the nginx package, creates a user named webadmin, copies a configuration file, and makes sure the service is running and set to start at boot. The handler restarts nginx, but only if the copy task changed something.
Common modules
| Module | Typical use | Key arguments |
|---|---|---|
ansible.builtin.apt | Install or remove packages on Debian and Ubuntu | name, state |
ansible.builtin.yum or dnf | Package management on Red Hat family systems | name, state |
ansible.builtin.package | Package management that picks the right tool | name, state |
ansible.builtin.user | Create, change or remove a user account | name, state, shell |
ansible.builtin.copy | Copy a file to the host | src, dest, mode |
ansible.builtin.template | Fill in a template and copy the result | src, dest |
ansible.builtin.service | Start, stop, restart or enable a service | name, state, enabled |
ansible.builtin.command | Run a command | the command text |
The state argument
Most modules are described by the state they should reach, not by the action to take.
| State | Meaning |
|---|---|
present | The package, user or file should exist |
absent | It should be removed |
started | The service should be running |
stopped | The service should be stopped |
restarted | The service should be restarted |
Because modules check the current state first, running a playbook again changes nothing if the host already matches. This property is called idempotence.
Handlers
A handler is a task that runs only when another task reports a change. It runs once at the end of the play. This is why a service restarts only when its configuration file really changed.
The inventory
The inventory lists the hosts and groups that playbooks can target.
[webservers]
web1.example
web2.example
[dbservers]
db1.example
Running a playbook
$ ansible-playbook -i inventory.ini site.yml
$ ansible-playbook -i inventory.ini site.yml --check
The --check option performs a dry run. The output ends with a recap, which counts tasks as ok, changed, failed or skipped.
Describing the workflow
When you interpret a playbook, answer in a sentence. For the sample above: it installs nginx on the web servers, creates a service account, deploys a configuration file, and ensures that nginx is started and enabled, with a restart if the file changes.
Stopping a service
A task with state: stopped and enabled: false stops a service and keeps it from starting at boot.
- name: Stop and disable the service
ansible.builtin.service:
name: nginx
state: stopped
enabled: false
Ignore the task names and read the modules and their arguments. The module says what happens, and state says the desired result.
What to remember for the exam
Seven facts cover this topic.
- A playbook is YAML with plays, and a play has
hosts, optionalvarsand a list oftasks. - Each task calls a module, such as
apt,user,copyorservice. state: presentinstalls or creates, andstate: absentremoves.state: startedorstoppedcontrols a service, andenabledcontrols start at boot.become: trueraises privileges.- A handler runs only when notified by a task that changed something.
- Modules are idempotent, so repeated runs make no change if the host already matches.
Common mistakes
Four mistakes are typical.
Reading the task name as the action. The name is a label, so check the module.
Confusing started with enabled. One runs the service now, and the other starts it at boot.
Forgetting become. Installing packages and starting services needs elevated rights.
Expecting a handler to run every time. It runs only if a notifying task changed something.
Be careful with the command module. It runs every time, so it is not idempotent unless you add conditions.
Check yourself
Work through these three questions.
What does
state: presentmean for a package or a user?It means that the package or user should exist, and Ansible installs or creates it if missing.
When does a handler run?
Only when a task that notifies it reports a change, and then once at the end of the play.
What is the difference between
startedandenabledon a service?startedruns the service now, andenabledmakes it start at boot.
Topic 5 of 8 in Infrastructure and Automation
Test yourself on Infrastructure and Automation
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.