Getting Started
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 {.marker-round}
Scalar types
↓ Equivalent JSON
Use spaces to indent. There must be space between the element parts.
Variables
↓ Equivalent JSON
Comments
Multiline strings
↓ Equivalent JSON
Inheritance
↓ Equivalent JSON
Reference
↓ Equivalent JSON
Folded strings
↓ Equivalent JSON
Two Documents
YAML uses --- to separate directives from document content.
YAML Collections
Sequence
↓ Equivalent JSON
Mapping
↓ Equivalent JSON
Mapping to Sequences
↓ Equivalent JSON
Sequence of Mappings
↓ Equivalent JSON
Sequence of Sequences
↓ Equivalent JSON
Mapping of Mappings
↓ Equivalent JSON
Nested Collections
↓ Equivalent JSON
Unordered Sets
↓ Equivalent JSON
Sets are represented as a Mapping where each key is associated with a null value
Ordered Mappings
↓ Equivalent JSON
YAML Reference
Terms
- Sequences aka arrays or lists
- Scalars aka strings or numbers
- Mappings aka hashes or dictionaries {.marker-round}
Based on the YAML.org refcard.
Document indicators
|
|
% |
Directive indicator |
--- |
Document header |
... |
Document terminator |
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 |
Alias indicators
|
|
& |
Anchor property |
* |
Alias indicator |
Special keys
|
|
= |
Default “value” mapping key |
<< |
Merge keys from another mapping |
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 (` |
1or>2). <br/> Modifiers can be combined ( |
2-, >+1`) |
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) |
Misc indicators
|
|
# |
Throwaway comment indicator |
`@ |
Both reserved for future use |
Core types (default automatic tags)
|
|
!!map |
{Hash table, dictionary, mapping} |
!!seq |
{List, array, tuple, vector, sequence} |
!!str |
Unicode string |
Escape Codes
Numeric
\x12 (8-bit)
\u1234 (16-bit)
\U00102030 (32-bit)
{.cols-2 .marker-none}
Protective
\\ (\)
\" (")
\ ( )
\<TAB> (TAB)
{.cols-3 .marker-none}
C
\0 (NUL)
\a (BEL)
\b (BS)
\f (FF)
\n (LF)
\r (CR)
\t (TAB)
\v (VTAB)
{.cols-3 .marker-none}
Additional
\e (ESC)
\_ (NBSP)
\N (NEL)
\L (LS)
\P (PS)
{.cols-3 .marker-none}
More types
|
|
!!set |
{cherries, plums, apples} |
!!omap |
[one: 1, two: 2] |
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 |
Also see