Skip to content

Commit 566760b

Browse files
committed
Updated 'src/refs/YAML_syntax.md'.
1 parent 834ed82 commit 566760b

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed

ansible_quickstart/demo_yaml.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- hosts: localhost
3+
gather_facts: no
4+
5+
tasks:
6+
- debug:
7+
msg: "{{ version }}"
8+
9+
vars:
10+
non_boolean: "yes"
11+
other_string: "False"
12+
version: 1.10

src/refs/YAML_syntax.md

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,291 @@
11
# YAML 语法
22

3+
本页提供了正确 YAML 语法的一个基本概述,这是 Ansible playbooks(我们的配置管理语言)表达的方式。
4+
5+
6+
我们使用 YAML,因为他比 XML 或 JSON 等其他常见数据格式,更易于人类读写。此外,大多数编程语言都有处理 YAML 的库。
7+
8+
9+
咱们可能还希望同时阅读 [使用 playbook](../usage/playbooks.md),了解在实践中是 YAML 语法如何使用的。
10+
11+
12+
13+
## YAML 基础
14+
15+
对于 Ansible 来说,几乎每个 YAML 文件都以一个列表开始。列表中的每个项目,都是一些键/值对的列表,他们通常称为 “哈希值” 或 “字典”。因此,我们需要知道编写 YAML 中的列表与字典。
16+
17+
18+
YAML 还有个小怪癖,another small quirk。所有 YAML 文件(无论是否与 Ansible 相关),都可以可选地以 `---` 开头,以 `....` 结尾。这是 YAML 格式的一部分,表示文档的开始和结束。
19+
20+
21+
某个列表中的所有成员,都是以 `- `(一个破折号和一个空格)开头,位处同一缩进级别的一些行:
22+
23+
24+
```yaml
25+
---
26+
# A list of tasty fruits
27+
- Apple
28+
- Orange
29+
- Strawberry
30+
- Mango
31+
...
32+
```
33+
34+
字典是以简单的 `key: value` 形式表示(冒号后必须跟有一个空格):
35+
36+
37+
```yaml
38+
# An employee record
39+
martin:
40+
name: Martin D'vloper
41+
job: Developer
42+
skill: Elite
43+
```
44+
45+
46+
更复杂的一些数据结构是可行的,比如字典的列表、值为列表的字典,或二者的混合结构:
47+
48+
49+
```yaml
50+
# Employee records
51+
- martin:
52+
name: Martin D'vloper
53+
job: Developer
54+
skills:
55+
- python
56+
- perl
57+
- pascal
58+
- tabitha:
59+
name: Tabitha Bitumen
60+
job: Developer
61+
skills:
62+
- lisp
63+
- fortran
64+
- erlang
65+
```
66+
67+
68+
如果咱们确实打算这么做,那么字典和列表也可以用以一种简短形式表示:
69+
70+
71+
```yaml
72+
---
73+
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
74+
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']
75+
```
76+
77+
这些被称为 “流式集合, Flow Collections”。
78+
79+
80+
> **译注**:上面是编写 YAML 的两种风格/样式,前一种称作序列样式,sequences style;后一种是更紧凑的流样式,flow style。
81+
>
82+
> 参考:
83+
>
84+
> - [Flow Style](https://www.yaml.info/learn/flowstyle.html)
85+
86+
87+
虽然 Ansible 并不经常用到,但咱们也可以通过多种形式,指定某个布尔值(`true`/`false`):
88+
89+
90+
```yaml
91+
create_key: true
92+
needs_agent: false
93+
knows_oop: True
94+
likes_emacs: TRUE
95+
uses_cvs: false
96+
```
97+
98+
若咱们与默认的 `yamllint` 选项兼容,那么就要对字典中的布尔值,使用小写 `true` 或 `false`。
99+
100+
101+
使用 `|` 或 `>` 就可以让值跨越多行。使用 “字面区块标量,Literal Block Scalar” `|` 的跨越多行,将包括换行符和全部的结尾空格。而使用 “折叠区块标量,Folded Block Scalar” `>`,则会将换行符折叠为空格,fold newlines to spaces;其被用于使本来很长的行,成为更容易阅读和编辑。不论使用 `|` 还是 `>`,缩进都将被忽略。例如:
102+
103+
104+
```yaml
105+
include_newlines: |
106+
exactly as you see
107+
will appear these three
108+
lines of poetry
109+
110+
fold_newlines: >
111+
this is really a
112+
single line of text
113+
despite appearances
114+
```
115+
116+
尽管在上面的 `>` 示例中,所有换行符都会被折叠成空格,但有两种强制保留换行符的方法:
117+
118+
119+
```yaml
120+
fold_some_newlines: >
121+
a
122+
b
123+
124+
c
125+
d
126+
e
127+
f
128+
```
129+
130+
> **译注**:输出为 `"a b\nc d\n e\nf\n"`。
131+
132+
133+
或者,也可以通过加入换行符,强制保留换行符:
134+
135+
```yaml
136+
fold_same_newlines: "a b\nc d\n e\nf\n"
137+
```
138+
139+
140+
咱们把我们迄今所学到的知识,结合在一个任意的 YAML 示例中。这其实与 Ansible 无关,但可以让咱们感受一下这种格式:
141+
142+
143+
```yaml
144+
---
145+
# An employee record
146+
name: Martin D'vloper
147+
job: Developer
148+
skill: Elite
149+
employed: True
150+
foods:
151+
- Apple
152+
- Orange
153+
- Strawberry
154+
- Mango
155+
languages:
156+
perl: Elite
157+
python: Elite
158+
pascal: Lame
159+
education: |
160+
4 GCSEs
161+
3 A-Levels
162+
BSc in the Internet of Things
163+
```
164+
165+
166+
这就是开始编写 *Ansible* playbook,咱们需要知道的全部 YAML 知识了。
167+
168+
169+
## 一些问题
170+
171+
**Gotchas**
172+
173+
174+
虽然咱们可以把几乎任何东西,都放在一个在无引号的标量中,但也有一些例外。冒号后跟一个空格(或换行符)`": "`是一种映射的指示符。而空格后跟一个磅号 `" #"` 则表示注释。
175+
176+
177+
由于这个原因,以下带脉将导致 YAML 的语法错误:
178+
179+
```yaml
180+
foo: somebody said I should put a colon here: so I did
181+
182+
windows_drive: c:
183+
```
184+
185+
...... 不过下面这个是可行的:
186+
187+
188+
```yaml
189+
foo: somebody said I should put a colon here:so I did
190+
windows_path: c:\windows
191+
```
192+
193+
> **译注:** 这将得到 `"c:\\windows"`。
194+
195+
对于那些用到了后跟空格,位处行末的冒号的哈希值,咱们会打算用单引号将其括起来:
196+
197+
```yaml
198+
foo: 'somebody said I should put a colon here: so I did'
199+
200+
windows_drive: 'c:'
201+
```
202+
203+
...... 这是其中的冒号就将被保留。
204+
205+
或者,咱们也可以使用双引号:
206+
207+
208+
```yaml
209+
foo: "somebody said I should put a colon here: so I did"
210+
211+
windows_drive: "c:"
212+
```
213+
214+
215+
单引号与双引号的区别在于,在双引号中咱们可以使用转义字符:
216+
217+
218+
```yaml
219+
foo: "a \t TAB and a \n NEWLINE"
220+
```
221+
222+
223+
允许使用的转义字符列表,可在 YAML 规范中的 [“转义序列”](https://yaml.org/spec/1.1/#escaping%20in%20double-quoted%20style/)(YAML 1.1)或 [“转义字符”](https://yaml.org/spec/1.2.2/#57-escaped-characters)(YAML 1.2)下找到。
224+
225+
226+
以下是无效的 YAML:
227+
228+
229+
```yaml
230+
foo: "an escaped \' single quote"
231+
```
232+
233+
此外,Ansible 会将 `"{{ var }}}"` 用于变量。如果冒号后的某个值以 `"{"` 开头,YAML 就会认为他是个字典,因此咱们必须像下面这样将其用双引号括起来:
234+
235+
236+
```yaml
237+
foo: "{{ variable }}"
238+
```
239+
240+
如果咱们的值以单引号开头,则必须将整个值用双引号括起来,而不只是其中一部分。下面是一些如何正确地把值用双引号括起来的补充示例:
241+
242+
```yaml
243+
foo: "{{ variable }}/additional/string/literal"
244+
foo2: "{{ variable }}\\backslashes\\are\\also\\special\\characters"
245+
foo3: "even if it is just a string literal it must all be quoted"
246+
```
247+
248+
249+
无效示例:
250+
251+
252+
```yaml
253+
foo: "E:\\path\\"rest\\of\\path
254+
```
255+
256+
257+
除 `'` 和 `"` 外,还有一些字符是特殊字符(或保留字符),而不能用作无引号标量的第一个字符: <code>[] {} > | * & ! % # ` @ ,</code>。
258+
259+
260+
咱们还应当心 `? : -`。 在 YAML 中,如果他们后跟某个非空格字符,则允许在字符串开头使用这些字符,但 YAML 处理器的实现各不相同,因此最好使用引号(译注:单/双引号)。
261+
262+
263+
264+
在流式集合中,规则更为严格:
265+
266+
267+
```yaml
268+
a scalar in block mapping: this } is [ all , valid
269+
270+
flow mapping: { key: "you { should [ use , quotes here" }
271+
```
272+
273+
布尔值的转换很有帮助,但在咱们想要的是字面意义上的 `"yes"`,或将别的布尔值作为字符串时,就会出现问题。在这种情况下,就要使用引号:
274+
275+
276+
```yaml
277+
non_boolean: "yes"
278+
other_string: "False"
279+
```
280+
281+
282+
YAML 会将某些字符串转换为浮点数值,例如字符串 `1.0`。在咱们需要指定出版本号(例如在 `requirements.yml` 文件中)shi,如果该值看起来像个浮点数值,就要将该值用引号括起来:
283+
284+
285+
```yaml
286+
version: "1.10"
287+
```
288+
3289

4290
(End)
5291

0 commit comments

Comments
 (0)