What is YAML?

We look at the pros and cons of the language developed in 2001

Programming code abstract on a reflective background

The rapid rise in Kubernetes has paved the way for a new kind of programming language called YAML, which is used to format containerised files. 

YAML originally stood for 'Yet Another Markup Language", but it has been renamed to "YAML Ain't Markup Language" as a way to differentiate it from documentation languages, such as SGML and HTML, as YAML is designed for data configuration. 

The language is not as ubiquitous as JSON and does have uses beyond Kubernetes. It is known as a 'human-readable data-serialisation language' and was released in 2001. It has use cases in AI where it is used in tools for OpenStack and Ansible playbooks and more. 

It is a text-based format that is used to configure information or specific tasks for data serialisation - such as where you convert complex data into a flat-file format to store or transmit). Similar to XML, but also different in that it is easier to read and write for humans - this is what makes it great for projects like Ansible - and the entire YAML  website is also easy to read. 

How does YAML work?

This programming language borrows features and patterns from a host of others to simplify the process of reading and writing code.

You may use indentations and new lines to structure code so how the code is displayed on your monitor is how it would work, as in Python for example. You’ll be able to select the degrees of indentation you wish to adopt so you can choose whichever you find the most readable, so long as you maintain consistency. You cannot, however, use the tab character, which avoids a major issue that varying operating systems handle tabs differently - in addition to the ongoing spaces versus tabs debate.

Users may also adopt a more compact format where the two main data types, lists and associative arrays (also known as maps) are denoted by the [] and) {} figures. This makes it effectively a superset of JSON, although this is outlined for machines, not humans, to read. Incidentally, YAML also has features that are absent from JSON, including comments, which JSON hasn’t been created to support. There are, however, workarounds.

These data types may also be nested to represent more complicated structures based on those present in Perl. Features are lifted from C, HTML, MIME, as well as mail headers, with colons used to denote key: value pairs.

The space function is present so users won’t have to put quotation marks around strings and numbers. Simple types such as integers, floats and Booleans are detected by default, and there’s priced-in support for ISO-formatted dates and times, although you can also declare your own data types.

Structures let you store multiple documents in a single file or refer to content in one part of the document from elsewhere using an anchor (which also lets you duplicate or inherent properties).

That means it's much more flexible than JSON where the hierarchy is fixed, with each child node having only one parent node and while there's a similar option in XML the YAML parse automatically expands the references. That way you get a file that's easier to read and you avoid potential errors copying and pasting parameters where only a handful of things change between different instances, but external systems don't need to be told about the structure of the YAML file.

Lines of code

What are the benefits of YAML?

Because the formatting is straightforward and you don't have to worry about closing tags, brackets or quote marks, you can edit YAML in simple text editing tools, and subsections of YAML files are often valid YAML. But there are also plugins to add YAML support to common IDEs like Visual Studio Code and Atom; these can use the YAML Language Server provide autocomplete and Intellisense, and there are several YAML linters to check code for correctness.

You can't write YAML that validates itself the way XML documents can do, based on schema, but if you need to define a schema for your YAML there are languages that let you do that. The combination of YAML and JSON Schema can be powerful: VS Code, the DocFX static web site generator and even the schema for Microsoft's Q# Quantum Chemistry library use them together to achieve a more human-readable version of JSON.

Using YAML files has advantages over typing in command line options: you can create much more complex structures in YAML and you don't have to deal with long and unwieldy strings of parameters. And because they're files, you can check them into source control systems, track versions and changes. Because YAML treats lines as information, it works better with git-based systems for tracking changes than JSON. That makes it easier to treat configuration as code that you manage, test and consume the same way you do all your other code.

Are there any downsides to YAML?

Like all programming languages, YAML is not without fault. Because it was designed specifically to be easy to read and write mistakes are far more easier to make than with other languages. This also means that changes can be made to your YAML could by very simple typos, like adding an extra space, which are more difficult to spot as it is on a sparse, white background.

Since YAML is more readable than JSON or XML, reading through a YAML file will more likely lead to finding errors than other languages. With the kind of configuration you perform in YAML becoming more relevant to the adoption of DevOps, configurations you’re specifying may become more complex and may demand more expertise - regardless of language you’re writing them in. Arguably, there are better languages, such as TOML, but these haven’t been adopted as widely, so YAML is the language more developers will face.

Higher-grade tools will always be easier to work with versus reading and writing YAML files, and there’s a swelling choice of those for Kubernetes. This choice of tools ranges such as Helm, that streamlines installing and managing Kubernetes apps to managed cloud services, to the kubectl command line. Tools such as Pulumi, that use familiar programming languages like JavaScript or PowerShell, also fall into this camp. YAML is a configuration format, however, used in so many widely-used tool and projects that it’s worth familiarising yourself with it, and understanding its benefits and drawbacks.

Featured Resources

Choosing a collaboration platform

Eight questions every IT leader should ask

Download now

Performance benchmark: PostgreSQL/ MongoDB

Helping developers choose a database

Download now

Customer service vs. customer experience

Three-step guide to modern customer experience

Download now

Taking a proactive approach to cyber security

A complete guide to penetration testing

Download now

Most Popular

How to find RAM speed, size and type
Laptops

How to find RAM speed, size and type

8 Apr 2021
Microsoft is submerging servers in boiling liquid to prevent Teams outages
data centres

Microsoft is submerging servers in boiling liquid to prevent Teams outages

7 Apr 2021
REvil threatens to release Apple’s hardware schematics
ransomware

REvil threatens to release Apple’s hardware schematics

21 Apr 2021