Learn the YAML Basics
Jun 08, 2023 • 2 Minute Read
- Software Development
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 FileYAML files are made up of three core components: Mappings, lists, and scalars. Mappings are simple key-value pairs, like
ip: 10.0.4.0. 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 WhitespaceOutside 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.