YAML 的設計目標就是方便人們讀寫,實質上是通用的數據串行化格式。
[在線驗證][1],[阮一峰 YAML教程][2]
## 基本規則
* 大小寫敏感
* 使用縮進表示層級關系,縮進只能使用空格
* 文檔以“---”作為開頭,以“...”作為結尾
* \# 表示注釋
* YAML的字符串即使含有空格也不需要引號引起來
## 純量
### 布爾型
`True`和`False`
### 數值
~~~
number: 1
float: 3.41
~~~
### 字符串
~~~
str: 這是一行字符串
str: !!str 123 # 強制類型轉換
~~~
### 多行字符串
~~~
str: 這是
多行
字符串
~~~
> 多行字符串可以使用`|`保留換行符,也可以使用`>`折疊換行。
> `+`表示保留文字塊末尾的換行,`-`表示刪除字符串末尾的換行。
~~~
this: |
Foo
Bar
that: >
Foo
Bar
~~~
轉為 JavaScript 代碼如下。
~~~
{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }
~~~
~~~
s1: >
hi
abc
s2: >-
hi
abc
s3: >+
Foo
hi
s4: |
Foo
hi
s5: |-
Foo
s6: |+
Foo
~~~
轉為 JavaScript 代碼如下。
~~~
{ s1: 'hi abc\n',
s2: 'hi abc',
s3: 'Foo hi\n\n',
s4: 'Foo\nhi\n',
s5: 'Foo',
s6: 'Foo\n' }
~~~
## 變量
### 列表
列表使用`'-'`作為分隔符,
~~~
- My Fair Lady
- The old boy
- The sea
~~~
YAML還有一種內聯格式的列表
~~~
[My Fair Lady, The old boy, The sea]
~~~
### 字典
~~~
hello: world
hi: yang
~~~
YAML還有一種內聯格式的字典
~~~
{hello: world, hi: yang}
~~~
## 引用
> 錨點&和別名*,可以用來引用。
~~~
defaults: &defaults
adapter: postgres
host: localhost
development:
database: myapp_development
<<: *defaults
test:
database: myapp_test
<<: *defaults
~~~
等同于下面的代碼。
~~~
defaults:
adapter: postgres
host: localhost
development:
database: myapp_development
adapter: postgres
host: localhost
test:
database: myapp_test
adapter: postgres
host: localhost
~~~
&用來建立錨點(defaults),<<表示合并到當前數據,*用來引用錨點。
下面是另一個例子。
~~~
- &showell Steve
- Clark
- Brian
- Oren
- *showell
~~~
轉為 JavaScript 代碼如下。
~~~
[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]
~~~
[1]:http://nodeca.github.io/js-yaml/
[2]:http://www.ruanyifeng.com/blog/2016/07/yaml.html
- 目錄
- ansible基礎
- ansible簡介
- ansible安裝和測試
- ansible配置文件
- 常用命令
- yaml在ansible中的用法
- inventory
- 變量與facts
- when語句
- handler模塊
- 大雜燴
- ansible模塊
- assert 模塊
- copy模塊
- cron模塊
- debug模塊
- django_manage模塊
- file模塊
- filesystem模塊
- git模塊
- hostname模塊
- lineinfile模塊
- mount模塊
- mysql_user模塊
- mysql_db模塊
- pip模塊
- selinux
- setup模塊
- shell 和 command 模塊
- stat模塊
- supervisorctl
- systemd
- timezone
- unarchive模塊
- user模塊
- wait_for
- yum和service模塊
- 其他模塊或者方法
- setup模塊
- url模塊
- slack 模塊
- pause 模塊
- 其他
- 報錯處理
- playbooks
- 復雜的playbook
- 循環
- roles
- YAML
- jinja2