logo
📚 其他

YAML

YAML Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 其他🧭 Markdown 速查🏷️ 2 个标签
#yaml#config
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Introduction

YAML is a data serialization language designed to be directly writable and readable by humans

  • YAML does not allow the use of tabs
  • Must be space between the element parts
  • YAML is CASE sensitive
  • End your YAML file with the .yaml or .yml extension
  • YAML is a superset of JSON
  • Ansible playbooks are YAML files {.marker-round}
Scalar types
<!-- prettier-ignore -->
YAML
滚动查看更多
n1: 1            # integer
n2: 1.234        # float

s1: 'abc'        # string
s2: "abc"        # string
s3: abc          # string

b: false         # boolean type

d: 2015-04-05    # date type

↓ Equivalent JSON

JSON
滚动查看更多
{
	"n1": 1,
	"n2": 1.234,
	"s1": "abc",
	"s2": "abc",
	"s3": "abc",
	"b": false,
	"d": "2015-04-05"
}

Use spaces to indent. There must be space between the element parts.

Variables
YAML
滚动查看更多
some_thing: &VAR_NAME foobar
other_thing: *VAR_NAME

↓ Equivalent JSON

<!-- prettier-ignore -->
JSON
滚动查看更多
{
  "some_thing": "foobar",
  "other_thing": "foobar"
}
Comments
YAML
滚动查看更多
# A single line comment example

# block level comment example
# comment line 1
# comment line 2
# comment line 3
Multiline strings
YAML
滚动查看更多
description: |
    hello
    world

↓ Equivalent JSON

JSON
滚动查看更多
{ "description": "hello\nworld\n" }
Inheritance
YAML
滚动查看更多
parent: &defaults
    a: 2
    b: 3

child:
    <<: *defaults
    b: 4

↓ Equivalent JSON

JSON
滚动查看更多
{
	"parent": {
		"a": 2,
		"b": 3
	},
	"child": {
		"a": 2,
		"b": 4
	}
}
Reference
YAML
滚动查看更多
values: &ref
    - Will be
    - reused below

other_values:
    i_am_ref: *ref

↓ Equivalent JSON

<!-- prettier-ignore -->
JSON
滚动查看更多
{
  "values": [
    "Will be",
    "reused below"
  ],
  "other_values": {
    "i_am_ref": [
      "Will be",
      "reused below"
    ]
  }
}
Folded strings
YAML
滚动查看更多
description: >
    hello world

↓ Equivalent JSON

JSON
滚动查看更多
{ "description": "hello world\n" }
Two Documents
YAML
滚动查看更多
---
document: this is doc 1
---
document: this is doc 2

YAML uses --- to separate directives from document content.

YAML Collections

Sequence
YAML
滚动查看更多
- Mark McGwire
- Sammy Sosa
- Ken Griffey

↓ Equivalent JSON

<!-- prettier-ignore -->
JSON
滚动查看更多
[
  "Mark McGwire",
  "Sammy Sosa",
  "Ken Griffey"
]
Mapping
<!-- prettier-ignore -->
YAML
滚动查看更多
hr:  65       # Home runs
avg: 0.278    # Batting average
rbi: 147      # Runs Batted In

↓ Equivalent JSON

JSON
滚动查看更多
{
	"hr": 65,
	"avg": 0.278,
	"rbi": 147
}
Mapping to Sequences
YAML
滚动查看更多
attributes:
    - a1
    - a2
methods: [getter, setter]

↓ Equivalent JSON

JSON
滚动查看更多
{
	"attributes": ["a1", "a2"],
	"methods": ["getter", "setter"]
}
Sequence of Mappings
<!-- prettier-ignore -->
YAML
滚动查看更多
children:
  - name: Jimmy Smith
    age: 15
  - name: Jimmy Smith
    age: 15
  -
    name: Sammy Sosa
    age: 12

↓ Equivalent JSON

<!-- prettier-ignore -->
JSON
滚动查看更多
{
  "children": [
    {"name": "Jimmy Smith", "age": 15},
    {"name": "Jimmy Smith", "age": 15},
    {"name": "Sammy Sosa", "age": 12}
  ]
}
Sequence of Sequences
<!-- prettier-ignore -->
YAML
滚动查看更多
my_sequences:
  - [1, 2, 3]
  - [4, 5, 6]
  -
    - 7
    - 8
    - 9
    - 0 

