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.

Yaml Tutorial | Learn YAML in 18 mins

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.

Lists

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]

Dictionaries

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

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: https://yaml.org/spec/1.2/spec.html.

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



About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

Your email address will not be published.