Ansible: YAML Basics

Understanding YAML is essential as it is a popular markup language. In addition, you need to get familiar with the basics of the YAML syntax to master the art of writing playbooks in Ansible.

YAML is often called “Yet Another Markup Language” because it appeared in the era of other markup languages such as (HTML, XML, etc.). However, later its reference changed to “YAML Ain't Markup Language” to emphasize its data-oriented purpose. Indeed, it is used for configuration files and other declarations. For example, in the case of Ansible, you may think of a playbook written in YAML as a declaration of configuration procedures and processes.

Basic YAML components

Starting a YAML file

Although optional, it is a clever idea to start your file with --- and end with ... to let everyone know that you are using YAML.


Your Ansible tasks will appear as a list, and here are two options to define lists:

	- Item one
	- Item two
	- item three

Alternatively, you can define your list like below:

[task one, task two, task three]


Key-value pairs known as dictionaries are usually defined as follows:

os: Linux
build: Ubuntu
version: 18.10

However, they may look the same as Python dictionaries:

{os: Linux, build: Ubuntu, version: 18.10}


Comments are defined using #, which you can place on a separate line or inline.

# This is a comment
key: value # Comments can be placed here as well

Whitespace in YAML files

Whitespace is part of YAML's format. A new line indicates the end of a field except for a specific case such as line folding. Just like in Python, YAML needs indentation. The indentation level can be one or more spaces. However, unlike Python, the specification does not allow tabs, so we must use spaces instead.

Strings variables

Defining strings is very straightforward in YAML, and you do not have to use single-quotes or double-quotes. However, if you have any special characters, you need to use quotes. For example, if you look at the dictionary, it contains a colon. Therefore, if your string contains a colon, you must use double quotes to ensure that YAML does not treat it as a key-value pair.

A couple of useful advanced string features is line folding. For example, if you want to keep your file neat and avoid long strings, you can use > or | sign. The string after > is treated as a single line. In comparison, the string followed by | is treated as is.

key: >
  A long value
  which stretches across 
  multiple lines 
  but it is treated as a single line

key: |
  This time
  you will get 
  four lines 
  instead of one

Boolean variables

You can define Boolean variables as True, False, Yes, No, On, or Off.

become: True
become: False
	key: On
	key : No

Nesting data structures

Nesting data structures are data structures that contain the same data structures within. It is much easier to visualize using this example. As you can see, dictionaries can contain other dictionaries.

pi : 3.14
exp : 199.3455e+05
int : 2021

Wrapping up

You now understand the basics of YAML when using it with Ansible. However, you may want to use a cheat sheet initially. You will, in time, remember the most common syntax features as you practice.

The complete list of YAML specs is available here:

You may also be interested in

About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee

Leave a Reply

Your email address will not be published. Required fields are marked *