任务块
1.可以通过block关键字,将多个任务组合到一起
2.可以将整个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和rescue、always联合使用:
1.block中的任务都成功,rescue中的任务不执行
2.block中的任务出现失败(failed),rescue中的任务执行
3.block中的任务不管怎么样,always中的任务总是执行
# 修改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中的任务总是执行
loop循环
1.相当于shell中for循环
2.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]
[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角色
1.为了实现playbook重用,可以使用role角色
2.角色role相当于把任务打散,放到不同的目录中
3.再把一些固定的值,如用户名、软件包、服务等,用变量来表示
4.role角色定义好之后,可以在其他playbook中直接调用
# 创建角色
# 1. 声明角色存放的位置
[root@pubserver ansible]# vim ansible.cfg
[defaults]
inventory = hosts
roles_path = roles # 定义角色存在当前目录的roles子目录中
# 2. 创建角色目录
[root@pubserver ansible]# mkdir roles
3. 创建名为motd的角色
[root@pubserver ansible]# ansible-galaxy init roles/motd
[root@pubserver ansible]# ls roles/
motd # 生成了motd角色目录
roles/motd/{
---defaults # 定义变量的目录,优先级最低
└── main.yml
---files # 保存上传的文件(如copy模块用到的文件)
--- handlers # handlers任务写到这个目录的main.yml中
└── main.yml
---meta # 保存说明数据,如角色作者、版本等
└── main.yml
--- README.md # 保存角色如何使用之类的说明
--- tasks # 保存任务
└── main.yml
--- templates # 保存template模块上传的模板文件
---tests # 保存测试用的playbook。可选
├── inventory
└── test.yml
---vars # 定义变量的位置,推荐使用的位置
└── main.yml
}
# 4. 将不同的内容分别写到对应目录的main.yml中
# 4.1 创建motd模板文件
[root@pubserver ansible]# vim roles/motd/templates/motd
Hostname: {{ansible_hostname}}
Date: {{ansible_date_time.date}}
Contact to: {{admin}}
# 4.2 创建变量
[root@pubserver ansible]# vim roles/motd/vars/main.yml # 追加一行
admin: zzg@tedu.cn
# 4.3 创建任务
[root@pubserver ansible]# vim roles/motd/tasks/main.yml # 追加
- name: modify motd
template:
src: motd # 这里的文件,自动到templates目录下查找
dest: /etc/motd
# 5. 创建playbook,调用motd角色
[root@pubserver ansible]# vim role_motd.yml
---
- name: modify motd with role
hosts: webservers
roles:
- motd
# 6. 执行playbook
[root@pubserver ansible]# ansible-playbook role_motd.yml