YAML 语法速查表
YAML(YAML Ain't Markup Language)是一种人类可读的数据序列化格式,广泛用于配置文件、CI/CD 流水线定义和跨语言数据交换。本速查表涵盖从基础语法到高级特性(锚点、多行字符串、类型系统)的常用用法。
更新: 2026-07-17·32 条命令
概述
YAML 是一种人类可读的数据序列化格式,广泛用于配置文件。本速查表涵盖 YAML 语法、数据类型、多行字符串、锚点和高级特性。
快速开始
``yaml
key: value
list:
- item1
- item2
map:
subkey: subvalue
``
详细用法请参阅下方命令表格。
基本语法(6)
| 命令 | 难度 | ||
|---|---|---|---|
key: value基本键值对映射 | 基础 | name: John Doe | |
- item列表/数组定义 | 基础 | - apple
- banana
- cherry | |
nested mapping嵌套映射(缩进表示层级) | 基础 | person:
name: Alice
age: 30
address:
city: Beijing | |
list of mappings映射列表(每个元素是键值对) | 基础 | - name: Alice
age: 30
- name: Bob
age: 25 | |
# comment注释(从 # 到行尾) | 基础 | # This is a YAML comment
key: value # inline comment | |
indentation rules缩进规则(2 空格,禁止 Tab) | 基础 | parent:
child:
grandchild: value |
数据类型(7)
| 命令 | 难度 | ||
|---|---|---|---|
'single quoted'单引号字符串(不解析转义) | 基础 | 'Don''t escape \n here' | |
"double quoted"双引号字符串(解析转义序列) | 基础 | "Line1\nLine2\tTabbed" | |
unquoted string无引号字符串(简单值) | 基础 | hello world | |
true / false布尔值(大小写不敏感) | 基础 | enabled: true
debug: no
verbose: on | |
null / ~空值表示 | 基础 | value: null
also_null: ~
blank: | |
integer / float整数和浮点数 | 基础 | count: 42
pi: 3.14
hex: 0x1A
sci: 1e6 | |
ISO 8601 timestampISO 8601 时间戳 | 中级 | created: 2026-07-17T14:30:00Z
updated: 2026-07-17 |
多行字符串(4)
| 命令 | 难度 | ||
|---|---|---|---|
|字面块标量(保留换行) | 中级 | script: |
echo hello
echo world | |
>折叠块标量(换行变空格) | 中级 | description: >
This is a long
description that will
be folded into one line | |
|-字面块 + 去掉末尾换行 | 中级 | script: |-
line1
line2 | |
|+字面块 + 保留所有末尾换行 | 中级 | block: |+
keep
blank lines |
锚点别名(5)
| 命令 | 难度 | ||
|---|---|---|---|
&-name定义锚点 | 中级 | defaults: &defaults
timeout: 30
retries: 3 | |
*-name引用锚点(别名) | 中级 | server1:
<<: *defaults
host: host1.example.com | |
<<合并键(将锚点映射合并到当前映射) | 中级 | base: &base
x: 1
y: 2
item:
<<: *base
z: 3 | |
nested anchors嵌套锚点与别名 | 中级 | conn: &conn
host: localhost
port: 5432
db:
<<: *conn
name: mydb | |
multiple aliases一个锚点被多处引用 | 中级 | shared: &shared
env: prod
srv1:
<<: *shared
name: web
srv2:
<<: *shared
name: worker |
高级特性(10)
| 命令 | 难度 | ||
|---|---|---|---|
GitHub Actions workflowYAML 在 GitHub Actions 中的应用 | 中级 | name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 | |
GitLab CI pipelineYAML 在 GitLab CI 中的应用 | 中级 | stages:
- build
- test
build_job:
stage: build
script:
- make build | |
yq evalyq 读取 YAML 值 | 基础 | yq eval '.name' config.yaml | |
yq eval arrayyq 遍历数组元素 | 基础 | yq eval '.items[]' config.yaml | |
yq eval with pipeyq 管道查询 | 中级 | yq eval '.users[] | select(.active == true)' config.yaml | |
yq eval -iyq 原地修改 YAML 文件 | 中级 | yq eval -i '.version = "2.0"' config.yaml | |
yq --yaml-allyq 处理多文档 YAML 的所有文档 | 高级 | yq eval --yaml-all '.kind' multi.yaml | |
yq path expressionsyq 路径表达式(通配、递归) | 高级 | yq eval '.. | select(. == "target")' config.yaml | |
merge YAML files合并多个 YAML 文件 | 高级 | yq eval-all '. as $item ireduce ({}; . * $item)' a.yaml b.yaml | |
multi-document YAML多文档 YAML(--- 分隔) | 中级 | ---
apiVersion: v1
kind: Pod
---
apiVersion: v1
kind: Service |
常见问题
本速查表数据整理自各工具官方文档。最后更新: 2026-07-17。