Some links in this article are affiliate links. We earn a commission at no extra cost to you when you purchase through them. Full disclosure.
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](/blog/docker-compose-validator/) 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](/blog/github-actions-linter/) 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
Quick access: Raycast lets you search commands, snippets, and cheat sheets instantly from your keyboard. Free for Mac.
Related: Kubernetes Cheat Sheet Β· Docker Cheat Sheet