概念

YAML(YAML Ain’t Markup Language),是一种可读性非常高的数据格式。YAML不再以标记为重点语言,而是围绕数据来组织结构化格式。常见YAML用于Playbook、K8S资源文件的书写标准

YAML文件后缀为yml、yaml

YAML语法规则

  1. 使用缩进表示层级关系、缩进的空格数不重要,只要相同的元素左对齐即可
  2. YAML支持字典、数组、纯量(字符串、布尔值、整数、浮点数、Null、时间、日期)
  3. YAML以#表示注释
  4. 区分大小写

纯量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
name: xiaowangc		# 字符串
age: 18 # 整数
number: 18.1 # 浮点数
one: true # 布尔值
two: null # null
two: ~ # null
date: 2022-07-09 # 时间
time: 2022-07-09T20:30+08:00 # 日期 年月日 小时分钟 时区

# 多行表示
str: >
This is a long string written by xiaowangc
This is a long string written by xiaowangc

效果: "This is a long string written by xiaowangc This is a long string written by xiaowangc"

# 按原格式
str: |
This is a long string written by xiaowangc
This is a long string written by xiaowangc
This is a long string written by xiaowangc

效果: "
This is a long string written by xiaowangc
This is a long string written by xiaowangc
This is a long string written by xiaowangc
"

数组

1
2
3
4
5
6
7
8
9
10
11
12
# 方式一
name:
- xiaowangc
- zhangsan

# 方式二
name:
- xiaowangc
- zhangsan

# 方式三
name: [xiaowangc,zhangsan]

对象

1
2
3
4
5
6
7
names:
name01: xiaowangc
name02: zhangsan
name03: lisi
name04: wanger
age01: 1234
age02: 7.7