YAMLSyntax.rst 3.23 KB
Newer Older
1 2 3
YAML Syntax
===========

michael@ansibleworks.com committed
4 5
.. image:: http://ansible.cc/docs/_static/ansible_fest_2013.png
   :alt: ansiblefest 2013
6
   :target: http://ansibleworks.com/fest
michael@ansibleworks.com committed
7

8 9 10 11 12 13 14
This page provides a basic overview of correct YAML syntax, which is how Ansible
playbooks (our configuration management language) are expressed.  

We use YAML because it is easier to read and write for humans than other common 
data formats like XML or JSON.  Further, there are libraries available for reading
and writing YAML in most programming languages.  

Michael DeHaan committed
15
You may also wish to read :doc:`playbooks` at the same time to see how this
16 17 18 19 20 21
is used in practice.


YAML Basics
-----------

22 23
For `ansible`, nearly every YAML file starts with a list.   
Each item in the list is a list of key/value pairs, commonly
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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
`ansible` or not) should start with ``---``.  This is just a YAML
format thing that means "this is the start of a document".

All members of a list are lines beginning at the same indentation level starting
with a ``-`` (dash) character::

    ---
    # A list of tasty fruits
    - Apple
    - Orange
    - Strawberry
    - Mango

A dictionary is represented in a simple ``key:`` and ``value`` form::

    ---
    # An employee record
Michael DeHaan committed
45
    name: Example Developer
46 47 48 49 50 51 52
    job: Developer
    skill: Elite

Dictionaries can also be represented in an abbreviated form if you really want to::

    ---
    # An employee record
Michael DeHaan committed
53
    {name: Example Developer, job: Developer, skill: Elite}
54 55 56 57 58 59 60

.. _truthiness:

Ansible doesn't really use these too much, but you can also specify a 
boolean value (true/false) in several forms::

    ---
61 62
    create_key: yes
    needs_agent: no
63 64 65 66 67 68 69 70 71
    knows_oop: True
    likes_emacs: TRUE
    uses_cvs: false

Let's combine what we learned so far in an arbitary YAML example.  This really
has nothing to do with Ansible, but will give you a feel for the format::

    ---
    # An employee record
Michael DeHaan committed
72
    name: Example Developer
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    job: Developer
    skill: Elite
    employed: True
    foods:
        - Apple
        - Orange
        - Strawberry
        - Mango
    languages:
        ruby: Elite
	python: Elite
	dotnet: Lame

That's all you really need to know about YAML to get started writing
`Ansible` playbooks.

Michael DeHaan committed
89 90 91 92 93 94 95 96 97 98 99 100 101
Gotchas
-------

While YAML is generally friendly, the following is going to be a YAML syntax error:

    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.

102 103 104 105 106 107
.. 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
108
   `Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks>`_
109
       Complete playbook files from the github project source
110 111 112 113
   `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
114