Introduction
The term “Human Readable Automation” is often associated with Ansible and is one of the major driving factors behind the exponential increase in its popularity over the last few years. YAML is used in conjunction with Ansible in the sense that it tells Ansible how to run complex deployments and apply configurations. It is a perfect candidate for Ansible playbooks because of its ease of creation and readability in a top-down manner used by Ansible for applying the configuration described in our playbook. Within Ansible, the playbook content is linear and not object-oriented like in some other deployment mechanisms. In this article, we will cover a basic introduction to YAML along with a couple of examples. Although a deep knowledge of YAML is not required for working with Ansible some basic knowledge of YAML syntax is helpful when writing Ansible playbooks to describe and apply our required configurations.
What is YAML?
During its initial development, YAML was reported to stand for “Yet AnotherMarkup Language”. However, once it was completed as a standard, that acronym was morphed into “YAML Ain’t Markup Language.” In short, YAML is meant to be a “human-readable data serialization format”. In other words, it’s meant for non-computers (us) to be able to read easily and recognize the significance of its content without complex translation applications. It was designed to be easy to map to high-level languages, and you will often see it compared to key/value lists, associative arrays, and data outlines.
Structure
As a data structure, YAML most closely resembles an outline or list of items with basic descriptions. For example, if we wanted to list our favorite music bands in a way that YAML processing engines would be able to recognize, our YAML file would contain something like:
--- # My Favorite bands - Fall Out Boy - Greenday - Switchfoot
First, we have the three dashes indicating the beginning of the YAML file followed by a # symbol to indicate a comment. As with other languages, we write comments to describe the information or logic that follows the file or code. After the comment, we have a single dash to delineate the names of some of my favorite bands. There are different ways of delineating content in a YAML file and we will cover that shortly.
YAML has been documented as a standard and adopted by many applications for configuration. One of the reasons Ansible chose YAML to write configurations it is not only because of it’s ease of readability and configuration but also because Ansible being based on python has a significant text processing engine advantage. Python is very good at processing large amounts of text both formatted and unformatted. So, YAML made a natural method for importing complex configurations in a top-down model that could be read as an Ansible playbook. Using YAML allowed configurations in defined in Ansible playbooks to be easily created, deployed and understood by the engineers maintaining them.
We’ll now look at a couple of more YAML examples to understand it’s syntax. Please note that these examples are meant to provide a better understanding of YAML and do not apply specifically to Ansible.
Simple lists:
Given below is an example of a simple list.
--- # My favorite fruits - Apple - Orange - Plum
We’ve commented the list at the top with the three dashes and the pound sign. This line will be ignored by the YAML processing engine. After that, each line is delineated with a dash to indicate a new item.
Inline formatting of simple lists:
There is a more compact format available in YAML to represent simple lists.
We could represent the simple list from the previous example in a more compact form using inline formatting as shown below.
--- # My favorite fruits [Apple,Orange,Plum]
Some people may find this format more easy to read and it also does save some space on your computer screen.
Associative arrays/key-value pairs:
A key-value pair is just as the name implies, there is a key (consider it analogous to a variable) that has a corresponding value (the value for that variable). Given below is an example representing key-value pairs in list format.
--- # My information name: Sahil Suri age: 28 designation: devops engineer
Note that as described in the above example, we DO NOT have to delineate key-value pairs by dashes. On the left-hand side is the field or the key and on the right-hand side of the colon symbol is the corresponding value for the field. Each key-value pair is on a separate line and the new line is delineated by a carriage return line feed. We could have whitespaces within the line but as soon as we move to the next line, we will be writing or representing the next field.
Inline formatting of associative arrays:
We can use inline formatting of associative arrays as we did with simple lists earlier. Given below is an inline representation of the associative arrays we described in simple list format in the previous example.
--- # My information {name: Sahil Suri,age: 28, designation: devops engineer}
Here because we are using associative arrays, we needed to enclose the contents within curly braces {} instead of square brackets [] as we did for simple strings earlier.
Note that strings do not require a quotation and new lines are preserved in order to function as a delimiter for the field.
New line preservation in YAML:
When we are typing in a multiline string in YAML, we could use a pipe symbol to indicate that new lines need to be preserved while displaying the string or line on the terminal. If we do not use a | symbol and specify a > symbol then YAML folds the new lines and attempts to print as such content on the same line as the screen or console allows it to. Also, blank lines indicate new paragraphs. Given below is a quick example:
--- # Preserving new lines typing: | Welcome to Linuxnix Home to awesome Ansible tutorials --- # Newline folding folding: > This is a YAML Line folding example.
Data type casting in YAML:
Generally, YAML detects datatypes on its own but we can explicitly specify or type cast a data type using double exclamation marks. Here are a couple of examples:
--- s: 007 #an integer a: "007" # a string h: 007.00 #a float i: !!str 007 #explicit string data type specification l: !!float 007 #explicit float data type specification
We use the !! symbol followed by the data type that we would like to cast to a value.
Ansible playbook example
Given below is a very basic Ansible playbook that performs a task of installing the telnet package.
[root@linuxnix ~]# cat play.yml --- - name: a very basic playbook hosts: all gather_facts: no tasks: - name: install telnet yum: name=telnet state=installed
At this point, we need to just focus on the YAML syntax. We will be covering writing Ansible playbooks in detail in a subsequent post.
Conclusion
YAML is a powerful configuration tool for our DevOps toolkit and is the perfect candidate for compiling and running complex configuration and deployment tasks in our Ansible playbooks. We hope that you’ve found this introduction to YAML useful since a clear understanding of basic YAML syntax is important to be able to write complex and accurate Ansible playbooks.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021