第 5.3 章 Playbook基本使用-在变更时执行操作(handlers)
Playbook基本使用-在变更时执行操作(handlers)
简而言之:当你进行变更操作时(简单来说就是修改了某个服务配置文件后),触发另一个操作进程(比如修改服务配置后重启服务)
举例:
#conf.yml
---
- hosts: web
gather_facts: no
remote_user: root
tasks:
- name: copy nginx configuration file
copy:
src: site.conf
dest: /etc/nginx/conf.d
- name: reload nginx
service: name=nginx state=reloaded
#站点配置文件
vim site.conf
#site.conf
server {
listen 80;
server_name test.ctnrs.com;
location / {
root /var/www/html;
index index.html;
}
}
那么问题来了,当我们需要更新的配置文件未发生改变情况下,依然执行了Nginx reload 操作。
我们想配置变更时候才执行Nginx ereload 操作,这时我们引进 handlers 和 notify。更改后如下:
#conf.yml --- - hosts: web gather_facts: no remote_user: root tasks: - name: copy nginx configuration file copy: src: site.conf dest: /etc/nginx/conf.d notify:#与copy模块同级 - reload nginx #已用名称要与handlers中的name名称相同。 handlers: - name: reload nginx service: name=nginx state=reloaded
当执行copy任务状态为change 时,才会执行reload nginx操作。
换而言之,但你更改了site.conf文件内容后,Nginx reload命令才会被执行。
文章目录
关闭
