Contents
  1. 1. list
  2. 2. dictionary
  3. 3. gotcha

对ansible来说,几乎每个yaml文件都以一个列表开头,而每一项则是一个key-value对。yaml文件可分别以---...来标识文件的开始和结束。

list

ymal列表项起始于同一缩进的-,注意空格不可省略。

1
2
3
4
5
6
7
8
---
# A list of tasty fruits
fruits:
- Apple
- Orange
- Strawberry
- Mango
...

dictionary

而字典则是以:分割,空格同样不可省略。

1
2
3
4
martin:
name: Martin D'vloper
job: Developer
skill: Elite

列表与字典的混合也是支持的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Employee records
- martin:
name: Martin D'vloper
job: Developer
skills:
- python
- perl
- pascal
- tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- lisp
- fortran
- erlang

如果你一定要的话,也有另外的表达方式

1
2
3
---
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']

在ansible中不常用到boolean,但yaml支持几种表达方式

1
2
3
4
5
create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false

值可以通过|>来跨行,不同的是前者包括了换行符而后者则会将行拼接

1
2
3
4
5
6
7
8
9
include_newlines: |
exactly as you see
will appear these three
lines of poetry
ignore_newlines: >
this is really a
single line of text
despite appearances

总结一下所学

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---
# An employee record
name: Martin D'vloper
job: Developer
skill: Elite
employed: True
foods:
- Apple
- Orange
- Strawberry
- Mango
languages:
perl: Elite
python: Elite
pascal: Lame
education: |
4 GCSEs
3 A-Levels
BSc in the Internet of Things

gotcha

如果字符串中包含冒号:,则需要用引号引起,否则会出错

1
foo: "somebody said I should put a colon here: so I did"

另外ansible使用来表示变量,以{开头的行也需要用引号。

1
foo: "{{ variable }}"

以上情况对以yaml中特殊字符如[] {} : > |开头的行都适用。

如果想得到字符串而不是对应的boolean值,那么也要用括号

1
2
non_boolean: "yes"
other_string: "False"

Contents
  1. 1. list
  2. 2. dictionary
  3. 3. gotcha