mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
5.2 KiB
ReStructuredText
169 lines
5.2 KiB
ReStructuredText
13 years ago
|
YAML Syntax
|
||
|
===========
|
||
|
|
||
|
This page provides a basic overview of correct YAML syntax, which is how Ansible
|
||
|
playbooks (our configuration management language) are expressed.
|
||
|
|
||
12 years ago
|
We use YAML because it is easier for humans to read and write than other common
|
||
|
data formats like XML or JSON. Further, there are libraries available in most
|
||
|
programming languages for working with YAML.
|
||
13 years ago
|
|
||
13 years ago
|
You may also wish to read :doc:`playbooks` at the same time to see how this
|
||
13 years ago
|
is used in practice.
|
||
|
|
||
|
|
||
|
YAML Basics
|
||
|
-----------
|
||
|
|
||
12 years ago
|
For Ansible, nearly every YAML file starts with a list.
|
||
13 years ago
|
Each item in the list is a list of key/value pairs, commonly
|
||
13 years ago
|
called a "hash" or a "dictionary". So, we need to know how
|
||
|
to write lists and dictionaries in YAML.
|
||
|
|
||
9 years ago
|
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.
|
||
13 years ago
|
|
||
9 years ago
|
All members of a list are lines beginning at the same indentation level starting with a ``"- "`` (a dash and a space)::
|
||
13 years ago
|
|
||
|
---
|
||
|
# A list of tasty fruits
|
||
9 years ago
|
fruits:
|
||
|
- Apple
|
||
|
- Orange
|
||
|
- Strawberry
|
||
|
- Mango
|
||
|
...
|
||
13 years ago
|
|
||
10 years ago
|
A dictionary is represented in a simple ``key: value`` form (the colon must be followed by a space)::
|
||
13 years ago
|
|
||
|
# An employee record
|
||
9 years ago
|
martin:
|
||
9 years ago
|
name: Martin D'vloper
|
||
|
job: Developer
|
||
|
skill: Elite
|
||
13 years ago
|
|
||
9 years ago
|
More complicated data structures are possible, such as lists of dictionaries, dictionaries whose values are lists or a mix of both::
|
||
9 years ago
|
|
||
|
# Employee records
|
||
|
- martin:
|
||
|
name: Martin D'vloper
|
||
|
job: Developer
|
||
|
skills:
|
||
|
- python
|
||
|
- perl
|
||
|
- pascal
|
||
|
- tabitha:
|
||
|
name: Tabitha Bitumen
|
||
|
job: Developer
|
||
|
skills:
|
||
|
- lisp
|
||
|
- fortran
|
||
|
- erlang
|
||
|
|
||
9 years ago
|
Dictionaries and lists can also be represented in an abbreviated form if you really want to::
|
||
13 years ago
|
|
||
|
---
|
||
9 years ago
|
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
|
||
9 years ago
|
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']
|
||
13 years ago
|
|
||
|
.. _truthiness:
|
||
|
|
||
9 years ago
|
Ansible doesn't really use these too much, but you can also specify a boolean value (true/false) in several forms::
|
||
13 years ago
|
|
||
12 years ago
|
create_key: yes
|
||
|
needs_agent: no
|
||
13 years ago
|
knows_oop: True
|
||
|
likes_emacs: TRUE
|
||
|
uses_cvs: false
|
||
|
|
||
9 years ago
|
Values can span multiple lines using ``|`` or ``>``. Spanning multiple lines using a ``|`` will include the newlines. Using a ``>`` will ignore newlines; it's used to make what would otherwise be a very long line easier to read and edit.
|
||
9 years ago
|
In either case the indentation will be ignored.
|
||
|
Examples are::
|
||
9 years ago
|
|
||
|
include_newlines: |
|
||
|
exactly as you see
|
||
|
will appear these three
|
||
|
lines of poetry
|
||
9 years ago
|
|
||
9 years ago
|
ignore_newlines: >
|
||
|
this is really a
|
||
|
single line of text
|
||
|
despite appearances
|
||
|
|
||
9 years ago
|
|
||
|
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::
|
||
13 years ago
|
|
||
|
---
|
||
|
# An employee record
|
||
9 years ago
|
name: Martin D'vloper
|
||
13 years ago
|
job: Developer
|
||
|
skill: Elite
|
||
|
employed: True
|
||
|
foods:
|
||
|
- Apple
|
||
|
- Orange
|
||
|
- Strawberry
|
||
|
- Mango
|
||
|
languages:
|
||
9 years ago
|
perl: Elite
|
||
12 years ago
|
python: Elite
|
||
9 years ago
|
pascal: Lame
|
||
|
education: |
|
||
|
4 GCSEs
|
||
|
3 A-Levels
|
||
|
BSc in the Internet of Things
|
||
13 years ago
|
|
||
9 years ago
|
That's all you really need to know about YAML to start writing `Ansible` playbooks.
|
||
13 years ago
|
|
||
12 years ago
|
Gotchas
|
||
|
-------
|
||
|
|
||
10 years ago
|
While YAML is generally friendly, the following is going to result in a YAML syntax error::
|
||
12 years ago
|
|
||
|
foo: somebody said I should put a colon here: so I did
|
||
|
|
||
10 years ago
|
You will want to quote any hash values using colons, like so::
|
||
12 years ago
|
|
||
|
foo: "somebody said I should put a colon here: so I did"
|
||
|
|
||
|
And then the colon will be preserved.
|
||
|
|
||
12 years ago
|
Further, Ansible uses "{{ var }}" for variables. If a value after a colon starts
|
||
12 years ago
|
with a "{", YAML will think it is a dictionary, so you must quote it, like so::
|
||
12 years ago
|
|
||
|
foo: "{{ variable }}"
|
||
|
|
||
8 years ago
|
The same applies for strings that start or contain any YAML special characters ``[] {} : > |`` .
|
||
9 years ago
|
|
||
|
Boolean conversion is helpful, but this can be a problem when you want a literal `yes` or other boolean values as a string.
|
||
|
In these cases just use quotes::
|
||
|
|
||
|
non_boolean: "yes"
|
||
|
other_string: "False"
|
||
|
|
||
12 years ago
|
|
||
8 years ago
|
YAML converts certain strings into floating-point values, such as the string
|
||
|
`1.0`. If you need to specify a version number (in a requirements.yml file, for
|
||
|
example), you will need to quote the value if it looks like a floating-point
|
||
|
value::
|
||
|
|
||
|
version: "1.0"
|
||
|
|
||
|
|
||
13 years ago
|
.. seealso::
|
||
|
|
||
|
:doc:`playbooks`
|
||
|
Learn what playbooks can do and how to write/run them.
|
||
|
`YAMLLint <http://yamllint.com/>`_
|
||
|
YAML Lint (online) helps you debug YAML syntax if you are having problems
|
||
10 years ago
|
`Github examples directory <https://github.com/ansible/ansible-examples>`_
|
||
13 years ago
|
Complete playbook files from the github project source
|
||
9 years ago
|
`Wikipedia YAML syntax reference <https://en.wikipedia.org/wiki/YAML>`_
|
||
|
A good guide to YAML syntax
|
||
13 years ago
|
`Mailing List <http://groups.google.com/group/ansible-project>`_
|
||
|
Questions? Help? Ideas? Stop by the list on Google Groups
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|
||
13 years ago
|
|