YAML 备忘清单

这是理解和编写 YAML 格式配置文件的快速参考备忘单。

入门

介绍

YAML 是一种数据序列化语言,旨在供人类直接读写

  • YAML 不允许使用制表符
  • 元素部分之间必须有空间
  • YAML 区分大小写
  • .yaml.yml 扩展名结束您的 YAML 文件
  • YAML 是 JSON 的超集
  • Ansible playbook 是 YAML 文件

标量类型

  1. n1: 1 # 整数
  2. n2: 1.234 # 浮点
  3. s1: 'abc' # 字符串
  4. s2: "abc" # 字符串
  5. s3: abc # 字符串
  6. b: false # 布尔类型
  7. d: 2015-04-05 # 日期类型

↓ 等效的 JSON

  1. {
  2. "n1": 1,
  3. "n2": 1.234,
  4. "s1": "abc",
  5. "s2": "abc",
  6. "s3": "abc",
  7. "b": false,
  8. "d": "2015-04-05"
  9. }

使用空格缩进。 元素部分之间必须有空间。

变量

  1. some_thing: &VAR_NAME foobar
  2. other_thing: *VAR_NAME

↓ 等效的 JSON

```json {.wrap} { “some_thing”: “foobar”, “other_thing”: “foobar” }

  1. ### 注释
  2. ```yaml
  3. # A single line comment example
  4. # block level comment example
  5. # comment line 1
  6. # comment line 2
  7. # comment line 3

多行字符串

  1. description: |
  2. hello
  3. world

↓ 等效的 JSON

```json {.wrap} {“description”: “hello\nworld\n”}

  1. ### 继承
  2. <!--rehype:wrap-class=row-span-2-->
  3. ```yaml
  4. parent: &defaults
  5. a: 2
  6. b: 3
  7. child:
  8. <<: *defaults
  9. b: 4

↓ 等效的 JSON

```json {.wrap} { “parent”: { “a”: 2, “b”: 3 }, “child”: { “a”: 2, “b”: 4 } }

  1. ### 参考
  2. <!--rehype:wrap-class=row-span-2-->
  3. ```yaml
  4. values: &ref
  5. - Will be
  6. - reused below
  7. other_values:
  8. i_am_ref: *ref

↓ 等效的 JSON

```json {.wrap} { “values”: [ “Will be”, “reused below” ], “other_values”: { “i_am_ref”: [ “Will be”, “reused below” ] } }

  1. ### 折叠的字符串
  2. ```yaml
  3. description: >
  4. hello
  5. world

↓ 等效的 JSON

```json {.wrap} {“description”: “hello world\n”}

  1. ### 两份文件
  2. ```yaml
  3. ---
  4. document: this is doc 1
  5. ---
  6. document: this is doc 2

YAML使用---将指令与文档内容分开。

YAML Collections

序列

  1. - Mark McGwire
  2. - Sammy Sosa
  3. - Ken Griffey

↓ 等效的 JSON

  1. [
  2. "Mark McGwire",
  3. "Sammy Sosa",
  4. "Ken Griffey"
  5. ]

映射

  1. hr: 65 # Home runs
  2. avg: 0.278 # Batting average
  3. rbi: 147 # Runs Batted In

↓ 等效的 JSON

  1. {
  2. "hr": 65,
  3. "avg": 0.278,
  4. "rbi": 147
  5. }

映射到序列

  1. attributes:
  2. - a1
  3. - a2
  4. methods: [getter, setter]

↓ 等效的 JSON

  1. {
  2. "attributes": ["a1", "a2"],
  3. "methods": ["getter", "setter"]
  4. }

映射序列

  1. children:
  2. - name: Jimmy Smith
  3. age: 15
  4. - name: Jimmy Smith
  5. age: 15
  6. -
  7. name: Sammy Sosa
  8. age: 12

↓ 等效的 JSON

  1. {
  2. "children": [
  3. {"name": "Jimmy Smith", "age": 15},
  4. {"name": "Jimmy Smith", "age": 15},
  5. {"name": "Sammy Sosa", "age": 12}
  6. ]
  7. }

序列的序列

  1. my_sequences:
  2. - [1, 2, 3]
  3. - [4, 5, 6]
  4. -
  5. - 7
  6. - 8
  7. - 9
  8. - 0

↓ 等效的 JSON

  1. {
  2. "my_sequences": [
  3. [1, 2, 3],
  4. [4, 5, 6],
  5. [7, 8, 9, 0]
  6. ]
  7. }

