피드 구독

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.


저자 소개

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

채널별 검색

automation icon

오토메이션

기술, 팀, 인프라를 위한 IT 자동화 최신 동향

AI icon

인공지능

고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트

open hybrid cloud icon

오픈 하이브리드 클라우드

하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요

security icon

보안

환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보

edge icon

엣지 컴퓨팅

엣지에서의 운영을 단순화하는 플랫폼 업데이트

Infrastructure icon

인프라

세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보

application development icon

애플리케이션

복잡한 애플리케이션에 대한 솔루션 더 보기

Original series icon

오리지널 쇼

엔터프라이즈 기술 분야의 제작자와 리더가 전하는 흥미로운 스토리