YAML extensive cheatsheet
Here’s an extensive YAML (YAML Ain’t Markup Language) cheatsheet, covering basic syntax, advanced concepts, and usage tips:
Basic YAML Syntax
Key-Value Pair
1
key: value
Nested Objects
1 2
parent: child: value
Lists (Arrays)
1 2 3 4
items: - item1 - item2 - item3
Inline Lists
1
items: [item1, item2, item3]
Inline Dictionaries
1
person: {name: John, age: 30}
Comments
1
# This is a comment
Data Types
String
1
name: "John"
Multiline String
1 2 3
description: | This is a long multiline string.
or folded style:
1 2 3
description: > This is a long folded string.
Integer
1
age: 30
Float
1
price: 99.99
Boolean
1
active: true
Null
1
key: null
Advanced Syntax
- Anchors and Aliases
- Anchor:
&anchor_name
- Alias:
*anchor_name
1 2 3 4 5 6 7
default_settings: &defaults retries: 3 timeout: 60 user_settings: <<: *defaults timeout: 30 # Override the default timeout
- Anchor:
Merging Keys
1 2 3 4 5 6 7 8
defaults: &defaults setting1: default1 setting2: default2 custom: <<: *defaults setting2: custom2 # Overridden setting3: custom3
Multiple Documents in One File
1 2 3 4 5 6
--- document1: key1: value1 --- document2: key2: value2
Reference Reuse
1 2 3
values: - &id001 42 - *id001
Multiline Folded Block
1 2 3 4
description: > This is a multiline string with a folded style that will be rendered as a single line.
Multiline Literal Block
1 2 3 4
description: | This is a multiline string with literal style that will retain the newline characters.
Common YAML Patterns
Config Files
1 2 3 4 5
version: 1.0 environment: production server: port: 8080 host: localhost
Docker Compose Example
1 2 3 4 5 6 7 8 9 10 11
version: "3.8" services: web: image: nginx ports: - "8080:80" db: image: postgres environment: POSTGRES_USER: user POSTGRES_PASSWORD: password
Kubernetes Pod Configuration
1 2 3 4 5 6 7 8 9 10
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: nginx ports: - containerPort: 80
CI/CD Pipeline Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
stages: - build - test - deploy build_job: stage: build script: - make build test_job: stage: test script: - make test deploy_job: stage: deploy script: - make deploy
Special Characters
- Reserved Characters
:
- Indicates a key-value separator.-
- Indicates an item in a list.#
- Used for comments.&
- Used for anchors.*
- Used for aliases.|
- Indicates a literal block scalar (preserves newlines).>
- Indicates a folded block scalar (folds newlines).
Escaping Characters
1
text: "This is a string with a special character: \t (tab)"
YAML Best Practices
- Consistent Indentation
- Always use spaces for indentation, never tabs.
- YAML does not allow mixed spaces and tabs.
- Avoid Trailing Whitespace
- YAML is sensitive to trailing spaces, which may cause parsing errors.
- Keep It Simple
- Avoid overcomplicating YAML with deeply nested structures. Aim for readability.
- Use Anchors and Aliases to DRY (Don’t Repeat Yourself)
- Use anchors and aliases to reduce repetition.
- Avoid Boolean Strings
Words like
yes
,no
,on
,off
,true
,false
can be misinterpreted as booleans. It’s best to wrap them in quotes:1
setting: "yes"
Error Handling and Debugging
- Indentation Errors
- YAML is strict about indentation levels. Always use consistent indentation (usually 2 or 4 spaces).
- Invalid Characters
- Avoid using special characters in keys unless quoted:
1
"key-with-special:characters": value
- Common Parsing Errors
- Ensure no tabs are used for indentation; YAML only supports spaces.
- Check for colons
:
outside of keys.
This post is licensed under CC BY 4.0 by the author.