映射的映射

  1. Mark McGwire: {hr: 65, avg: 0.278}
  2. Sammy Sosa: {
  3. hr: 63,
  4. avg: 0.288
  5. }

↓ 等效的 JSON

  1. {
  2. "Mark McGwire": {
  3. "hr": 65,
  4. "avg": 0.278
  5. },
  6. "Sammy Sosa": {
  7. "hr": 63,
  8. "avg": 0.288
  9. }
  10. }

嵌套集合

  1. Jack:
  2. id: 1
  3. name: Franc
  4. salary: 25000
  5. hobby:
  6. - a
  7. - b
  8. location: {country: "A", city: "A-A"}

↓ 等效的 JSON

  1. {
  2. "Jack": {
  3. "id": 1,
  4. "name": "Franc",
  5. "salary": 25000,
  6. "hobby": ["a", "b"],
  7. "location": {
  8. "country": "A", "city": "A-A"
  9. }
  10. }
  11. }

无序集

  1. set1: !!set
  2. ? one
  3. ? two
  4. set2: !!set {'one', "two"}

↓ 等效的 JSON

  1. {
  2. "set1": {"one": null, "two": null},
  3. "set2": {"one": null, "two": null}
  4. }

集合表示为一个映射,其中每个键都与一个空值相关联

有序映射

  1. ordered: !!omap
  2. - Mark McGwire: 65
  3. - Sammy Sosa: 63
  4. - Ken Griffy: 58

↓ 等效的 JSON

  1. {
  2. "ordered": [
  3. {"Mark McGwire": 65},
  4. {"Sammy Sosa": 63},
  5. {"Ken Griffy": 58}
  6. ]
  7. }

YAML 参考

条款

  • 序列又名数组或列表
  • 标量又名字符串或数字
  • 映射又名哈希或字典

基于 YAML.org refcard

文档指标

:- :-
% 指令指标
--- 文档标题
... 文档终结者

收集指标

:- :-
? 关键指标
: 价值指标
- 嵌套系列条目指示器
, 单独的内联分支条目
[] 环绕串联系列分支
{} 环绕在线键控分支

别名指标

:- :-
& 锚属性
* 别名指示符

特殊键

:- :-
= 默认“值”映射键
<< 合并来自另一个映射的键

标量指标

:- :-
'' 环绕内联未转义标量
" 环绕内嵌转义标量
` ` 块标量指示器
> 折叠标量指示器
- 剥离 chomp 修饰符(`\ ->-`)
+ 保留 chomp 修饰符(`\ +>+`)
1-9 显式缩进修饰符(`\ 1>2)。 <br/> 修饰符可以组合(\ 2-,>+1`)

标签属性(通常未指定)

:- :-
none 未指定的标签(由应用程序自动解析)
! 非特定标签(默认情况下,!!map/!!seq/!!str
!foo 主要(按照惯例,表示本地 !foo 标记)
!!foo 次要的(按照惯例,表示 tag:yaml.org,2002:foo
!h!foo 需要 %TAG !h! <prefix>(然后表示 <prefix>foo
!<foo> 逐字标记(始终表示“foo”)

杂项指标

# 一次性评论指示器
`@ 两者都保留供将来使用

核心类型(默认自动标签)

!!map {Hash table, dictionary, mapping}
!!seq {List, array, tuple, vector, sequence}
!!str Unicode 字符串

转义码

Numeric

  • \x12 (8-bit)
  • \u1234 (16-bit)
  • \U00102030 (32-bit)

Protective

  • \\ (\)
  • \" (“)
  • \ ( )
  • \<TAB> (TAB)

C

  • \0 (NUL)
  • \a (BEL)
  • \b (BS)
  • \f (FF)
  • \n (LF)
  • \r (CR)
  • \t (TAB)
  • \v (VTAB)

额外的

  • \e (ESC)
  • \_ (NBSP)
  • \N (NEL)
  • \L (LS)
  • \P (PS)

更多类型

!!set {cherries, plums, apples}
!!omap [one: 1, two: 2]

与语言无关的标量类型

{~, null} 空(无值)。
[1234, 0x4D2, 02333] [十进制整数、十六进制整数、八进制整数]
[1_230.15, 12.3015e+02] [固定浮点数,指数浮点数]
[.inf, -.Inf, .NAN] [无穷大(浮点数),负数,不是数字]
{Y, true, Yes, ON} 布尔真
{n, FALSE, No, off} 布尔假

另见