yaml的字符串,字典和列表写法yaml的字符串,字典和列表写法
配置vim的yaml格式友好⽀持
# 创建vimrc
vim ~/.vimrc
# 插⼊yaml配置
## ai = auto indent ⾃动退格对齐
## ts=tabstop,即⼀个tab的宽带设定为2个空格
## sw=shiftwidth,即退格对齐以两个空格为准
## et=expandtab,将tab转换位空格
autocmd FileType yaml tlocal ai ts=2sw=2 et
# 配置完下次会直接⽣效
yaml的开头和结尾
yaml⽂件开头三个杠,结束三个点
---
yaml内容
...
yaml的字符串
字符串引号与不加引号
yaml中的字符串加不加引号效果都是⼀样的
单引号可以避免转义符错误
yaml中不允许在双引号中出现转义符号,所以都是单引号来避免转义符错误
ansible单引号内容全是字符串,双引号⾥⾯可以带变量
---
-name:
hosts: all
tasks:
-name:
debug:
msg:不加引号字符串测试
-debug:
msg:'单引号字符串测试'
-debug:
msg:"双引号字符串测试"
运⾏结果: 字符串加不加引号运⾏效果都是⼀样的。
[student@workstation gzy]$ l
PLAY [all] *********************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [rvera]
TASK [debug] *******************************************************************************************
ok: [rvera]=>{
"msg":"不加引号字符串测试"
}
TASK [debug] *******************************************************************************************
ok: [rvera]=>{
"msg":"单引号字符串测试"
hdt}
TASK [debug] *******************************************************************************************
it名词
ok: [rvera]=>{
"msg":"双引号字符串测试"
}
PLAY RECAP *********************************************************************************************
rvera :ok=4changed=0unreachable=0failed=0skipped=0rescued=0ignored=0字符串换⾏
如果⽂本需要换⾏,在⾏⾸加竖线|
martina hill
---
-name:字符串演⽰
dataforthhosts: all
tasks:
-name:字符串换⾏演⽰
debug:
msg:|
字符串
换⾏
演⽰
运⾏效果
[student@workstation gzy]$ l
PLAY [字符串演⽰] *******************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [rvera]
TASK [字符串换⾏演⽰] *****************************************************************************************
ok: [rvera]=>{
"msg":"字符串\n换⾏\n演⽰\n"
}
PLAY RECAP *********************************************************************************************
rvera :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0字符串连⾏
字符串连⾏显⽰,需要在⾏⾸加⼤于号>
---
-name:字符串演⽰3
hosts: all
tasks:
-name:字符串连⾏演⽰
debug:
msg:>
连
⾏
演
⽰
运⾏效果
[student@workstation gzy]$ l
asiansPLAY [字符串演⽰3] ******************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [rvera]
工作室英文
TASK [字符串连⾏演⽰] *****************************************************************************************
ok: [rvera]=>{
"msg":"连⾏演⽰\n"
}
PLAY RECAP *********************************************************************************************
rvera :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0 yaml的字典
和python定义字典很像,两种写法效果都⼀样的
多⾏定义字典:ansible推荐的写法
name:安装httpd
svcrvice: httpd
svcport:80
⼀⾏定义字典:python风格写法
明器{name:安装httpd,svcrvice: httpd,svcport:80}
中文系考研yaml的列表
yaml的列表,可以单⾏也可以多⾏
单⾏写法需要加-
多⾏写法以⽅括号扩起来,和python的列表⽅法⼀样,但不需要加引号
多⾏列表演⽰:ansible推荐的写法
---
-name:列表演⽰
hosts:
- rvera
- rverb
- rverc
tasks:
-name:多⾏列表演⽰
debug:
msg:多⾏列表演⽰
运⾏效果
[student@workstation gzy]$ l
PLAY [列表演⽰] ********************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [rvera]
ok: [rverc]
ok: [rverb]
TASK [多⾏列表演⽰] ******************************************************************************************
ok: [rvera]=>{
"msg":"多⾏列表演⽰"
}
ok: [rverb]=>{
"msg":"多⾏列表演⽰"
}
ok: [rverc]=>{
"msg":"多⾏列表演⽰"
}
PLAY RECAP *********************************************************************************************
rvera :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0 rverb :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0 rverc :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0
单⾏列表演⽰:python风格写法
---
-name:列表演⽰
hosts:[rvera,rverb,rverc]
tasks:
-name:单⾏列表演⽰
debug:
msg:单⾏列表演⽰
运⾏效果演⽰
[student@workstation gzy]$ l
PLAY [列表演⽰] ********************************************************************************************
TASK [Gathering Facts] *********************************************************************************
ok: [rverc]
ok: [rvera]
节气门清洗
ok: [rverb]
TASK [单⾏列表演⽰] ******************************************************************************************
ok: [rvera]=>{
"msg":"单⾏列表演⽰"
}
ok: [rverb]=>{
"msg":"单⾏列表演⽰"
}
ok: [rverc]=>{
新视野大学英语3"msg":"单⾏列表演⽰"
}
PLAY RECAP *********************************************************************************************
rvera :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0 rverb :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0 rverc :ok=2changed=0unreachable=0failed=0skipped=0rescued=0ignored=0