Ansible is an infrastructure-as-code tool that lets you manage and monitor multiple remote servers using a single control node.
With Ansible, you can use Playbooks to manage remote servers. These playbooks relay instructions to remote servers and allow them to perform predefined tasks.
How do I create and configure a playbook? In this tutorial, we’ll cover what Ansible playbooks are, how they work, and how to create and execute them, including related examples of creating and configuring playbooks.
Prerequisite
- A Linux system (we are using Ubuntu 20.04)
- Access the command line/terminal window
- Ansible installation and configuration
What is Ansible Playbook?
An Ansible playbook is a list of job instructions that are automatically executed by a remote computer in an Ansible network.
Note: If you don’t have Ansible installed, check out our guide on installing Ansible on Ubuntu and Windows.
Ansible playbooks are made up of playbooks. Plays are a well-designed set of tasks that are completed once the playbook is executed. You can add multiple playbooks to a single playbook.
Plays uses modules to define what changes need to be made to complete a task. Each module provides a framework for a specific category of work and can be customized using parameters and variables.
Ansible Playbook is saved as a . yaml files, which makes them flexible and easy to use.
Playbooks are mainly used for:
- Declare the configuration.
- Automate certain repetitive tasks that take a long time to do manually.
- Start tasks synchronously or asynchronously.
Playbook Creation and Configuration Tutorial: Ansible Playbook Variables
Ansible playbooks use variables to provide flexibility and ease of use. Variables can be built-in (e.g., system information) or user-defined.
Note: All variable names in Ansible must start with a letter or underscore and cannot contain any spaces. Numbers are allowed.
In Ansible playbooks, variables are defined by using the vars
keyword:
In the example above, we define a variable with greeting
assigned to Hello World!
.
To access the value, put the variable name greeting
in double curly braces:
Once executed, the Playbook prints out the message Hello World!
In addition to variables with a single value, there are several other types of Ansible variables. Using variables with arrays, assign multiple values to a variable using the following syntax:
vars:
array_name:
- value1
- value2
…
- valueN
Use to access a specific value{{ array_name[value number] }}
, for example:
Important: Array values are numbered from zero, so always choose a number lower than the number you want to access.
Variables with dictionaries combine multiple arrays of values into a single variable:
vars:
array_name:
dictionary_name1:
item1: value1
item2: value2
dictionary_name2:
item1: value1
item2: value2
Finally, special variables are predefined by Ansible and cannot be set by the user. Use the following command to get a list of special variables:
ansible -m setup hostname
How do I write an Ansible playbook?
How do I create and configure a playbook? Create an Ansible playbook using any text editor available on your system. For example, create a playbook with Nano:
sudo nano /path/to/playbook/directory/playbook_name.yaml
Ansible playbooks follow basic YAML syntax rules.
The playbook starts with three n dashes (---
), followed by the name of the playbook, the name of the remote host group that executed the playbook, and other parameters such as come
(executing the playbook as an administrator).
The next section contains all user-defined variables. It starts with the same indentation level tag as in the previous section of vars
, followed by a list of variables in the next indentation.
Note: Pressing the space button twice is sufficient to create a new indentation level.
Finally, the task list starts with the tasks
tab, and the level of indentation depends on the task type and the parameters used.
The YAML syntax allows you to end the playbook with three periods (...
). Although most Playbooks end up this way, it is not necessary for the Playbook to work properly.
Ansible Playbook syntax
There are some important elements of YAML syntax to keep in mind when writing Ansible playbooks:
- When writing a list, use a space between the dash (
-
) and the list entry. - When using
the key: value
table, the colon must be followed by a space. - Booleans can use
both yes
andno
,true and
false
. - Use a space followed by
#
for comments.
Example of creating and configuring a playbook: Playbook example
How do I create and configure a playbook? For this example, we’ll create a playbook in the Ansible installation folder:
sudo nano /etc/ansible/example_playbook.yaml
The playbook contains the following elements:
---
: indicates the start of the playbook.name
: Defines the name of the Ansible playbook.hosts
: Defines which hosts execute the playbook. In the example above, the playbook is executed on all hosts included in the manifest file.become
: Instructs the remote host to execute the playbook as an administrator. Using this tag requires you to enter the administrator password of the remote host when executing the playbook.vars
: Define variable – in our case, it’s a variable named after thegreeting
valueHello World!
.tasks
😛 a list of tasks to be executed by the laybook. In the example above, the first task creates a directory named example_playbook on the remote host. The second task creates a new text file in that directory and takes the value of thegreeting
variable as the content....
: indicates that the playbook has ended.
How are playbooks executed?
Ansible playbooks are executed sequentially, from top to bottom. This is true for both individual playbooks and the tasks listed in those playbooks.
In the example above, the playbook does the following in order:
- Define a list of variables.
- Create a new folder on the remote host.
- Create a text file in a new folder.
- Add the value of the variable as the file content.
Playbook Creation and Configuration Tutorial: How to run an Ansible playbook
To execute an Ansible playbook, use:
ansible-playbook /path/to/playbook/directory/playbook_name.yaml
For the purposes of this article, we’ll use example_playbook.yaml
with -K
terminology, which allows us to enter the root password for the remote host and perform playbook management:
ansible-playbook /etc/ansible/example_playbook.yaml -K
Example of creating and configuring a playbook
How to verify a playbook
How do I create and configure a playbook? Run the playbook with the --check
flag to check the mode to verify it. This allows you to see the changes made to your playbook without actually making the changes:
ansible-playbook /path/to/playbook/directory/playbook_name.yaml --check
Example of creating and configuring a playbook – For a more detailed overview of potential changes, use a combination of the --check
and --diff
flags:
ansible-playbook /path/to/playbook/directory/playbook_name.yaml --check --diff
Helpful Ansible Playbook tips
There are a few ways to make Ansible playbooks easier to read and use:
Always name the Playbook and the task
Use the name
keyword to define the name of the playback and task. Adding a short and straightforward name makes it easier to understand what each game or mission does at a glance.
Use comments
Make sure to add a lot of comments, especially when defining variables and tasks.
Detailed annotations help make the playbook more clear and easy to understand. They can also help others use your Playbook.
Format the Playbook for ease of use
While you’ll need to follow the indentation rules to make the playbook work, you can still use spaces (such as blank lines between tasks) to make the playbook easier to read.
Playbook Creation and Configuration Tutorial Conclusion
How do I create and configure a playbook? By the end of this tutorial, you should now have some understanding of Ansible playbooks, including how to create and execute them. Playbooks can be used to define a variety of tasks to be performed by a remote host, including checking for the presence of files and folders.
If you’re considering Ansible alternatives, learn how they stack up against each other in our Ansible vs Terraform vs Puppet article.