阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ansible入门notify和handlers

168次阅读
没有评论

共计 1919 个字符,预计需要花费 5 分钟才能阅读完成。

简单记录下 Ansible 入门基础知识 notify 和 handlers。

notify
notify 这个 action 可用于在每个 play 的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。

在 notify 中列出的操作称为 handler,也即 notify 中调用 handler 中定义的操作。

—- name: test.yml just for test 
    hosts: testserver 
    vars:   
        region: ap-southeast-1 
    tasks:   
        – name: template configuration
          file      template: src=template.j2 dest=/etc/foo.conf     
    notify:         
        – restart memcached         
        – restart apache 
    handlers:   
          – name: restart memcached     
            service: name=memcached state=restarted   
          – name: restart apache     
            service: name=apache state=restarted

handlers
Handlers 也是一些 task 的列表, 通过名字来引用, 它们和一般的 task 并没有什么区别。

Handlers 是由通知者进行 notify, 如果没有被 notify,handlers 不会执行。

不管有多少个通知者进行了 notify, 等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次。

Handlers 最佳的应用场景是用来重启服务, 或者触发系统重启操作. 除此以外很少用到了。

—- name: test.yml just for test 
    hosts: testserver 
    vars:    region: ap-southeast-1 
    tasks:   
    – name: check memory free     
      template: src=template1.j2 dest=/etc/foo.conf     
      notify: restart apache   
    – name: check memory free     
      template: src=template2.j2 dest=/etc/okk.conf     
      notify: restart apache  handlers:   
    – name: restart apache     
      service: name=apache state=restarted#//@Apache 只会重启一次 #//@handlers 需要等到所有 tasks 完成后才执行

下面关于 Ansible 的文章您也可能喜欢,不妨参考下:

使用 Ansible 批量管理远程服务器  http://www.linuxidc.com/Linux/2015-05/118080.htm

Ansible 安装配置与简单使用  http://www.linuxidc.com/Linux/2015-07/120399.htm

在 CentOS 7 中安装并使用自动化工具 Ansible  http://www.linuxidc.com/Linux/2015-10/123801.htm

Ansible 和 Docker 的作用和用法  http://www.linuxidc.com/Linux/2014-11/109783.htm

CentOS 7 上搭建 Jenkins+Ansible 服务  http://www.linuxidc.com/Linux/2016-12/138737.htm

Ansible 批量搭建 LAMP 环境 http://www.linuxidc.com/Linux/2014-10/108264.htm

Ansible:一个配置管理和 IT 自动化工具  http://www.linuxidc.com/Linux/2014-11/109365.htm

Ansible 基础—安装与常用模块  http://www.linuxidc.com/Linux/2017-02/140216.htm

自动化运维工具之 Ansible 介绍及安装使用  http://www.linuxidc.com/Linux/2016-12/138104.htm

Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140871.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计1919字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中