Inscreva-se no 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.


Sobre o autor

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

Navegue por canal

automation icon

Automação

Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes

AI icon

Inteligência artificial

Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente

open hybrid cloud icon

Nuvem híbrida aberta

Veja como construímos um futuro mais flexível com a nuvem híbrida

security icon

Segurança

Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias

edge icon

Edge computing

Saiba quais são as atualizações nas plataformas que simplificam as operações na borda

Infrastructure icon

Infraestrutura

Saiba o que há de mais recente na plataforma Linux empresarial líder mundial

application development icon

Aplicações

Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações

Original series icon

Programas originais

Veja as histórias divertidas de criadores e líderes em tecnologia empresarial