ansible学习之playbook剧本编写

Idonia ·
更新时间:2024-09-21
· 994 次阅读

yaml语法 hosts: web
remote_user: root
task: name: task1
yum: name=nginx state=present
开头必须 —
语法缩进为两个空格
所有键值对都以字典形式写,格式固定为冒号+空格
每个playbook中的play 都必须包含 hosts 和 tasks两个项 列表

使用“-”作为列表项,一个“-”代表一个任务,某项是一个动作,一个对象,或一个实体时,都加‘-’
例1:

zhangsan lisi wangwu
例2:
[zhangsan,lisi,wangwu]
例3:
[
“zhangsan”,
“lisi”,
“wangwu”
]
例4: 班名: 初中一班
人数:35
班主任:张三 字典

key: value

在playbook 中,一般“虚拟性”的内容都可以通过字典的方式书写,实体化,动作性的,对象性的内容,定义为列表

hosts: localhost #列表1
remote_user: root
task: name: test1 #子列表
shell: echo /tmp/a.txt
register: hi_var debug: var=hi_var.stdout #调用模块,为动作,列表 include: /tmp/nginx.yml #包含文件,即为动作 include: /tmp/mysql.yml copy: #调用模块,列表,参数为虚拟性内容,即为字典
src: /etc/resolv.conf
dest: /tmp 多行写 三种方式:
1.在“key: ”后面使用大于号
- hosts: localhost tasks: - shell: > echo 2 >> /tmp/test.txt creates=/tmp/haha.txt
2.在“key: ”后面使用管道符
- hosts: localhost - tasks: - shell: | echo 2 >>/tmp/test.txt creates=/tmp/haha.txt 3.多层缩进 - hosts: localhost tasks: - shell: echo 2 >>/tmp/test.txt creates=/tmp/haha.txt 模块传递参数的方式

1.直接写在模块后

yum: name=nginx state=present
2.写成字典模式 yum:
name: nginx
state:present
3.内置属性args多层缩进定义参数列表 yum:
args:
name: vim
state: present playbook 和play的关系

一个playbook中可以包含多个play。每个play都至少包含有tasks和hosts这两项,还可以包含其他非必须项,如vars,vars_files,remote_user等。tasks中可以通过模块调用定义一系列的action。只不过,绝大多数时候,一个playbook都只定义一个play。
所以,大致关系为:

playbook:[play1,play2…]
play:[hosts,tasks,remote_user,vars…]
tasks:[module1,module2…]

每个顶级列表都是一个play

需要注意,有些时候play中使用了role,可能看上去没有tasks,这是因为role本身就是整合playbook的,所以没有也没关系。但没有使用role的时候,必须得包含hosts和tasks。例如:

hosts: centos remote_user: root pre_tasks: - name: config the yum repo for centos 7 yum_repository: name: epel description: epel baseurl: http://mirrors.aliyun.com/epel/7/$basearch/ gpgcheck: no when: ansible_distribution_major_version == "7" - name: config the yum repo for centos 6 yum_repository: name: epel description: epel baseurl: http://mirrors.aliyun.com/epel/6/$basearch/ gpgcheck: no when: ansible_distribution_major_version == "6" roles: - nginx post_tasks: - shell: echo 'deploy nginx/mysql over' register: ok_var - debug: msg='{{ ok_var.stdout }}' playbook中什么时候使用引号

playbook中定义的都是些列表和字典。绝大多数时候,都不需要使用引号,但有两个特殊情况需要考虑使用引号。
• 出现大括号"{}"。
• 出现冒号加空格时": "。
大括号要使用引号包围,是因为不使用引号时会被yaml解析成内联字典。例如要使用大括号引用变量的时候,以及想输出大括号符号的时候。

- hosts: localhost tasks: - shell: echo "{{inventory_hostname}}:haha"

冒号尾随空格时要使用引号包围,是因为它会被解析为"key: value"的形式。而且包围冒号的引号还更严格。例如下面的debug模块中即使使用了引号也是错误的。

- hosts: localhost tasks: - shell: echo "{{inventory_hostname}}:haha" register: hello - debug: msg="{{hello.stdout}}: heihei"

因为它把{{…}}当成key,heihei当成value了。因此,必须将整个debug模块的参数都包围起来,显式指定这一段是模块的参数。但这样会和原来的双引号冲突,因此使用单引号。

- hosts: localhost tasks: - shell: echo "{{inventory_hostname}}:haha" register: hello - debug: 'msg="{{hello.stdout}}: heihei"'

但是,如果将shell模块中的冒号后也尾随上空格,即写成echo “{{inventory_hostname}}: haha”,那么shell模块也会报错。因此也要使用多个引号,正确的如下:

- hosts: localhost tasks: - shell: 'echo "{{inventory_hostname}}: haha"' register: hello - debug: 'msg="{{hello.stdout}}: heihei"'
作者:weixin_38163225



playbook ansible

需要 登录 后方可回复, 如果你还没有账号请 注册新账号