【Ansible】03
任务块
block任务块
- 使用 block 可以将多个任务合并为一个组
- 可以将整个 block任务组 , 一起控制是否要执行
# 如果webservers组中的主机系统发行版是Rocky,则安装并启动nginx
[root@pubserver ansible]# vim block1.yml
---
- name: block tasks
hosts: webservers
tasks:
- name: define a group of tasks
block:
- name: install nginx # 通过yum安装nginx
yum:
name: nginx
state: present
- name: start nginx # 通过service启动nginx服务
service:
name: nginx
state: started
enabled: yes
when: ansible_distribution=="Rocky" # 条件为真才会执行上面的任务
[root@pubserver ansible]# ansible-playbook block1.yml
rescue 和 always
- block 组成任务组
- resuce 判定任务组中是否有失败组 ( failed ) , 失败则执行 resuce 中的任务
- always 不与任务组的成功与否关联 , 总是执行任务
[root@pubserver ansible]# vim block2.yml
---
- name: block test
hosts: webservers
tasks:
- name: block / rescue / always test1
block:
- name: touch a file
file:
path: /tmp/test1.txt
state: touch
rescue:
- name: touch file test2.txt
file:
path: /tmp/test2.txt
state: touch
always:
- name: touch file test3.txt
file:
path: /tmp/test3.txt
state: touch
# 执行playbook web1上将会出现/tmp/test1.txt和/tmp/test3.txt
[root@pubserver ansible]# ansible-playbook block2.yml
[root@web1 ~]# ls /tmp/test*.txt
/tmp/test1.txt /tmp/test3.txt
# 修改上面的playbook,使block中的任务出错
[root@web1 ~]# rm -f /tmp/test*.txt
[root@pubserver ansible]# vim block2.yml
---
- name: block test
hosts: webservers
tasks:
- name: block / rescue / always test1
block:
- name: touch a file
file:
path: /tmp/abcd/test11.txt
state: touch
rescue:
- name: touch file test22.txt
file:
path: /tmp/test22.txt
state: touch
always:
- name: touch file test33.txt
file:
path: /tmp/test33.txt
state: touch
# 因为web1上没有/tmp/abcd目录,所以block中的任务失败。但是playbook不再崩溃,而是执行rescue中的任务。always中的任务总是执行
[root@pubserver ansible]# ansible-playbook block2.yml
[root@web1 ~]# ls /tmp/test*.txt
/tmp/test22.txt /tmp/test33.txt
loop 循环
- 相当于 shell 中的 for循环
- ansible 中循环用到的变名名称是固定的 , item
# 在test组中的主机上创建5个目录/tmp/{aaa,bbb,ccc,ddd,eee}
[root@pubserver ansible]# vim loop1.yml
---
- name: use loop
hosts: webservers
tasks:
- name: create directory
file:
path: /tmp/{{item}}
state: directory
loop: [aaa,bbb,ccc,ddd,eee]
# 上面写法,也可以改为:
---
- name: use loop
hosts: webservers
tasks:
- name: create directory
file:
path: /tmp/{{item}}
state: directory
loop:
- aaa
- bbb
- ccc
- ddd
- eee
[root@pubserver ansible]# ansible-playbook loop1.yml
# 使用复杂变量。创建zhangsan用户,密码是123;创建lisi用户,密码是456
# item是固定的,用于表示循环中的变量
# 循环时,loop中每个-后面的内容作为一个整体赋值给item。
# loop中{}中的内容是自己定义的,写法为key:val
# 取值时使用句点表示。如下例中取出用户名就是{{item.uname}}
[root@pubserver ansible]# vim loop_user.yml
---
- name: create users
hosts: webservers
tasks:
- name: create multiple users
user:
name: "{{item.uname}}"
password: "{{item.upass|password_hash('sha512')}}"
loop:
- {"uname": "zhangsan", "upass": "123"}
- {"uname": "lisi", "upass": "456"}
[root@pubserver ansible]# ansible-playbook loop_user.yml
role 角色
- role角色 , 可以实现playbook重复使用
- 角色role相当于把任务打散 , 放到不同的目录中
- 再把一些固定的值 , 如用户名 , 软件包 , 服务等 , 用变量来表示
- role 角色定义好之后, 可以在其他playbook中直接调用
1. 书写不同内容到对应目录的 main.yml
1) 创建motd模板文件 [ roles/motd/templates/motd ]
[root@pubserver ansible]# vim roles/motd/templates/motd
Hostname: {{ansible_hostname}}
Date: {{ansible_date_time.date}}
Contact to: {{admin}}
2) 创建变量 [ roles/motd/vars/main.yml ]
[root@pubserver ansible]# vim roles/motd/vars/main.yml # 追加一行
admin: zzg@tedu.cn
3) 创建任务 [ roles/motd/tasks/main.yml ]
[root@pubserver ansible]# vim roles/motd/tasks/main.yml # 追加
- name: modify motd
template:
src: motd # 这里的文件,自动到templates目录下查找
dest: /etc/motd
2. 创建 playbook , 调用motd角色
[root@pubserver ansible]# vim role_motd.yml
---
- name: modify motd with role
hosts: webservers
roles:
- motd
3. 执行playbook
[root@pubserver ansible]# ansible-playbook role_motd.yml