2012-03-11 20:34:21 +01:00
|
|
|
YAML Syntax
|
|
|
|
===========
|
|
|
|
|
|
|
|
This page provides a basic overview of correct YAML syntax, which is how Ansible
|
|
|
|
playbooks (our configuration management language) are expressed.
|
|
|
|
|
2013-07-23 00:09:18 +02:00
|
|
|
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.
|
2012-03-11 20:34:21 +01:00
|
|
|
|
2012-03-17 22:16:31 +01:00
|
|
|
You may also wish to read :doc:`playbooks` at the same time to see how this
|
2012-03-11 20:34:21 +01:00
|
|
|
is used in practice.
|
|
|
|
|
|
|
|
|
|
|
|
YAML Basics
|
|
|
|
-----------
|
|
|
|
|
2013-07-23 00:09:18 +02:00
|
|
|
For Ansible, nearly every YAML file starts with a list.
|
2012-03-13 04:18:54 +01:00
|
|
|
Each item in the list is a list of key/value pairs, commonly
|
2012-03-11 20:34:21 +01:00
|
|
|
called a "hash" or a "dictionary". So, we need to know how
|
|
|
|
to write lists and dictionaries in YAML.
|
|
|
|
|
|
|
|
There's another small quirk to YAML. All YAML files (regardless of their association with
|
2013-07-23 00:09:18 +02:00
|
|
|
Ansible or not) should begin with ``---``. This is part of the YAML
|
|
|
|
format and indicates the start of a document.
|
2012-03-11 20:34:21 +01:00
|
|
|
|
|
|
|
All members of a list are lines beginning at the same indentation level starting
|
2014-12-16 21:25:41 +01:00
|
|
|
with a ``"- "`` (dash and whitespace) character::
|
2012-03-11 20:34:21 +01:00
|
|
|
|
|
|
|
---
|
|
|
|
# A list of tasty fruits
|
|
|
|
- Apple
|
|
|
|
- Orange
|
|
|
|
- Strawberry
|
|
|
|
- Mango
|
|
|
|
|
2014-12-15 08:55:12 +01:00
|
|
|
A dictionary is represented in a simple ``key: `` (colon and whitespace) and ``value`` form::
|
2012-03-11 20:34:21 +01:00
|
|
|
|
|
|
|
---
|
|
|
|
# An employee record
|
2012-10-17 01:03:54 +02:00
|
|
|
name: Example Developer
|
2012-03-11 20:34:21 +01:00
|
|
|
job: Developer
|
|
|
|
skill: Elite
|
|
|
|
|
|
|
|
Dictionaries can also be represented in an abbreviated form if you really want to::
|
|
|
|
|
|
|
|
---
|
|
|
|
# An employee record
|
2012-10-17 01:03:54 +02:00
|
|
|
{name: Example Developer, job: Developer, skill: Elite}
|
2012-03-11 20:34:21 +01:00
|
|
|
|
|
|
|
.. _truthiness:
|
|
|
|
|
|
|
|
Ansible doesn't really use these too much, but you can also specify a
|
|
|
|
boolean value (true/false) in several forms::
|
|
|
|
|
|
|
|
---
|
2012-12-14 11:56:53 +01:00
|
|
|
create_key: yes
|
|
|
|
needs_agent: no
|
2012-03-11 20:34:21 +01:00
|
|
|
knows_oop: True
|
|
|
|
likes_emacs: TRUE
|
|
|
|
uses_cvs: false
|
|
|
|
|
2013-07-23 00:09:18 +02:00
|
|
|
Let's combine what we learned so far in an arbitrary YAML example. This really
|
2012-03-11 20:34:21 +01:00
|
|
|
has nothing to do with Ansible, but will give you a feel for the format::
|
|
|
|
|
|
|
|
---
|
|
|
|
# An employee record
|
2012-10-17 01:03:54 +02:00
|
|
|
name: Example Developer
|
2012-03-11 20:34:21 +01:00
|
|
|
job: Developer
|
|
|
|
skill: Elite
|
|
|
|
employed: True
|
|
|
|
foods:
|
|
|
|
- Apple
|
|
|
|
- Orange
|
|
|
|
- Strawberry
|
|
|
|
- Mango
|
|
|
|
languages:
|
|
|
|
ruby: Elite
|
2013-07-23 00:09:18 +02:00
|
|
|
python: Elite
|
|
|
|
dotnet: Lame
|
2012-03-11 20:34:21 +01:00
|
|
|
|
2013-07-23 00:09:18 +02:00
|
|
|
That's all you really need to know about YAML to start writing
|
2012-03-11 20:34:21 +01:00
|
|
|
`Ansible` playbooks.
|
|
|
|
|
2012-10-17 01:03:54 +02:00
|
|
|
Gotchas
|
|
|
|
-------
|
|
|
|
|
2013-07-23 00:09:18 +02:00
|
|
|
While YAML is generally friendly, the following is going to result in a YAML syntax error:
|
2012-10-17 01:03:54 +02:00
|
|
|
|
|
|
|
foo: somebody said I should put a colon here: so I did
|
|
|
|
|
|
|
|
You will want to quote any hash values using colons, like so:
|
|
|
|
|
|
|
|
foo: "somebody said I should put a colon here: so I did"
|
|
|
|
|
|
|
|
And then the colon will be preserved.
|
|
|
|
|
2013-06-10 03:35:51 +02:00
|
|
|
Further, Ansible uses "{{ var }}" for variables. If a value after a colon starts
|
2013-07-23 00:09:18 +02:00
|
|
|
with a "{", YAML will think it is a dictionary, so you must quote it, like so::
|
2013-06-10 03:35:51 +02:00
|
|
|
|
|
|
|
foo: "{{ variable }}"
|
|
|
|
|
|
|
|
|
2012-03-11 20:34:21 +01:00
|
|
|
.. 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
|
2012-08-07 04:00:50 +02:00
|
|
|
`Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks>`_
|
2012-03-31 15:29:31 +02:00
|
|
|
Complete playbook files from the github project source
|
2012-03-31 15:55:37 +02:00
|
|
|
`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
|
2012-03-11 20:34:21 +01:00
|
|
|
|