Create a Playbook

Make Ansible tasks repeatable using playbooks.

Playbooks are the basis of configuration management, and orchestrated automation with Ansible, and are designed for automating repeatable tasks.

Playbooks are expressed in YAML and have a minimum amount of syntax. YAML is a model of a configuration or a process. Because playbooks define a process and/or configuration, they are kept in source control.

Each playbook is composed of one or more plays. A play starts by defining the hosts line. The hosts line is a list of one or more groups or host patterns separated by colons.

Press + to interact
---
- hosts: localhost

Optionally, you can define the user to run the play as.

Press + to interact
---
- hosts: localhost
remote_user: root

The hosts line is followed by a tasks list. Ansible executes tasks in order, one at a time, against all the machines matched by the pattern in the hosts line.

Press + to interact
---
- hosts: localhost 1
tasks: 2
- name: create a directory 3
file:
path: /ansible/playbooks
state: directory

If a task fails to run on a host, it is taken out of the entire playbook’s rotation.

A playbook is not limited to a single play. You can define multiple plays within a single playbook. Keep in mind that play maps tasks to a host or group of hosts.

Press + to interact
---
- hosts: webservers
tasks:
- name: deploy code to webservers
deployment:
path: {{ filepath }}
state: present
- hosts: dbserver
tasks:
- name: update database schema
updatedbschema:
host: {{ dbhost }}
state: present
- hosts: webservers
tasks:
- name: check app status page
deployment:
statuspathurl: {{ url }}
state: present

All the highlighted lines represent the start of a play.

  • Play One - deploy code to web servers.
  • Play Two - update the database schema.
  • Play Three - check app status page.

The modules here are used for demonstration, They are not real Ansible modules.

Create your first playbook

A playbook is a natural progression from ad-hoc commands, making it less tedious.

Let’s look at an ad-hoc command that helps you ping a remote server.

Press + to interact
ansible all -i <Public Ip Address>, -m ping

Write a playbook

Convert the ad-hoc command to ping a remote host into a playbook. Run the playbook to ping a docker container.

We’ll go through each step one by one.

  1. Create a ping.yml file.
  2. Add the Hosts line using the host pattern of all.
Press + to interact
---
- hosts: all

The host pattern of all is used to match the default Ansible group that matches all hosts within an inventory or host list.

  1. Add a tasks list.
Press + to interact
asks:
- name: run ping
ping:

The final form of the ping.yml will look like the one below:

Press + to interact
---
- hosts: all
tasks:
- name: run ping
ping:

Run a playbook

To run a playbook, use the ansible-playbook command. At a minimum, it requires the path to the playbook and an inventory source. The inventory source can be a dynamic inventory, static inventory, or a host list.

In this scenario, you will use an inventory to provide the container’s IP address. To specify the host list, use the -i option, just as you did with the Ansible command.

We will look into the host’s list method and the inventories in greater depth in the upcoming chapters.

Use the following command:

Press + to interact
ansible-playbook docker-ping.yml -i inventory

Click on the Run button. Wait for the environment to be set up. Once done, execute the ansible-playbook command in the terminal.

---
- hosts: all

  tasks:
  - name: run ping
    ping:
execute ping.yml

Review the output of the playbook. It starts with the first play. The first play targets the host pattern of all and is called out when the play starts.

Press + to interact
PLAY [all]
*********************************************************************************
TASK [run ping]
****************************************************************************
ok: [python-container]
PLAY RECAP
*********************************************************************************
python-container : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Ansible then moves on to the tasks list and executes the run ping task. This task calls the ping Ansible module and runs against the all group targets.

Press + to interact
TASK [run ping]
[WARNING]: Platform linux on host python-container is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
ok: [python-container]

After the tasks are completed, the playbook outputs a recap. This recap lists:

  • targeted hosts
  • status

The status returns counts informing you of how many tasks:

  • Returned okay
  • Changed
  • Unreachable
  • Failed
  • Skipped
  • Rescued
  • Ignored
Press + to interact
PLAY RECAP
*********************************************************************************************
python-container : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Writing a playbook for the ping task simplified the command used. It did this by moving the host pattern and modules into the playbook.

In this lesson, we introduced playbooks and how you can do the same thing as ad-hoc commands using playbooks.

The current version of the docker-ping.yml playbook is more comfortable to run but not necessarily more repeatable.

We will explore more detailed examples in the upcoming chapters.

Get hands-on with 1300+ tech skills courses.