YAML in 15 minutes
Docker Compose files, Kubernetes manifests, GitHub Actions workflows, Ansible playbooks, Helm values: they're all YAML. The format is simple, but one wrong space breaks it, and a few of its rules surprise everyone once. Learn them here and the rest of DevOps gets easier to read.
Keys and values
YAML describes data with key: value pairs. The space after the colon is required.
name: web
replicas: 3
debug: false
image: nginx:1.27 # a colon inside a value is fine when it's not followed by a space
Indentation is the structure
Nesting is shown by indentation, the same idea as Python. Use spaces only, never tabs, and be consistent; two spaces per level is the convention almost everywhere.
database:
host: db.internal
port: 5432
credentials:
user: app
Here port belongs to database, and user belongs to credentials. Move port two spaces left and it becomes a top-level key instead; nothing errors, the meaning just silently changes.
Lists
A dash and a space start each list item. Items can be simple values or whole maps, which is exactly how Kubernetes lists containers and how Actions lists steps:
ports:
- 80
- 443
containers:
- name: web # one item: a map with two keys
image: nginx:1.27
- name: sidecar # the next item
image: busybox:1.36
There's also a compact "flow" style taken from JSON: ports: [80, 443] and labels: {app: web, tier: front}. Use it for short things only.
Strings, and when to quote them
Most text needs no quotes. Single quotes keep everything exactly as written; double quotes allow escapes like \n. Quote a value when YAML might read it as something else:
- Numbers that are really text:
version: 3.10is read as the number 3.1. Write"3.10". This is the classic bug inpython-versionin GitHub Actions. - yes, no, on, off: some tools read these as true and false. Quote them when you mean the word.
- Leading zeros and colons:
mode: 0755can be read as an octal number, and older parsers turn22:22into a base-60 number. Quote file modes and port mappings like"8080:80". - Values starting with special characters such as
*,&,!,{,[,@,`or#. - Kubernetes environment variables must be strings:
value: "8080", notvalue: 8080.
Multi-line text
Scripts inside CI files are usually written as block text:
run: | # | keeps every line break: perfect for shell commands
pip install -r requirements.txt
pytest -q
description: > # > folds lines into one paragraph
This text is written over
several lines but becomes one.
Adding a minus (|- or >-) drops the final newline.
Comments, several documents and reuse
#starts a comment, anywhere outside quotes.---separates several documents in one file. Kubernetes uses it to put a Deployment and its Service in the same file.- Anchors and aliases reuse a block:
&namelabels it,*namerepeats it, and<<: *namemerges a map into another. Docker Compose (withx-fields) and GitLab CI use this a lot:
x-common: &common
restart: unless-stopped
logging:
options: {max-size: "10m"}
services:
web:
<<: *common
image: ghcr.io/you/web:1.0
worker:
<<: *common
image: ghcr.io/you/worker:1.0
YAML and JSON
Modern YAML is a superset of JSON, so any JSON document is also valid YAML. That's why Kubernetes accepts both, and why Python chapter 8 can load either into the same dicts and lists.
Check before you apply
$ pip install yamllint && yamllint compose.yaml # style and syntax
$ python3 -c 'import sys, yaml; yaml.safe_load(open(sys.argv[1]))' file.yaml
$ docker compose config # Compose: shows the final, merged file
$ kubectl apply --dry-run=client -f web.yaml # Kubernetes: validates without changing anything
For GitHub Actions, the actionlint tool catches mistakes in workflow files before you push. VS Code's YAML extension underlines most errors as you type.
Common errors
mapping values are not allowed here: a missing space after a colon, or a colon followed by a space inside an unquoted value. Quote the value.found character '\t' that cannot start any token: a tab. Replace it with spaces, and set your editor to insert spaces.did not find expected keyorcould not find expected ':': inconsistent indentation, usually one line off by a space.cannot unmarshal number into ... of type stringfrom Kubernetes: a number where text is expected (environment values, annotations). Quote it.