YAML syntax
This article is from: http://www.ansible.com.cn/docs/YAMLSyntax.html
This page provides a basic overview of the correct YAML syntax, which is used to describe a playbooks (our Configuration management language).
We use YAML because it is like XML or JSON as a data format that facilitates people's reading and writing. In addition, there are libraries that use YAML in most languages.
You may want to read how playbooks is used in practice.
Basic YAML
For Ansible, each YAML file is started from a list. Each item in the list is a key-value pair, often referred to as a "hash" or "dictionary." So, we need to know how to write lists and dictionaries in YAML.
YAML also has a small quirk. All YAML files (regardless of whether they have a relationship with Ansible) should start with the line ---
. This is part of the YAML format, which indicates the beginning of a file.
All members in the list start at the same indentation level and use one "- "
as the beginning (a bar and a space):
---# A list of delicious fruits-Apple-orange-strawberry-mango
A dictionary is made 键: 值
up of a simple form (the colon must be followed by a space):
---# One employee's record name:example Developerjob:Developerskill:Elite
Dictionaries can also be expressed in indented form, if you like:
---# A worker's record {name:example Developer, job:developer, Skill:elite}
Ansible does not use this format too much, but you can specify a Boolean value (True/fase) in the following format:
---create_key:yesneeds_agent:noknows_oop:Truelikes_emacs:TRUEuses_cvs:false
Let's put together the YAML examples we've learned so far. None of this is going to work in Ansible, but these formats will give you a feeling:
---# A staff record name:example Developerjob:Developerskill:Eliteemployed:Truefoods:-Apple-orange-strawberry -Mangolanguages:ruby:Elite Python:elite Dotnet:lame
This is all the YAML syntax you need to know to start writing Ansibleplaybooks.
Gotchas
Although Yaml is usually friendly, the following will result in a YAML syntax error:
Foo:somebody said I should put a colon here:so I did
You need to use quotation marks to wrap any hash value that contains a colon, like this:
Foo: "Somebody said I should put a colon here:so I did"
The colon will then be terminated.
In addition, Ansible uses "{{var}}" to refer to variables. If a value begins with "{", YAML will think of it as a dictionary, so we must refer to it, like this:
Foo: "{{variable}}"
Learn about the YAML syntax before learning ansible playbook