Abonnez-vous au flux

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.


À propos de l'auteur

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

Parcourir par canal

automation icon

Automatisation

Les dernières nouveautés en matière d'automatisation informatique pour les technologies, les équipes et les environnements

AI icon

Intelligence artificielle

Actualité sur les plateformes qui permettent aux clients d'exécuter des charges de travail d'IA sur tout type d'environnement

open hybrid cloud icon

Cloud hybride ouvert

Découvrez comment créer un avenir flexible grâce au cloud hybride

security icon

Sécurité

Les dernières actualités sur la façon dont nous réduisons les risques dans tous les environnements et technologies

edge icon

Edge computing

Actualité sur les plateformes qui simplifient les opérations en périphérie

Infrastructure icon

Infrastructure

Les dernières nouveautés sur la plateforme Linux d'entreprise leader au monde

application development icon

Applications

À l’intérieur de nos solutions aux défis d’application les plus difficiles

Original series icon

Programmes originaux

Histoires passionnantes de créateurs et de leaders de technologies d'entreprise