1.handlers触发器
使用场景:ansible会对每一部分进行监控,只有当数据改变时,absible才会执行,但是重启是不管配置文件是否发生变动在剧本执行的时候都会对服务重启一次,因此需要对配置文件进行检测,只有当配置文件发生改变时,服务才会重启,因此需要使用到handlers触发器。
#修改剧本对需要重启的服务加上handlers触发器
[root@master-61 /server]# vim install_nfs.yaml
[root@master-61 /server]# cat install_nfs.yaml
- hosts: nfs
tasks:
- name: 01 通过yum安装nfs-utils和rpcbind
yum:
name: nfs-utils,rpcbind
state: present
- name: 02 发送本地配置文件
copy:
src: /etc/exports
dest: /etc/exports
notify:
- 重启nfs服务
- name: 03 创建目录,更改目录所有者
file:
path: /nfs-data
state: directory
owner: nfsnobody
group: nfsnobody
- name: 04 启动rpcbind服务
systemd:
name: rpcbind
enabled: yes
state: started
- name: 05 启动nfs服务
systemd:
name: nfs
enabled: yes
state: started
handlers:
- name: 重启nfs服务
systemd:
name: nfs
state: restarted
- hosts: web
tasks:
- name: 01 客户端安装nfs-utils
yum:
name: nfs-utils
state: present
- name: 02 挂载目录
mount:
src: 172.16.1.31:/nfs-data
path: /nfs-test
fstype: nfs
state: mounted
#修改nfs配置文件,执行剧本,看是否可以触发handlers
[root@master-61 /server]# vim /etc/exports
[root@master-61 /server]# cat /etc/exports
#共享目录1
/nfs-data 172.16.1.0/24(rw,all_squash)
[root@master-61 /server]# ansible-playbook install_nfs.yaml
PLAY [nfs] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.31]
TASK [01 通过yum安装nfs-utils和rpcbind] ***********************************************************************************************
ok: [172.16.1.31]
TASK [02 发送本地配置文件] ***************************************************************************************************************
changed: [172.16.1.31]
TASK [03 创建目录,更改目录所有者] ***********************************************************************************************************
ok: [172.16.1.31]
TASK [04 启动rpcbind服务] ************************************************************************************************************
ok: [172.16.1.31]
TASK [05 启动nfs服务] ****************************************************************************************************************
ok: [172.16.1.31]
RUNNING HANDLER [重启nfs服务] ********************************************************************************************************
ok: [172.16.1.31]
PLAY [web] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.9]
ok: [172.16.1.7]
ok: [172.16.1.8]
TASK [01 客户端安装nfs-utils] *********************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.9]
ok: [172.16.1.8]
TASK [02 挂载目录] *******************************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.9]
ok: [172.16.1.8]
PLAY RECAP ***********************************************************************************************************************
172.16.1.31 : ok=7 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.9 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.when判断,在运行ansible剧本时,加上when变量,可以让对应模块在条件满足或者条件不满足时运行
#让nfs-31打印我是nfs-31机器
[root@master-61 /server]# vim echo_31.yaml
[root@master-61 /server]# cat echo_31.yaml
- hosts: all
tasks:
- name: 打印
debug:
msg: "我是nfs-31机器"
when: ansible_hostname=="nfs-31"
[root@master-61 /server]# ansible-playbook echo_31.yaml
PLAY [all] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.9]
ok: [172.16.1.8]
ok: [172.16.1.7]
ok: [172.16.1.31]
ok: [192.168.186.129]
TASK [打印] ************************************************************************************************************************
skipping: [172.16.1.7]
skipping: [172.16.1.8]
skipping: [172.16.1.9]
ok: [172.16.1.31] => {
"msg": "我是nfs-31机器"
}
skipping: [192.168.186.129]
PLAY RECAP ***********************************************************************************************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.8 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.16.1.9 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.186.129 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
3.循环loop,用于批量创建文件,批量添加用户,批量启动服务
#利用loop循环批量创建文件
[root@master-61 /server]# vim create_dir.yaml
[root@master-61 /server]# cat create_dir.yaml
- hosts: server
tasks:
- name: 创建文件
file:
path: "{{ item }}"
state: directory
loop:
- /1.txt
- /1.log
- /etc/test.txt
[root@master-61 /server]# ansible-playbook create_dir.yaml
PLAY [server] ********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [192.168.186.129]
TASK [创建文件] **********************************************************************************************************************
changed: [192.168.186.129] => (item=/1.txt)
changed: [192.168.186.129] => (item=/1.log)
changed: [192.168.186.129] => (item=/etc/test.txt)
PLAY RECAP ***********************************************************************************************************************
192.168.186.129 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.剧本调试
-C 模拟运行,查看运行结果,实际上并没有发生改变
[root@master-61 /server]# ansible-playbook -C install_nfs.yaml
PLAY [nfs] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.31]
TASK [01 通过yum安装nfs-utils和rpcbind] ***********************************************************************************************
ok: [172.16.1.31]
TASK [02 发送本地配置文件] ***************************************************************************************************************
ok: [172.16.1.31]
TASK [03 创建目录,更改目录所有者] ***********************************************************************************************************
ok: [172.16.1.31]
TASK [04 启动rpcbind服务] ************************************************************************************************************
ok: [172.16.1.31]
TASK [05 启动nfs服务] ****************************************************************************************************************
ok: [172.16.1.31]
PLAY [web] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.9]
ok: [172.16.1.8]
TASK [01 客户端安装nfs-utils] *********************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
ok: [172.16.1.9]
TASK [02 挂载目录] *******************************************************************************************************************
ok: [172.16.1.9]
ok: [172.16.1.8]
ok: [172.16.1.7]
PLAY RECAP ***********************************************************************************************************************
172.16.1.31 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.9 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
--syntax-check 只做语法检查,不运行
[root@master-61 /server]# ansible-playbook --syntax-check install_nfs.yaml
playbook: install_nfs.yaml
--step 单步运行,y执行,n忽略,c自动运行
[root@master-61 /server]# ansible-playbook --step install_nfs.yaml
PLAY [nfs] ***********************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: ***********************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.31]
Perform task: TASK: 01 通过yum安装nfs-utils和rpcbind (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 01 通过yum安装nfs-utils和rpcbind (N)o/(y)es/(c)ontinue: ***********************************************************
Perform task: TASK: 02 发送本地配置文件 (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 02 发送本地配置文件 (N)o/(y)es/(c)ontinue: ***************************************************************************
Perform task: TASK: 03 创建目录,更改目录所有者 (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 03 创建目录,更改目录所有者 (N)o/(y)es/(c)ontinue: ***********************************************************************
Perform task: TASK: 04 启动rpcbind服务 (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 04 启动rpcbind服务 (N)o/(y)es/(c)ontinue: ************************************************************************
Perform task: TASK: 05 启动nfs服务 (N)o/(y)es/(c)ontinue: c
Perform task: TASK: 05 启动nfs服务 (N)o/(y)es/(c)ontinue: ****************************************************************************
TASK [05 启动nfs服务] ****************************************************************************************************************
ok: [172.16.1.31]
PLAY [web] ***********************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: n
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: ***********************************************************************
Perform task: TASK: 01 客户端安装nfs-utils (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 01 客户端安装nfs-utils (N)o/(y)es/(c)ontinue: *********************************************************************
Perform task: TASK: 02 挂载目录 (N)o/(y)es/(c)ontinue: n
Perform task: TASK: 02 挂载目录 (N)o/(y)es/(c)ontinue: *******************************************************************************
PLAY RECAP ***********************************************************************************************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.tags标签
运行剧本的时候指定tags标签或者排除某些tags标签
[root@master-61 /server]# vim install_nfs.yaml
[root@master-61 /server]# cat install_nfs.yaml |head -10
- hosts: nfs
tasks:
- name: 01 通过yum安装nfs-utils和rpcbind
yum:
name: nfs-utils,rpcbind
state: present
tags:
- 安装
- name: 02 发送本地配置文件
copy:
[root@master-61 /server]# ansible-playbook --tags 安装 install_nfs.yaml
PLAY [nfs] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.31]
TASK [01 通过yum安装nfs-utils和rpcbind] ***********************************************************************************************
ok: [172.16.1.31]
PLAY [web] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
ok: [172.16.1.9]
PLAY RECAP ***********************************************************************************************************************
172.16.1.31 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.7 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.9 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6.忽略错误,在剧本执行时某些文件已经存在,就会报错,可以修改剧本来忽略这些错误
ignore_errors: true
#执行create_dir.yaml剧本
[root@master-61 /server]# vim create_dir.yaml
[root@master-61 /server]# cat create_dir.yaml
- hosts: server
tasks:
- name: 创建文件
file:
path: "{{ item }}"
state: directory
ignore_errors: true
loop:
- /1.txt
- /1.log
- /etc/test.txt
[root@master-61 /server]# ansible-playbook create_dir.yaml
PLAY [server] ********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [192.168.186.129]
TASK [创建文件] **********************************************************************************************************************
ok: [192.168.186.129] => (item=/1.txt)
ok: [192.168.186.129] => (item=/1.log)
ok: [192.168.186.129] => (item=/etc/test.txt)
PLAY RECAP ***********************************************************************************************************************
192.168.186.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0