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.

YouTube player

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 Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.