手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Ansible入门notify和handlers

[日期:2017-02-21] 来源:Linux社区  作者:Linux [字体: ]

简单记录下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

linux
相关资讯       ansible  Notify  handlers 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款