@ -26,7 +26,9 @@ to write lists and dictionaries in YAML.
There's another small quirk to YAML. All YAML files (regardless of their association with Ansible or not) can optionally
There's another small quirk to YAML. All YAML files (regardless of their association with Ansible or not) can optionally
begin with ``---`` and end with ``...``. This is part of the YAML format and indicates the start and end of a document.
begin with ``---`` and end with ``...``. This is part of the YAML format and indicates the start and end of a document.
All members of a list are lines beginning at the same indentation level starting with a ``"- "`` (a dash and a space)::
All members of a list are lines beginning at the same indentation level starting with a ``"- "`` (a dash and a space):
..code:: yaml
---
---
# A list of tasty fruits
# A list of tasty fruits
@ -36,7 +38,9 @@ All members of a list are lines beginning at the same indentation level starting
- Mango
- Mango
...
...
A dictionary is represented in a simple ``key: value`` form (the colon must be followed by a space)::
A dictionary is represented in a simple ``key: value`` form (the colon must be followed by a space):
..code:: yaml
# An employee record
# An employee record
martin:
martin:
@ -44,7 +48,9 @@ A dictionary is represented in a simple ``key: value`` form (the colon must be f
job: Developer
job: Developer
skill: Elite
skill: Elite
More complicated data structures are possible, such as lists of dictionaries, dictionaries whose values are lists or a mix of both::
More complicated data structures are possible, such as lists of dictionaries, dictionaries whose values are lists or a mix of both:
..code:: yaml
# Employee records
# Employee records
- martin:
- martin:
@ -62,7 +68,9 @@ More complicated data structures are possible, such as lists of dictionaries, di
- fortran
- fortran
- erlang
- erlang
Dictionaries and lists can also be represented in an abbreviated form if you really want to::
Dictionaries and lists can also be represented in an abbreviated form if you really want to:
..code:: yaml
---
---
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
@ -72,7 +80,9 @@ These are called "Flow collections".
.._truthiness:
.._truthiness:
Ansible doesn't really use these too much, but you can also specify a :ref:`boolean value <playbooks_variables>` (true/false) in several forms::
Ansible doesn't really use these too much, but you can also specify a :ref:`boolean value <playbooks_variables>` (true/false) in several forms:
..code:: yaml
create_key: true
create_key: true
needs_agent: false
needs_agent: false
@ -85,7 +95,9 @@ Use lowercase 'true' or 'false' for boolean values in dictionaries if you want t
Values can span multiple lines using ``|`` or ``>``. Spanning multiple lines using a "Literal Block Scalar" ``|`` will include the newlines and any trailing spaces.
Values can span multiple lines using ``|`` or ``>``. Spanning multiple lines using a "Literal Block Scalar" ``|`` will include the newlines and any trailing spaces.
Using a "Folded Block Scalar" ``>`` will fold newlines to spaces; it's used to make what would otherwise be a very long line easier to read and edit.
Using a "Folded Block Scalar" ``>`` will fold newlines to spaces; it's used to make what would otherwise be a very long line easier to read and edit.
In either case the indentation will be ignored.
In either case the indentation will be ignored.
Examples are::
Examples are:
..code:: yaml
include_newlines: |
include_newlines: |
exactly as you see
exactly as you see
@ -97,7 +109,9 @@ Examples are::
single line of text
single line of text
despite appearances
despite appearances
While in the above ``>`` example all newlines are folded into spaces, there are two ways to enforce a newline to be kept::
While in the above ``>`` example all newlines are folded into spaces, there are two ways to enforce a newline to be kept:
..code:: yaml
fold_some_newlines: >
fold_some_newlines: >
a
a
@ -108,12 +122,16 @@ While in the above ``>`` example all newlines are folded into spaces, there are
e
e
f
f
Alternatively, it can be enforced by including newline ``\n`` characters::
Alternatively, it can be enforced by including newline ``\n`` characters:
..code:: yaml
fold_same_newlines: "a b\nc d\n e\nf\n"
fold_same_newlines: "a b\nc d\n e\nf\n"
Let's combine what we learned so far in an arbitrary YAML example.
Let's combine what we learned so far in an arbitrary YAML example.
This really has nothing to do with Ansible, but will give you a feel for the format::
This really has nothing to do with Ansible, but will give you a feel for the format:
..code:: yaml
---
---
# An employee record
# An employee record
@ -144,17 +162,23 @@ While you can put just about anything into an unquoted scalar, there are some ex
A colon followed by a space (or newline) ``": "`` is an indicator for a mapping.
A colon followed by a space (or newline) ``": "`` is an indicator for a mapping.
A space followed by the pound sign ``" #"`` starts a comment.
A space followed by the pound sign ``" #"`` starts a comment.
Because of this, the following is going to result in a YAML syntax error::
Because of this, the following is going to result in a YAML syntax error:
..code:: text
foo: somebody said I should put a colon here: so I did
foo: somebody said I should put a colon here: so I did
windows_drive: c:
windows_drive: c:
...but this will work::
...but this will work:
..code:: yaml
windows_path: c:\windows
windows_path: c:\windows
You will want to quote hash values using colons followed by a space or the end of the line::
You will want to quote hash values using colons followed by a space or the end of the line:
..code:: yaml
foo: 'somebody said I should put a colon here: so I did'
foo: 'somebody said I should put a colon here: so I did'
@ -162,14 +186,18 @@ You will want to quote hash values using colons followed by a space or the end o
...and then the colon will be preserved.
...and then the colon will be preserved.
Alternatively, you can use double quotes::
Alternatively, you can use double quotes:
..code:: yaml
foo: "somebody said I should put a colon here: so I did"
foo: "somebody said I should put a colon here: so I did"
windows_drive: "c:"
windows_drive: "c:"
The difference between single quotes and double quotes is that in double quotes
The difference between single quotes and double quotes is that in double quotes
you can use escapes::
you can use escapes:
..code:: yaml
foo: "a \t TAB and a \n NEWLINE"
foo: "a \t TAB and a \n NEWLINE"
@ -183,17 +211,23 @@ The following is invalid YAML:
Further, Ansible uses "{{ var }}" for variables. If a value after a colon starts
Further, Ansible uses "{{ var }}" for variables. If a value after a colon starts
with a "{", YAML will think it is a dictionary, so you must quote it, like so::
with a "{", YAML will think it is a dictionary, so you must quote it, like so:
..code:: yaml
foo: "{{ variable }}"
foo: "{{ variable }}"
If your value starts with a quote the entire value must be quoted, not just part of it. Here are some additional examples of how to properly quote things::
If your value starts with a quote the entire value must be quoted, not just part of it. Here are some additional examples of how to properly quote things: