YAML Cheat Sheet
YAML reference with scalars, sequences, mappings, anchors, and common configuration patterns for CI/CD and Kubernetes.
Basics
| Syntax | Description | Example |
|---|---|---|
| Key-value pair | name: Alice | |
| Comment (not included in data) | # This is a comment | |
| Document start marker | ---\
name: Alice | |
| Document end marker | name: Alice\
... | |
| Inline comment after value | port: 8080 # default port |
Scalars
| Syntax | Description | Example |
|---|---|---|
| Unquoted string | name: Alice | |
| Double-quoted (supports escapes) | message: "Hello\
World" | |
| Single-quoted (literal) | path: 'C:\Users\file' | |
| Integer | port: 8080 | |
| Floating-point number | version: 1.5 | |
| Boolean value | debug: true | |
| Null value | middleName: null | |
| Date (ISO 8601) | created: 2024-01-15 | |
| Datetime with timezone | updatedAt: 2024-01-15T10:30:00+02:00 | |
| Octal number | permissions: 0o755 | |
| Hexadecimal number | color: 0xFF5733 | |
| Special float values | infinity: .inf |
Multi-line
| Syntax | Description | Example |
|---|---|---|
| Literal block scalar (preserves newlines) | description: |\
Line 1\
Line 2 | |
| Folded block scalar (joins lines) | summary: >\
This is a long\
paragraph | |
| Keep trailing newlines | text: |+\
content\
\ | |
| Strip trailing newlines | text: |-\
content | |
| Fold and strip trailing newline | msg: >-\
Single line\
no trailing newline | |
| Explicit indentation indicator | code: |2\
indented content |
Sequences
| Syntax | Description | Example |
|---|---|---|
| List item (block style) | fruits:\
- apple\
- banana | |
| Inline list (flow style) | tags: [python, yaml, devops] | |
| List of objects | users:\
- name: Alice\
age: 30 | |
| Lists within lists | matrix:\
- [1, 2]\
- [3, 4] |
Mappings
| Syntax | Description | Example |
|---|---|---|
| Nested mapping (indented) | database:\
host: localhost\
port: 5432 | |
| Inline mapping (flow style) | point: {x: 1, y: 2} | |
| Multi-line key with ? | ? multi\
line key\
: value | |
| Empty mapping | metadata: {} | |
| Empty sequence | items: [] |
Anchors
| Syntax | Description | Example |
|---|---|---|
| Define an anchor (reusable value) | defaults: &defaults\
timeout: 30\
retries: 3 | |
| Reference an anchor (alias) | production:\
<<: *defaults\
timeout: 60 | |
| Merge key (merge anchor into mapping) | dev:\
<<: *defaults\
debug: true | |
| Override merged values | staging:\
<<: *defaults\
retries: 5 |
Tags
| Syntax | Description | Example |
|---|---|---|
| Force string type | zip: !!str 07101 | |
| Force integer type | count: !!int "42" | |
| Force float type | ratio: !!float "3.14" | |
| Force boolean type | flag: !!bool "true" | |
| Force null type | empty: !!null "" | |
| Base64 encoded binary | icon: !!binary |\
R0lGODlhAQABAI... |
Patterns
| Syntax | Description | Example |
|---|---|---|
| Environment variable reference | password: ${DB_PASSWORD} | |
| Service definition | services:\
web:\
image: nginx\
ports:\
- "80:80" | |
| Workflow definition | on: push\
jobs:\
build:\
runs-on: ubuntu-latest | |
| Resource definition | apiVersion: v1\
kind: Pod\
metadata:\
name: my-pod | |
| Multiple YAML docs in one file | ---\
doc1: data\
---\
doc2: data |
Frequently asked questions
YAML vs JSON - when should I use which?
YAML is better for human-written config files (more readable, supports comments, anchors). JSON is better for data exchange between systems (simpler, faster parsing, universal support). Most tools that accept YAML also accept JSON, since YAML is a superset of JSON.
Why does YAML interpret 'yes' and 'no' as booleans?
YAML 1.1 treats yes/no/on/off/true/false as booleans. This is a common gotcha. Always quote strings that might be boolean-like: 'yes', 'NO', 'on'. YAML 1.2 (used by modern parsers) reduced this problem but not all tools have upgraded.
Can I use tabs in YAML?
No. YAML strictly requires spaces for indentation. Tabs will cause parse errors. Configure your editor to insert spaces when you press Tab. Most YAML style guides use 2 spaces per level.
How do I include one YAML file in another?
Standard YAML doesn't support file includes. However, many tools add this: Docker Compose has 'extends', Kubernetes has Helm/Kustomize, and CI/CD tools have their own include syntax. For generic YAML, use anchors/aliases within a single file.
What's the difference between | and > for multi-line strings?
| (literal) preserves line breaks exactly as written - great for scripts, code, and formatted text. > (folded) joins consecutive lines with spaces - great for long paragraphs. Both strip trailing whitespace by default; add + or - to control trailing newlines.
How do I handle special characters in YAML?
Use double quotes for escape sequences: \ (newline), \ (tab), \\ (backslash). Use single quotes for literal strings (no escaping). For colons in values, quote the string: title: 'Note: important'. Unquoted values can't start with special chars like { [ & *.
Go from reference to real skills
Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.
Browse all courses