{"id":561,"date":"2015-09-17T22:38:22","date_gmt":"2015-09-17T14:38:22","guid":{"rendered":"http:\/\/www.ipcpu.com\/?p=561"},"modified":"2015-09-17T22:38:22","modified_gmt":"2015-09-17T14:38:22","slug":"ansible-notify-handlers","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2015\/09\/ansible-notify-handlers\/","title":{"rendered":"Ansible\u5165\u95e8notify\u548chandlers"},"content":{"rendered":"

notify<\/h2>\n

notify\u8fd9\u4e2aaction\u53ef\u7528\u4e8e\u5728\u6bcf\u4e2aplay\u7684\u6700\u540e\u88ab\u89e6\u53d1\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u6709\u6539\u53d8\u53d1\u751f\u65f6\u6bcf\u6b21\u90fd\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\uff0c\u53d6\u800c\u4ee3\u4e4b\uff0c\u4ec5\u5728\u6240\u6709\u7684\u53d8\u5316\u53d1\u751f\u5b8c\u6210\u540e\u4e00\u6b21\u6027\u5730\u6267\u884c\u6307\u5b9a\u64cd\u4f5c\u3002<\/p>\n

\u5728notify\u4e2d\u5217\u51fa\u7684\u64cd\u4f5c\u79f0\u4e3ahandler\uff0c\u4e5f\u5373notify\u4e2d\u8c03\u7528handler\u4e2d\u5b9a\u4e49\u7684\u64cd\u4f5c\u3002<\/p>\n

  1. ---<\/span><\/code><\/li>
  2. -<\/span> name<\/span>:<\/span> test<\/span>.<\/span>yml just <\/span>for<\/span> test<\/span><\/code><\/li>
  3. hosts<\/span>:<\/span> testserver<\/span><\/code><\/li>
  4. vars<\/span>:<\/span><\/code><\/li>
  5. region<\/span>:<\/span> ap<\/span>-<\/span>southeast<\/span>-<\/span>1<\/span><\/code><\/li>
  6. tasks<\/span>:<\/span><\/code><\/li>
  7. <\/span>-<\/span> name<\/span>:<\/span> <\/span>template<\/span> configuration file<\/span><\/code><\/li>
  8. <\/span>template<\/span>:<\/span> src<\/span>=<\/span>template<\/span>.<\/span>j2 dest<\/span>=<\/span>\/etc\/<\/span>foo<\/span>.<\/span>conf<\/span><\/code><\/li>
  9. notify<\/span>:<\/span><\/code><\/li>
  10. <\/span>-<\/span> restart memcached<\/span><\/code><\/li>
  11. <\/span>-<\/span> restart apache<\/span><\/code><\/li>
  12. <\/code><\/li>
  13. handlers<\/span>:<\/span><\/code><\/li>
  14. <\/span>-<\/span> name<\/span>:<\/span> restart memcached<\/span><\/code><\/li>
  15. service<\/span>:<\/span> name<\/span>=<\/span>memcached state<\/span>=<\/span>restarted<\/span><\/code><\/li>
  16. <\/span>-<\/span> name<\/span>:<\/span> restart apache<\/span><\/code><\/li>
  17. service<\/span>:<\/span> name<\/span>=<\/span>apache state<\/span>=<\/span>restarted<\/span><\/code><\/li><\/ol><\/pre>\n

    handlers<\/h2>\n

    Handlers \u4e5f\u662f\u4e00\u4e9b task \u7684\u5217\u8868,\u901a\u8fc7\u540d\u5b57\u6765\u5f15\u7528,\u5b83\u4eec\u548c\u4e00\u822c\u7684 task \u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\u3002<\/p>\n

    Handlers \u662f\u7531\u901a\u77e5\u8005\u8fdb\u884c notify, \u5982\u679c\u6ca1\u6709\u88ab notify,handlers \u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

    \u4e0d\u7ba1\u6709\u591a\u5c11\u4e2a\u901a\u77e5\u8005\u8fdb\u884c\u4e86 notify,\u7b49\u5230 play \u4e2d\u7684\u6240\u6709 task \u6267\u884c\u5b8c\u6210\u4e4b\u540e,handlers \u4e5f\u53ea\u4f1a\u88ab\u6267\u884c\u4e00\u6b21\u3002<\/p>\n

    Handlers \u6700\u4f73\u7684\u5e94\u7528\u573a\u666f\u662f\u7528\u6765\u91cd\u542f\u670d\u52a1,\u6216\u8005\u89e6\u53d1\u7cfb\u7edf\u91cd\u542f\u64cd\u4f5c.\u9664\u6b64\u4ee5\u5916\u5f88\u5c11\u7528\u5230\u4e86\u3002<\/p>\n

    1. ---<\/span><\/code><\/li>
    2. -<\/span> name<\/span>:<\/span> test<\/span>.<\/span>yml just <\/span>for<\/span> test<\/span><\/code><\/li>
    3. hosts<\/span>:<\/span> testserver<\/span><\/code><\/li>
    4. vars<\/span>:<\/span><\/code><\/li>
    5. region<\/span>:<\/span> ap<\/span>-<\/span>southeast<\/span>-<\/span>1<\/span><\/code><\/li>
    6. tasks<\/span>:<\/span><\/code><\/li>
    7. <\/span>-<\/span> name<\/span>:<\/span> check memory free<\/span><\/code><\/li>
    8. <\/span>template<\/span>:<\/span> src<\/span>=<\/span>template1<\/span>.<\/span>j2 dest<\/span>=<\/span>\/etc\/<\/span>foo<\/span>.<\/span>conf<\/span><\/code><\/li>
    9. notify<\/span>:<\/span> restart apache<\/span><\/code><\/li>
    10. <\/code><\/li>
    11. <\/span>-<\/span> name<\/span>:<\/span> check memory free<\/span><\/code><\/li>
    12. <\/span>template<\/span>:<\/span> src<\/span>=<\/span>template2<\/span>.<\/span>j2 dest<\/span>=<\/span>\/etc\/<\/span>okk<\/span>.<\/span>conf<\/span><\/code><\/li>
    13. notify<\/span>:<\/span> restart apache<\/span><\/code><\/li>
    14. <\/code><\/li>
    15. <\/code><\/li>
    16. handlers<\/span>:<\/span><\/code><\/li>
    17. <\/span>-<\/span> name<\/span>:<\/span> restart apache<\/span><\/code><\/li>
    18. service<\/span>:<\/span> name<\/span>=<\/span>apache state<\/span>=<\/span>restarted<\/span><\/code><\/li>
    19. <\/code><\/li>
    20. #\/\/@Apache\u53ea\u4f1a\u91cd\u542f\u4e00\u6b21<\/span><\/code><\/li>
    21. #\/\/@handlers\u9700\u8981\u7b49\u5230\u6240\u6709tasks\u5b8c\u6210\u540e\u624d\u6267\u884c<\/span><\/code><\/li><\/ol><\/pre>\n

      \u53c2\u8003\u94fe\u63a5<\/h2>\n

      http:\/\/ansible-tran.readthedocs.org\/en\/latest\/docs\/playbooks_intro.html<\/a>
      \n
      http:\/\/www.21ops.com\/ops\/automate-ops\/26711.html<\/a><\/p>\n

      \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Ansible\u5165\u95e8notify\u548chandlers<\/a><\/p>","protected":false},"excerpt":{"rendered":"

      notify notify\u8fd9\u4e2aaction\u53ef\u7528\u4e8e\u5728\u6bcf\u4e2aplay\u7684\u6700\u540e\u88ab\u89e6\u53d1\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u591a\u6b21\u6709\u6539\u53d8\u53d1\u751f\u65f6\u6bcf\u6b21\u90fd\u6267\u884c\u6307\u5b9a\u7684\u64cd\u4f5c\uff0c\u53d6\u800c\u4ee3\u4e4b\uff0c\u4ec5\u5728\u6240\u6709\u7684\u53d8\u5316\u53d1\u751f\u5b8c\u6210\u540e\u4e00\u6b21\u6027\u5730\u6267\u884c\u6307\u5b9a\u64cd\u4f5c\u3002 \u5728notify\u4e2d\u5217\u51fa\u7684\u64cd\u4f5c\u79f0\u4e3ahandler\uff0c\u4e5f\u5373notify\u4e2d\u8c03\u7528handler\u4e2d\u5b9a\u4e49\u7684\u64cd\u4f5c\u3002 —- 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 \u4e5f\u662f\u4e00\u4e9b task \u7684\u5217\u8868,\u901a\u8fc7\u540d\u5b57\u6765\u5f15\u7528,\u5b83\u4eec\u548c\u4e00\u822c\u7684 task \u5e76\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\u3002 Handlers \u662f\u7531\u901a\u77e5\u8005\u8fdb\u884c notify, […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[45,17],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/561"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=561"}],"version-history":[{"count":0,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/561\/revisions"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}