第 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命令才会被执行。

版权声明:
作者:WaterBear
链接:https://l-t.top/2020.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录