??近期在調(diào)試SONIC上基于Ansible的PTF測試平臺時(shí),使用到了YANML配置語言,此處稍作解釋,匯總一下YAML的語法及基本使用方法。
??YAML(/?j?m?l/,尾音類似camel駱駝)是一個(gè)可讀性高,用來表達(dá)數(shù)據(jù)序列的格式。YAML參考了其他多種語言,包括:C語言、Python、Perl,并從XML、電子郵件的數(shù)據(jù)格式(RFC 2822)中獲得靈感。上述介紹引用自維基百科,通俗的講,YAML是專門用來寫配置文件的語言,非常簡潔和強(qiáng)大,遠(yuǎn)比 JSON/XML格式方便。YAML允許在層次結(jié)構(gòu)中存儲結(jié)構(gòu)化數(shù)據(jù)。YAML 旨在以人為和機(jī)器可讀,并且開銷最小。YAML 規(guī)范可以在 yaml.org 找到,官方還提供了一個(gè)便捷的YAML小抄。
YAML的基本語法規(guī)則
縮進(jìn)
- 使用空格作為縮進(jìn)標(biāo)記,不可以使用tab
- 使用縮進(jìn)表征層級關(guān)系,縮進(jìn)空格數(shù)不做限制,但要求同一層級左對齊
大小寫
- YAML對大小寫敏感
注釋
- 使用#表明注釋,從*字符一直到行尾,都會被解析器忽略
多文檔支持
- YAML支持使用三個(gè)破折號分割文檔---
- ---上下兩部分會作為兩個(gè)獨(dú)立的文檔同等對待
區(qū)塊字符
??再次強(qiáng)調(diào),字符串不需要包在引號之內(nèi)。有兩種方法書寫多行文字(multi-line strings),一種可以保存新行(使用|字符),另一種可以折疊新行(使用>字符)
- 保存新行(Newlines preserved)
data: | # 譯者注:這是一首著名的五行民謠
There once was a man from Darjeeling # 這里曾有一個(gè)人來自大吉領(lǐng)
Who got on a bus bound for Ealing # 他搭上一班往伊靈的公車
It said on the door # 門上這么說的
"Please don't spit on the floor" # "請勿在地上吐痰"
So he carefully spat on the ceiling # 所以他小心翼翼的吐在天花板上
- 折疊新行(Newlines folded)
data: >
Wrapped text # 摺疊的文字
will be folded # 將會被收
into a single # 進(jìn)單一一個(gè)
paragraph # 段落
Blank lines denote # 空白的行代表
paragraph breaks # 段落之間的間隔
和保存新行不同的是,換行字符會被轉(zhuǎn)換成空白字符。而引領(lǐng)空白字符則會被自動消去。
數(shù)據(jù)結(jié)構(gòu)
常量
常量值最基本的數(shù)字、字符串、布爾值等等
number-value: 42
floating-point-value: 3.141592
boolean-value: true
# 對于字符串,在YAML中允許不添加引號。
string-value: 'Bonjour'
字典
- 使用:表征一個(gè)鍵值對
my_key: my_value - 冒號后面必須間隔至少一個(gè)空格
my_key: my_value
#或者表達(dá)為:
my_key:
my_value
在python中,會將上述鍵值對解析為一個(gè)字典
{'my_key': 'my_value'}
上述鍵值對可以嵌套,從而實(shí)現(xiàn)更加復(fù)雜的數(shù)據(jù)結(jié)構(gòu):
first_level_dict_key:
second_level_dict_key: value_in_second_level_dict
在python中,會將其解析為嵌套的字典結(jié)構(gòu):
{
'first_level_dict_key': {
'second_level_dict_key': 'value_in_second_level_dict'
}
}
字典嵌套列表
- 破折號可用于描述列表結(jié)構(gòu)
- 破折號后必須有空格做間隔
列表和鍵值對嵌套使用:
my_dictionary:
- list_value_one
- list_value_two
- list_value_three
在python中,會將其解析為字典嵌套列表的復(fù)合數(shù)據(jù)結(jié)構(gòu):
{'my_dictionary': ['list_value_one', 'list_value_two', 'list_value_three']}
高級用法
數(shù)據(jù)合并和參考
??為了維持文件的簡潔,并避免數(shù)據(jù)輸入的錯(cuò)誤,YAML提供了結(jié)點(diǎn)參考(*)和散列合并(<<)參考到其他結(jié)點(diǎn)標(biāo)簽的錨點(diǎn)標(biāo)記(&)。參考會將樹狀結(jié)構(gòu)加入錨點(diǎn)標(biāo)記的內(nèi)容,并可以在所有數(shù)據(jù)結(jié)構(gòu)中運(yùn)作(可以參考上面"ship-to"的示例)合并只有散列表可以使用,可以將鍵值自錨點(diǎn)標(biāo)記復(fù)制到指定的散列表中。當(dāng)數(shù)據(jù)被instantiate合并和參考會被剖析器自動展開。
- step: &id001 #定義錨點(diǎn) &id001
instrument: Lasik 2000
pulseEnergy: 5.4
pulseDuration: 12
repetition: 1000
spotSize: 1mm
- step:
<<: *id001 # 合併鍵值:使用在錨點(diǎn)標(biāo)簽定義的內(nèi)容
spotSize: 2mm # 覆寫"spotSize"鍵值
- step:
<<: *id001 # 合併鍵值:使用在錨點(diǎn)標(biāo)簽定義的內(nèi)容
pulseEnergy: 500.0 # 覆寫鍵值
alert: > # 加入其他鍵值
warn patient of
audible pop
YAML小抄
%YAML 1.1 # Reference card
---
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.
Scalar indicators:
'''' : Surround in-line unescaped scalar ('' escaped ').
'"' : Surround in-line escaped scalar (see escape codes below).
'|' : Block scalar indicator.
'>' : Folded scalar indicator.
'-' : Strip chomp modifier ('|-' or '>-').
'+' : Keep chomp modifier ('|+' or '>+').
1-9 : Explicit indentation modifier ('|1' or '>2').
# Modifiers can be combined ('|2-', '>+1').
Alias indicators:
'&' : Anchor property.
'*' : Alias indicator.
Tag property: # Usually unspecified.
none : Unspecified tag (automatically resolved by application).
'!' : Non-specific tag (by default, "!!map"/"!!seq"/"!!str").
'!foo' : Primary (by convention, means a local "!foo" tag).
'!!foo' : Secondary (by convention, means "tag:yaml.org,2002:foo").
'!h!foo': Requires "%TAG !h! <prefix>" (and then means "<prefix>foo").
'!<foo>': Verbatim tag (always means "foo").
Document indicators:
'%' : Directive indicator.
'---': Document header.
'...': Document terminator.
Misc indicators:
' #' : Throwaway comment indicator.
'`@' : Both reserved for future use.
Special keys:
'=' : Default "value" mapping key.
'<<' : Merge keys from another mapping.
Core types: # Default automatic tags.
'!!map' : { Hash table, dictionary, mapping }
'!!seq' : { List, array, tuple, vector, sequence }
'!!str' : Unicode string
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
? !!binary >
R0lG...BADS=
: >-
Base 64 binary value.
Escape codes:
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 }
Additional: { "\e": ESC, "\_": NBSP, "\N": NEL, "\L": LS, "\P": PS }
...