- YAML Reference Card (yaml.org)
- Learn X in Y minutes (learnxinyminutes.com)
- YAML lint online (yamllint.com)
YAML Cheat Sheet
This is a quick reference cheat sheet for understanding and writing YAML format configuration files.
Also see
YAML Reference
Language Independent Scalar Types
{~, null} |
Null (no value). |
[1234, 0x4D2, 02333] |
[Decimal int, Hexadecimal int, Octal int] |
[1_230.15, 12.3015e+02] |
[Fixed float, Exponential float] |
[.inf, -.Inf, .NAN] |
[Infinity (float), Negative, Not a number] |
{Y, true, Yes, ON} |
Boolean true |
{n, FALSE, No, off} |
Boolean false |
More types
!!set |
{cherries, plums, apples} |
!!omap |
[one: 1, two: 2] |
Core types (default automatic tags)
!!map |
{Hash table, dictionary, mapping} |
!!seq |
{List, array, tuple, vector, sequence} |
!!str |
Unicode string |
Misc indicators
# |
Throwaway comment indicator |
`@ |
Both reserved for future use |
Tag Property (usually unspecified)
none |
Unspecified tag (automatically resolved by application) |
! |
Non-specific tag (by default, !!map /!!seq /!!str ) |
!foo |
Primary (by convention, means a local !foo tag) |
!!foo |
Secondary (by convention, means tag:yaml.org,2002:foo ) |
!h!foo |
Requires %TAG !h! <prefix> (and then means <prefix>foo ) |
!<foo> |
Verbatim tag (always means foo ) |
Scalar indicators
'' |
Surround in-line unescaped scalar |
" |
Surround in-line escaped scalar |
| |
Block scalar indicator |
> |
Folded scalar indicator |
- |
Strip chomp modifier (|- or >- ) |
+ |
Keep chomp modifier (|+ or >+ ) |
1-9 |
Explicit indentation modifier (|1 or >2 ). Modifiers can be combined (|2- , >+1 ) |
Special keys
= |
Default "value" mapping key |
<< |
Merge keys from another mapping |
Alias indicators
& |
Anchor property |
* |
Alias indicator |
Collection indicators
? |
Key indicator |
: |
Value indicator |
- |
Nested series entry indicator |
, |
Separate in-line branch entries |
[] |
Surround in-line series branch |
{} |
Surround in-line keyed branch |
Document indicators
% |
Directive indicator |
--- |
Document header |
... |
Document terminator |
YAML Collections
Ordered Mappings
ordered: !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58
#â Equivalent JSON
{
"ordered": [
{"Mark McGwire": 65},
{"Sammy Sosa": 63},
{"Ken Griffy": 58}
]
}
Unordered Sets
set1: !!set
? one
? two
set2: !!set {'one', "two"}
#â Equivalent JSON
{
"set1": {"one": null, "two": null},
"set2": {"one": null, "two": null}
}
Sets are represented as a Mapping where each key is associated with a null value
Nested Collections
Jack:
id: 1
name: Franc
salary: 25000
hobby:
- a
- b
location: {country: "A", city: "A-A"}
#â Equivalent JSON
{
"Jack": {
"id": 1,
"name": "Franc",
"salary": 25000,
"hobby": ["a", "b"],
"location": {
"country": "A", "city": "A-A"
}
}
}
Mapping of Mappings
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
hr: 63,
avg: 0.288
}
#â Equivalent JSON
{
"Mark McGwire": {
"hr": 65,
"avg": 0.278
},
"Sammy Sosa": {
"hr": 63,
"avg": 0.288
}
}
Sequence of Sequences
my\_sequences:
- [1, 2, 3]
- [4, 5, 6]
-
- 7
- 8
- 9
- 0
#â Equivalent JSON
{
"my\_sequences": [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9, 0]
]
}
Sequence of Mappings
children:
- name: Jimmy Smith
age: 15
- name: Jimmy Smith
age: 15
-
name: Sammy Sosa
age: 12
#â Equivalent JSON
{
"children": [
{"name": "Jimmy Smith", "age": 15},
{"name": "Jimmy Smith", "age": 15},
{"name": "Sammy Sosa", "age": 12}
]
}
Mapping to Sequences
attributes:
- a1
- a2
methods: [getter, setter]
#â Equivalent JSON
{
"attributes": ["a1", "a2"],
"methods": ["getter", "setter"]
}
Getting Started
Two Documents
---
document: this is doc 1
---
document: this is doc 2
YAML uses ---
to separate directives from document content.
Inheritance
parent: &defaults
a: 2
b: 3
child:
<<: \*defaults
b: 4
#â Equivalent JSON
{
"parent": {
"a": 2,
"b": 3
},
"child": {
"a": 2,
"b": 4
}
}
Comments
# A single line comment example
# block level comment example
# comment line 1
# comment line 2
# comment line 3
Scalar types
n1: 1 # integer
n2: 1.234 # float
s1: 'abc' # string
s2: "abc" # string
s3: abc # string
b: false # boolean type
d: 2015-04-05 # date type
#â Equivalent JSON
{
"n1": 1,
"n2": 1.234,
"s1": "abc",
"s2": "abc",
"s3": "abc",
"b": false,
"d": "2015-04-05"
}
Use spaces to indent. There must be space between the element parts.
Introduction
YAML is a data serialisation language designed to be directly writable and readable by humans
- YAML does not allow the use of tabs
- Must be space between the element parts
- YAML is CASE sensitive
- End your YAML file with the
.yaml
or.yml
extension - YAML is a superset of JSON
- Ansible playbooks are YAML files