YAML Syntax
YAML Basics
For Ansible, nearly every YAML file starts with a list. Each item in the list is a list of key/value pairs,
commonly called a “hash” or a “dictionary”. So, we need to know how to write lists and dictionaries in YAML.
Ansible,几乎每个YAML文件从列表开始。列表中的每个条目是一个键/值对列表,通常被称为“哈希”或“字典”。所以,我们需要知道如何
编写列表和字典在YAML。
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.
还有一个小怪癖YAML。所有YAML文件(不管他们的协会和Ansible)可以从- - -开始和结束与....这是YAML格式和显示文档的开始和结束。
All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):
所有成员列表的行开始有相同的缩进级别从“-”(一个破折号空间):
---
# A list of tasty fruits
fruits:
- Apple
- Orange
- Strawberry
- Mango
...
A dictionary is represented in a simple key: value form (the colon must be followed by a space):
字典是在一个简单的键:值形式(冒号必须遵循的空间):
# An employee record
- martin:
name: Martin D'vloper
job: Developer
skill: Elite
Dictionaries and lists can also be represented in an abbreviated form if you really want to:
字典和列表也可以表示如果你真的想在一个缩写形式:
---
employees:
- martin: {name: Martin D'vloper, job: Developer, skill: Elite}
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']
Ansible doesn’t really use these too much, but you can also specify a boolean value (true/false) in several forms:
Ansible并不使用这些太多,但你也可以指定一个布尔值(真/假)的几种形式:
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false
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:
让我们结合我们所学到到目前为止在任意YAML的例子。这真的与Ansible无关,但会让你感觉格式:
---
# An employee record
name: Martin D'vloper
job: Developer
skill: Elite
employed: True
foods:
- Apple
- Orange
- Strawberry
- Mango
languages:
ruby: Elite
python: Elite
dotnet: Lame
Gotchas
While YAML is generally friendly, the following is going to result in a YAML syntax error:
虽然YAML通常是友好的,下面将会导致YAML语法错误:
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"
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:
此外,Ansible使用“{ { var } }”变量。如果一个冒号后一个值从一个“{”YAML将认为这是一本字典,所以你必须引用它,如下所示:
foo: "` variable `"
The same applies for strings that start or contain any YAML special characters `` [] {} : > | `` .
同样适用于启动或包含任何YAML特殊字符的字符串“[]{ }:> |“。
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"
Ansible通过模块的方式来完成一些远程的管理工作。可以通过ansible-doc -l查看所有模块,可以使用ansible-doc -s module来查看某个模块的参数具体用法,也可以使用ansible-doc help module来查看该模块更详细的信息。