Playbook

Learn about Ansible playbooks, which are a list of automation procedures against an inventory.

An Ansible playbook is a set of plays to be executed against an inventory. In this lesson, we’ll explain why we need an Ansible playbook. We’ll start with a simple playbook from the basic syntax and add more tasks.

YAML syntax

YAML is a human-readable programming language with a shallow learning curve. Let’s take a look at the following YAML example file—this example file is not an Ansible document.

Press + to interact
---
# This is a YAML comment
some data # This is also a YAML comment
this is a string
'this is another string'
"this is yet another a string"
with_newlines: |
Example Company
123 Main Street
New York, NY 10001
without_newlines: >
This is an example
of a long string,
that will become
a single sentence.
yaml_dictionary: {name1: value1, name2: value2}
yaml_list1:
- value1
- value2
yaml_list2: [value1, value2]
...

Every playbook is based on YAML syntax, making the file easy and human-readable. YAML is a text format, and we can easily recognize it through the presence of the three dashes (---) at the beginning and the three dots (...) at the end. The three dots (...) are not mandatory. Therefore, we can omit them. Unlike certain programming languages, the INI file format relies on consistent spacing and indentation between elements at the same level.

> Note: YAML is highly sensitive to indentation. Correct and consistent indentation is crucial for accurately representing the structure and hierarchy of data in the YAML files.

Comments in YAML can be denoted using the # symbol, even on lines with existing code. String values in YAML can be specified directly or enclosed in single or double quotes. It is generally recommended to use double quotes. To define multiline strings, YAML provides two options: the pipe symbol (|) preserves newlines, while the greater-than symbol (>) removes them. YAML also supports dictionaries and lists, which can be observed in action on the grayboard.

> Note: It is important to include one empty line at the end of an Ansible file. This practice ensures proper formatting and adheres to standard conventions. However, omitting the empty line at the end of an Ansible playbook or including more than one line might generate a warning message.

Example: The helloworld.yml playbook

The first Ansible playbook displays a simple "Hello World!" message on the ...