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
In a nutshell
  • A playbook is YAML with plays, and a play has hosts, optional vars and a list of tasks.
  • Each task calls a module, such as apt, user, copy or service.
  • state: present installs or creates, and state: absent removes.

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.

Inventorylist of hoststargeted byPlaybookplays and taskscallsModulespackage, user, copy, servicechangeHostsdesired state

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

  • name is a label for humans. It does not change behaviour.
  • hosts: webservers picks the group from the inventory that the play runs on.
  • become: true runs the tasks with elevated privileges, as with sudo.
  • vars defines 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

ModuleTypical useKey arguments
ansible.builtin.aptInstall or remove packages on Debian and Ubuntuname, state
ansible.builtin.yum or dnfPackage management on Red Hat family systemsname, state
ansible.builtin.packagePackage management that picks the right toolname, state
ansible.builtin.userCreate, change or remove a user accountname, state, shell
ansible.builtin.copyCopy a file to the hostsrc, dest, mode
ansible.builtin.templateFill in a template and copy the resultsrc, dest
ansible.builtin.serviceStart, stop, restart or enable a servicename, state, enabled
ansible.builtin.commandRun a commandthe command text

The state argument

Most modules are described by the state they should reach, not by the action to take.

StateMeaning
presentThe package, user or file should exist
absentIt should be removed
startedThe service should be running
stoppedThe service should be stopped
restartedThe 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
Exam tip

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, optional vars and a list of tasks.
  • Each task calls a module, such as apt, user, copy or service.
  • state: present installs or creates, and state: absent removes.
  • state: started or stopped controls a service, and enabled controls start at boot.
  • become: true raises 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.

Watch out

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: present mean 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 started and enabled on a service?

    started runs the service now, and enabled makes 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.