yaml的字符串,字典和列表写法

更新时间:2023-05-28 08:22:41 阅读: 评论:0

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

本文发布于:2023-05-28 08:22:41,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/125541.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   空格   引号   写法   字典   开头   测试   转义
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图