Create a Playbook
Make Ansible tasks repeatable using playbooks.
We'll cover the following
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: localhost
Optionally, you can define the user to run the play as.
---- hosts: localhostremote_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.
---- hosts: localhost 1tasks: 2- name: create a directory 3file:path: /ansible/playbooksstate: 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.
---- hosts: webserverstasks:- name: deploy code to webserversdeployment:path: {{ filepath }}state: present- hosts: dbservertasks:- name: update database schemaupdatedbschema:host: {{ dbhost }}state: present- hosts: webserverstasks:- name: check app status pagedeployment: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.
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.
- Create a
ping.yml
file. - Add the
Hosts
line using the host pattern ofall
.
---- 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.
- Add a
tasks
list.
asks:- name: run pingping:
The final form of the ping.yml
will look like the one below:
---- hosts: alltasks:- name: run pingping:
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:
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:
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.
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.
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. Seehttps://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for moreinformation.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
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.