As YAML is increasingly used alongside various agile languages and applications, learning the basics of this data serialization language will let you jump in and start working with a variety of tools, such as Ansible, Kubernetes, and Salt.
Luckily, YAML is easily human-readable and simple to pick up, so here’s a quick “YAML in five minutes” rundown:
Components of a YAML File
YAML files are made up of three core components: Mappings, lists, and scalars.
Mappings are simple key-value pairs, like
Lists work like any plain-text bulleted list, with each item on a new line and starting with a dash.
Finally, a scalar is something that is a string, boolean, or number; an item on a list is its own scalar, while both the
value of a key-value pair are individual scalars.
Mappings and lists can also be combined.
name: Dana Scully # this is a mappingusername: dscullyroles: #this is a mapping with a list in it - FBI Agent - Medical doctor
YAML and Whitespace
Outside of the bits and pieces that make up YAML, what we can’t see is also important. Namely, the spacing. Spacing in YAML is what signifies a “collection” or a group of related lines. For example, everything under
roles in the above example is a single collection.
Whitespace in YAML should be spaces. In many instances, it must be spaces. Indentation, especially, is limited to two single places, but whitespace also has to be used between the colon and the value in a key-value pair and after the dash in a listed item.
And That’s It!
That was the five-minute rundown of the basics of YAML. Yes, YAML does have more features that we talk about in the hands-on YAML Essentials course, but for configuration files, this is all the YAML you really need.
Dying to learn about anchors and tabs, block style, and flow? Then check out YAML Essentials for more!