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來查看該模塊更詳細的信息。