What is YAML?
YAML (YAML Ain't Markup Language) is a human-readable data serialization format commonly used for configuration files. It's the standard format for Kubernetes, Docker Compose, Ansible, and many CI/CD pipelines.
YAML Syntax Basics
YAML uses indentation to represent structure. Here are the key concepts:
- Key-value pairs use colons:
key: value - Indentation creates hierarchy (use spaces, not tabs)
- Lists use dashes:
- item - Comments start with
# - Strings usually don't need quotes
Basic YAML Example
# User configuration name: John Doe age: 30 email: john@example.com active: true address: city: New York country: USA zipcode: "10001" # Quoted to preserve string hobbies: - reading - gaming - hiking
YAML Data Types
Strings
# Plain strings (no quotes needed usually) name: John Doe # Quoted strings (for special characters) message: "Hello: World" path: 'C:\Users\john' # Multiline strings description: | This is a multiline string. Line breaks are preserved. folded: > This is a folded string. Line breaks become spaces.
Numbers and Booleans
# Numbers integer: 42 float: 3.14 scientific: 1.0e+12 hex: 0xFF # Booleans (many formats accepted) enabled: true disabled: false yes_value: yes no_value: no
Lists and Objects
# List (block style)
fruits:
- apple
- banana
- orange
# List (flow style - like JSON)
colors: [red, green, blue]
# Nested objects
database:
host: localhost
port: 5432
credentials:
username: admin
password: secretCommon YAML Use Cases
1. Kubernetes Manifests
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 802. Docker Compose
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:14
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:3. GitHub Actions
name: CI Pipeline
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm testCommon YAML Mistakes
Using tabs instead of spaces
YAML requires spaces for indentation. Tabs cause parsing errors.
Inconsistent indentation
Use the same number of spaces throughout (usually 2).
Forgetting to quote special strings
time: 12:30 # Interpreted as minutes!time: "12:30" # CorrectMissing space after colon
key:value # Wrongkey: value # CorrectYAML vs JSON vs TOML
| Feature | YAML | JSON | TOML |
|---|---|---|---|
| Comments | Yes | No | Yes |
| Human Readable | Excellent | Good | Good |
| Indentation-based | Yes | No | No |
| Primary Use | Config files | APIs, data | Config files |
Learn more about other formats: