Iscriviti al 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.


Sull'autore

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

Ricerca per canale

automation icon

Automazione

Novità sull'automazione IT di tecnologie, team e ambienti

AI icon

Intelligenza artificiale

Aggiornamenti sulle piattaforme che consentono alle aziende di eseguire carichi di lavoro IA ovunque

open hybrid cloud icon

Hybrid cloud open source

Scopri come affrontare il futuro in modo più agile grazie al cloud ibrido

security icon

Sicurezza

Le ultime novità sulle nostre soluzioni per ridurre i rischi nelle tecnologie e negli ambienti

edge icon

Edge computing

Aggiornamenti sulle piattaforme che semplificano l'operatività edge

Infrastructure icon

Infrastruttura

Le ultime novità sulla piattaforma Linux aziendale leader a livello mondiale

application development icon

Applicazioni

Approfondimenti sulle nostre soluzioni alle sfide applicative più difficili

Original series icon

Serie originali

Raccontiamo le interessanti storie di leader e creatori di tecnologie pensate per le aziende