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.

Hosts

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

Define the user

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.

Tasks

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.

pseudo playbook multiple plays

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.

linux ad-hoc 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.
Hosts

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.
Tasks

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

ping.yml

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:

execute ping.yml

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

/
docker-ping.yml
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.

ping.yml output

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.

Task "run ping"

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
Recap

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.

Introduction to Ad-Hoc Commands
Summary
Mark as Completed
Report an Issue