Click any item to expand the explanation and examples.
📝 Basic Syntax
Key-value pairs basics
name: Alice age: 30 email: alice@example.comRules: space after colon, consistent indentation, no tabs.Nested (use 2-space indent — NEVER tabs)
address: street: 123 Main St city: Brussels country: Belgium
Lists (arrays) basics
# Block style fruits: - apple - banana - cherryInline style
fruits: [apple, banana, cherry]
List of objects
users:
- name: Alice role: admin
- name: Bob role: user
Inline objects and lists basics
# Inline object (flow style)
person: {name: Alice, age: 30}
Inline list
colors: [red, green, blue]
Mixed
servers:
- {host: web1, port: 80}
- {host: web2, port: 80}
📊 Data Types
Strings, numbers, booleans, null types
# Strings (quotes optional for simple strings) name: Alice name: "Alice" name: 'Alice'Numbers
count: 42 price: 19.99 hex: 0xFF octal: 0o77 scientific: 1.2e+5
Booleans
active: true active: false
Also: yes/no, on/off (YAML 1.1 — avoid these!)
Null
value: null value: ~ value: # Empty value = null
⚠️ The "Norway problem" and other gotchas gotcha
# ❌ These are NOT strings — they're booleans! country: NO # false (Norway!) answer: yes # true flag: on # true flag: off # falseRule of thumb: when in doubt, quote it.✅ Quote them
country: “NO” answer: “yes”
❌ These are NOT strings — they’re numbers!
version: 1.0 # float 1.0 zipcode: 01onal # octal in some parsers
✅ Quote them
version: “1.0” zipcode: “01onal”
❌ Colon in value breaks things
message: Error: file not found # YAML error!
✅ Quote it
message: “Error: file not found”
📄 Multiline Strings
| (literal) vs > (folded) multiline
# | = literal block (preserves newlines) description: | This is line one. This is line two.This is after a blank line.
Result: “This is line one.\nThis is line two.\n\nThis is after a blank line.\n”
> = folded block (newlines become spaces)
description: > This is a long paragraph that will be joined into one line.
Result: “This is a long paragraph that will be joined into one line.\n”
Chomp indicators (trailing newline control)
text: | # Keep trailing newline (default) text: |- # Strip trailing newline text: |+ # Keep all trailing newlines
Most common: |- for clean strings without trailing newline
script: |- #!/bin/bash echo “Hello” echo “World”
🔗 Anchors & Aliases (DRY)
&anchor and *alias advanced
# Define with & (anchor), reference with * (alias) defaults: &defaults timeout: 30 retries: 3 region: eu-west-1production: <<: *defaults # Merge all defaults timeout: 60 # Override one value
staging: <<: *defaults region: us-east-1
Simple value reuse
db_host: &db_host “db.example.com”
services: api: database: *db_host worker: database: *db_host
<<: is the merge key — it merges the anchored mapping into the current one.
📑 Multiple Documents
--- document separator advanced
# One file, multiple documents (common in Kubernetes) --- apiVersion: v1 kind: Service metadata: name: web --- apiVersion: apps/v1 kind: Deployment metadata: name: web
--- starts a new document. ... optionally ends one.
🔍 Comments
# comments basics
# Full line comment name: Alice # Inline commentYAML only has single-line comments
There is no multi-line comment syntax
Just use multiple # lines
⚡ Common Patterns
Docker Compose pattern
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
GitHub Actions pattern
name: CI
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
Validate your YAML tips
# Python
python -c "import yaml; yaml.safe_load(open('file.yaml'))"
Node.js
node -e “const y=require(‘yaml’);console.log(y.parse(require(‘fs’).readFileSync(‘file.yaml’,‘utf8’)))“
Online: yamllint.com
Install yamllint
pip install yamllint
yamllint file.yaml