↓ Equivalent JSON

JSON
滚动查看更多
{
	"my_sequences": [
		[1, 2, 3],
		[4, 5, 6],
		[7, 8, 9, 0]
	]
}
Mapping of Mappings
YAML
滚动查看更多
Mark McGwire: { hr: 65, avg: 0.278 }
Sammy Sosa: { hr: 63, avg: 0.288 }

↓ Equivalent JSON

JSON
滚动查看更多
{
	"Mark McGwire": {
		"hr": 65,
		"avg": 0.278
	},
	"Sammy Sosa": {
		"hr": 63,
		"avg": 0.288
	}
}
Nested Collections
YAML
滚动查看更多
Jack:
    id: 1
    name: Franc
    salary: 25000
    hobby:
        - a
        - b
    location: { country: 'A', city: 'A-A' }

↓ Equivalent JSON

JSON
滚动查看更多
{
	"Jack": {
		"id": 1,
		"name": "Franc",
		"salary": 25000,
		"hobby": ["a", "b"],
		"location": {
			"country": "A",
			"city": "A-A"
		}
	}
}
Unordered Sets
YAML
滚动查看更多
set1: !!set
    ? one
    ? two
set2: !!set { 'one', 'two' }

↓ Equivalent JSON

JSON
滚动查看更多
{
	"set1": { "one": null, "two": null },
	"set2": { "one": null, "two": null }
}

Sets are represented as a Mapping where each key is associated with a null value

Ordered Mappings
YAML
滚动查看更多
ordered: !!omap
    - Mark McGwire: 65
    - Sammy Sosa: 63
    - Ken Griffy: 58

↓ Equivalent JSON

<!-- prettier-ignore -->
JSON
滚动查看更多
{
  "ordered": [
    { "Mark McGwire": 65 },
    { "Sammy Sosa": 63 },
    { "Ken Griffy": 58 }
  ]
}

YAML Reference

Terms
  • Sequences aka arrays or lists
  • Scalars aka strings or numbers
  • Mappings aka hashes or dictionaries {.marker-round}

Based on the YAML.org refcard.

Document indicators
%Directive indicator
---Document header
...Document terminator
Collection indicators
?Key indicator
:Value indicator
-Nested series entry indicator
,Separate in-line branch entries
[]Surround in-line series branch
{}Surround in-line keyed branch
Alias indicators
&Anchor property
*Alias indicator
Special keys
=Default "value" mapping key
<<Merge keys from another mapping
Scalar indicators

| | | | ----- | --------------------------------- | --------------------------------------------- | ----------- | | '' | Surround in-line unescaped scalar | | " | Surround in-line escaped scalar | | | | Block scalar indicator | | > | Folded scalar indicator | | - | Strip chomp modifier ( | -or>-) | | + | Keep chomp modifier ( | +or>+) | | 1-9 | Explicit indentation modifier ( | 1or>2). <br/> Modifiers can be combined ( | 2-, >+1) |

Tag Property (usually unspecified)
noneUnspecified tag (automatically resolved by application)
!Non-specific tag (by default, !!map/!!seq/!!str)
!fooPrimary (by convention, means a local !foo tag)
!!fooSecondary (by convention, means tag:yaml.org,2002:foo)
!h!fooRequires %TAG !h! <prefix> (and then means <prefix>foo)
!<foo>Verbatim tag (always means foo)
Misc indicators
#Throwaway comment indicator
<code>`@</code>Both reserved for future use
Core types (default automatic tags)
!!map{Hash table, dictionary, mapping}
!!seq{List, array, tuple, vector, sequence}
!!strUnicode string
Escape Codes

Numeric

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

{.cols-2 .marker-none}

Protective

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

{.cols-3 .marker-none}

C

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

{.cols-3 .marker-none}

Additional

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

{.cols-3 .marker-none}

More types
!!set{cherries, plums, apples}
!!omap[one: 1, two: 2]
Language Independent Scalar Types
{~, null}Null (no value).
[1234, 0x4D2, 02333][Decimal int, Hexadecimal int, Octal int]
[1_230.15, 12.3015e+02][Fixed float, Exponential float]
[.inf, -.Inf, .NAN][Infinity (float), Negative, Not a number]
{Y, true, Yes, ON}Boolean true
{n, FALSE, No, off}Boolean false

Also see

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572