Why You Should Use YAML as YAML with Ansible


21 Apr

As a software engineer I require a consistent style in any configuration or code. Static analysis and style checking are very important for project health. These procedures have a place in any project.

With Ansible, parameters can be written as fully expanded YAML dictionaries or as condensed key=value pairs. It is left up to the user to decide between the styles. Working on playbooks, roles, or tasks that use both styles can be tedious compared to just using expanded YAML dictionaries.

Something that I have recently decided on is to use YAML as YAML with Ansible. I use fully expanded YAML dictionaries everywhere now. My recommendation is that you try it, too.

Here is a task written with the two different styles. The general idea is from a real role except for the fact that a floppy disk is being mounted in the example. I have no idea why this would be needed.

The dictionary parameters are easier to read. Of course, this is clearly a biased example. Let’s look at a task with only a few parameters and a loop:

It’s easier to skim over the dictionary parameters vertically, so they still win for me. Now let’s look at a ridiculously simple task:

It’s not clear to me if the condensed parameters buy us anything here, except a lack of syntax highlighting.

Now that I have shown several example tasks using each style…

Try out using pure YAML and see if your productivity increases. Mine did as soon as I didn’t have to scan through dense playbooks to find a specific parameter. Let me know what you think.

Tyler Cross
Tyler Cross

Tyler Cross is a passionate software engineer from eastern North Carolina with a breadth of knowledge to draw on from the web, bioinformatics, and telecommunications. So far during his 2 years at Bandwidth he has worked on teams that gave him exposure to all technical aspects of our products- backend provisioning systems, real-time communications software, networking, and prototype development in the Bandwidth Incubator. In addition to being a techie, he is an avid runner and athlete in general. He is proud to be a Bandwidth employee and live up to all our principles and ideals.

  • Eric Izoita
    Posted at 23:26h, 04 September Reply

    Absolutely. I much prefer using the expanded form whenever I can.

  • Strahinja Kustudić
    Posted at 19:46h, 04 December Reply

    Lack of proper syntax highlighting for Ansible YAML format is probably one of the biggest reasons to use pure YAML. I always liked using YAML with modules with a lot of parameters, it just makes them easier to read, but I have to say looking at your examples, modules with a small number of parameters look also nice and easy to read.

  • Eduardo Gonzalez
    Posted at 22:58h, 13 December Reply

    I can’t agree more with you. When i started to learn ansible, most of the examples around the web were condensed i a single line. That was really hard to understand what the code supossed to do. I decided to simply my playbooks using pure parameticed yaml.
    I agree with the people who says that that makes large task, but is easier to read a large vertically task than the horizontally ones.

    PS Good post

Post A Comment