YAML Ain’t Markup Language

In this lesson we’re going to be going over YAML.

Now YAML is a fun acronym, it is what’s known as a recursive acronym. because it uses its own name within the acronym definition. YAML stands for YAML ain’t markup language.

YAML is a structured data serialization language and it is what is mostly used by Ansible for playbook creation, as well as by PyEZ, where you can use YAML to go ahead and define and load information for Jinja or PyEZ to structure your data in a very human readable format that is also highly structured and machine friendly, and easy to convert into Python data structures.

YAML basics

First up, our YAML basics. I do want to point out that you should make sure that you are aware and know everything on this image particularly well for the exam.

YAML is case sensitive, so something written with a capital letter like the word Basics here with a capital b, is different than the word basics with a lowercase b.

YAML uses indentation for data structure, a lot like Python. YAML uses white space to define the hierarchy of where different data structures fall in for their code blocks. YAML only uses spaces not tabs. A lot of your text editors will convert your tabs into spaces if you are encoding in YAML, however if it doesn’t you might run into a loading error because tabs are not valid YAML syntax, so always use spaces for indentation in your YAML files and not tabs.

YAML has a standard practice of starting the document with three hyphens, — , on the first line and then moving to a new line before we start actually putting our data. This is just a convention that is used, it is not generally required by the interpreters but it is a standard practice and I would recommend doing it so that when you open a file you can easily determine that this is a YAML file, as opposed to some other type of encoding.

So unlike JSON, YAML does allow for comments using the hash symbol ( # ). This comments out an entire line if it is at the beginning of the line, or everything afterwards if it is somewhere in the middle. This may end up having us question “well are we able to use the hash symbol within our YAML variables?” and the answer is yes, as long as you have it within quotation marks. The quotation marks will effectively escape any special characters and you can go ahead and use that to have your strings contain characters like a hash.

So YAML data structures, as we mentioned a little earlier in the course, are mappings and sequences, where mappings are equivalent to dictionaries in Python and sequences are equivalent to lists in Python.

It’s important to note that YAML is a superset of JSON. I have seen this question pop up on the exam two times now, where it is asking what kind of set is YAML of JSON. YAML is a superset, which means that all of JSON is valid YAML, however not all of YAML is valid JSON.

YAML Mappings

Example of a YAML file with 3 mappings

YAML mappings are a set of key-value pairs. This is equivalent to a dictionary in Python and a JSON object. In the example above, we have first our three hyphens that define the start of our YAML document and then we have three individual mappings here. Our first mapping has the key of host-name and the value of the string Router 1, the second a key of version and a value of 12.1R3.

It’s important to note that all of these key-value pairs have a space after the colon. That space is required for valid YAML, you must have that space after your colon to have a mapping with a string. We can also encapsulate our strings in quotation marks, and that will also be a string as well.

YAML Sequences

Example of a YAML file with a sequence with 3 entries

YAML sequences are ordered sets of values. There are no keys here, only values. They are notated with a hyphen before the value. They are equivalent to a JSON array and a Python list. An example of how to define a YAML sequence is above, with our three hyphens defining the start of our YAML document. Then we have a hyphen and a space and then our sequence value. Those spaces again are very important, without the space between the hyphen and the value, it is not valid YAML syntax.

example of a YAML file with sequences and mappings nested within each other

We can intermingle these sequences and mappings, and mix them together. We can have nested mappings and sequences. Above is an example of a more complicated, valid, YAML file. So let’s run through this briefly and talk about what each of these items are.

First we have the start of our document with the three hyphens, and then our first mapping, our host-name key, then another mapping of our version key, and another mapping of our chassis key. All of these mappings have an individual value, the values are strings and they are all to the right of our colons. Next we have another mapping, our authentication-servers mapping and this value is a little more complicated. The value of this mapping is a sequence and that sequence has two items where each item is an IP address. Each IP address is defined by our hyphens starting those lines.

Next we have another mapping, our interfaces mapping. Now, this interfaces mapping has a sequence item where the sequence item is a mapping. The first item in that sequence is a mapping of the name value being ge-0/0/0 and then we have another mapping within this same sequence item, we’re still in the first sequence item here and the next mapping is primary. Finally we have the second item in the sequence which is a mapping of the name and primary keys as well.

For a full demonstration and example code to convert between YAML and JSON, please take a look at the full lesson video below!

Only subscribers can view the full content. Join Now!

Scroll to top

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

CiscoLessons will use the information you provide on this form to send occasional (less than 1/wk) updates and marketing.