YAML Ain’t Markup Language

Markup Languages

Let’s start at the beginning. What is a Markup Language? Simply put, it’s a way of writing something on a computer that might help it present as visually more appealing to the end user. For example HTML allows web designers to change font size, background color, and introduce paragraphs. There are other markup languages like XML and JSON. These are often used to provide input information to applications, but are commonly described as formats for data interchange. An example of using XML or JSON would be to use a REST client like Postman to talk to the APIs of a network switch and either get or modify information. XML or JSON would be the format you use in the body of the REST client to specify what you want to happen (GET or POST).

YAML Ain’t Markup Language

Well, it’s a little like a markup language. Originally it was called Yet Another Markup Language, but was re-branded to make sure there was more of a spotlight on the configuration file aspects and data serialization of it rather than creating documentation formatting/markup and it’s often used in similar ways as XML and JSON.

Basic Data Structures

Let’s talk about what this looks like. We could have Key Value Pairs. The key, which is the word before the colon, and the value, the word after the colon. These two items are linked:

vehicle: truck
tree: oak
phone: android

We can also have Arrays or Lists. A little like a Key Value Pair, but we might have several items in a list as shown here:

vehicle:
–     truck
–     car
–     van

The category at the top describes what kind of data it is, and then we add a hyphen followed by some spaces to specify the different items that go under that category. Arrays are considered to be an ordered data structure because it matters where the item falls on the list. If we were to reverse van and car that would be considered to be a totally different list since it’s in a different order.

Then there are Dictionaries which tell us about a certain item.

car:
    wheels: 4
    doors: 4
    cup_holders: 22

We can also combine each of these data structures (Dictionaries, Lists, Arrays) to use together. We can have an array or list of dictionaries for example. Dictionaries are unordered. It doesn’t matter if I have another dictionary where the order of wheels and doors were reversed. I could still compare wheels or doors for both dictionaries.

Indentation is very important in YAML. You must have the same amount of spaces for each item in an array or in the dictionary or else the application will be confused as to what belongs to what. It wouldn’t make sense for doors to belong to wheels in the example shown above, but that’s what would happen if we had one extra space to the left of doors.

Commenting

If you’re familiar with coding at all, you’re probably aware of using a # to “comment out” something. This is often used for documentation purposes within a file or within code. As long as we put a # before something, the computer ignores it.

Great…How Do I Use It?

We know a little bit about how to set up a YAML file (file.yml) but what do we do with it? So, we can use a .yml file to store configurations to be used later. It’s a file that holds the actual configurations we want and we can re-use or modify and re-use multiple times. A .yml can be used as a configuration file along side your code. For example, you can include it as an imported file in a .py (Python file) or as a required file in Ruby. We can also use .yml files as a Playbook with Ansible. Which may give you a hint as to where I’ll be heading with this blog in the near future.

As I begin my new role in product marketing at Juniper, I’m actually finding myself getting back to being more technical. While I’ve dabbled with coding and orchestration tools for the last several years it’s now something I’m going to be concentrating on almost full time.

No ego here…if you see a mistake or something I could flesh out better, feel free to @Malhoit me.