Subscribe to the feed

The language of Red Hat OpenShift, containers, Red Hat Ansible Automation Platform, and many more modern tools is YAML. Related to the equally popular JSON format, YAML is a clean and minimalist configuration language designed with a mere two data structures. It can look cryptic at first, but it's easy to understand once you know the data types and structure of a YAML file. This article explains how YAML works, and shows you how to write your own YAML and what tool you can use to verify its validity.

There are basically only two data structures in YAML: the sequence and the mapping.

What is a YAML sequence?

A sequence is a string of values listed in a specific order. A YAML sequence always starts with a dash and a space (- ), followed by a value.

This is a valid YAML file containing just a sequence:

---
- sysadmin
- developer
- engineer

What is a YAML mapping?

A YAML mapping is a key and value pair. Each key must occur only once within a mapping, and a mapping is unordered.

This is a valid YAML file containing just mappings:

---
server: "bare metal"
cloud: "cluster"
edge: "embedded"

Most YAML files contain a mixture of these two YAML data types. However, there are a few rules to keep in mind when composing or modifying real-world YAML.

Combined data in YAML

One YAML data type can contain the other. You can put a sequence into a mapping by using a sequence as the value of a single mapping. 

When you do, you indent the sequence one level (2 spaces):

---
infrastructure:
  - server
  - cloud
  - edge
connectors:
  - ethernet
  - wifi
  - sneakers

You can also put a mapping into a sequence:

---
- server: "bare metal"
- cloud: "cluster"
- edge: "embedded"

Additionally, you can put a sequence into a sequence. When you want to embed one sequence into another sequence, indent the child element one level (2 spaces):

---
- sysadmin
  - server
  - cloud
- developer
- engineer

And a mapping can be embedded into a mapping, but remember that a mapping is unordered. Depending on the data you're trying to preserve, order may or may not matter.

---
infrastructure:         # mapping key
  server: "bare metal"  # mapping value...
  cloud: "cluster"      # ...continued

To create an ordered list of mappings, embed a mapping into a sequence, and then the sequence into a mapping:

---
infrastructure:           # mapping key
  - server: "bare metal"  # this is a sequence
  - cloud: "cluster"      # with three mapping entities
  - edge: "embedded"

How to use yamllint

The application yamllint checks YAML files for syntax errors and gives useful feedback when something is wrong or non-optimal. You can install yamllint using Python:

$ python -m pip install yamllint --user

Once it's installed, run yamllint along with the file you want to verify:

$ yamllint bad.yaml
bad.yaml
  3:1  error  wrong indentation: expected 2 but found 0  (indentation)
  5:1  error  too many blank lines (1 > 0)  (empty-lines)

Once you fix your YAML, yamllint responds with silence:

$ yamllint good.yaml
$

Use YAML for your data

YAML files can contain an ordered list or an unordered index. It’s a simple configuration language, and yet the two data types can be combined and embedded for a robust representation of relational data. You will see a lot of YAML once you start administering Red Hat OpenShift Service on AWS (ROSA) clusters, for everything from container files to service and security context definitions. You now know how to read YAML files and understand what they're expressing to your toolchain.


About the author

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

Browse by channel

automation icon

Automation

The latest on IT automation for tech, teams, and environments

AI icon

Artificial intelligence

Updates on the platforms that free customers to run AI workloads anywhere

open hybrid cloud icon

Open hybrid cloud

Explore how we build a more flexible future with hybrid cloud

security icon

Security

The latest on how we reduce risks across environments and technologies

edge icon

Edge computing

Updates on the platforms that simplify operations at the edge

Infrastructure icon

Infrastructure

The latest on the world’s leading enterprise Linux platform

application development icon

Applications

Inside our solutions to the toughest application challenges

Original series icon

Original shows

Entertaining stories from the makers and leaders in enterprise tech