在Ansible中为多个任务定义一个通知块

pop*_*lka 7 ansible

是否可以notify为多个任务定义一个块?

在下一个代码片段notify: restart tomcat定义了3次,但我想只定义一次并"应用"到任务列表

- name : template context.xml
  template:
    src: context.xml.j2
    dest: /usr/share/tomcat/conf/context.xml
    group: tomcat
    mode: 0664
  notify: restart tomcat

- name : copy server.xml
  copy:
    src: server.xml
    dest: /etc/tomcat/server.xml
    group: tomcat
    mode: 0664
  notify: restart tomcat

- name : copy atomikos-integration-extension
  copy:
    src: atomikos-integration-extension-3.7.1-20120529.jar
    dest: /usr/share/tomcat/ext-libs/
    group: tomcat
    mode: 0664
  notify: restart tomcat
Run Code Online (Sandbox Code Playgroud)

tec*_*raf 12

你不能.

Notify设置触发器以根据任务的状态运行指定的处理程序.Ansible中没有"任务块的状态",因此您无法定义notify块.

此外,它不会在功能上改变任何东西,只影响视觉吸引力(我会通过模糊事物而不是简化来声称).无论触发多少任务,处理程序只运行一次.

  • 嗯,我知道如果任何任务发生变化,那么块就会发生变化。可惜ansible不支持这个 (5认同)
  • 现在可以了,请参阅 https://github.com/ansible/ansible/pull/73572/commits - 从 Ansible v2.11 开始 具体来说,此提交详细介绍了测试块的更改 https://github.com/ansible/ansible /拉/73572/提交/899f39800cb74e0e38010aba30087672819b2097 (4认同)

Wes*_*Wes 7

从 Ansible v2.11 开始(https://github.com/ansible/ansible/blame/stable-2.11/changelogs/CHANGELOG-v2.11.rst#L599

现在可以在块上设置通知,并 import_tasks https://github.com/ansible/ansible/pull/73572/commits/899f39800cb74e0e38010aba30087672819b2097

因此,用一个块包装您的任务并在块上设置通知!


小智 3

正如上面已经提到的,现在可以在块上使用通知。
请确保不要忘记您的处理程序。

- block:
  - name : template context.xml
    template:
      src: context.xml.j2
      dest: /usr/share/tomcat/conf/context.xml
      group: tomcat
      mode: 0664
  - name : copy server.xml
    copy:
      src: server.xml
      dest: /etc/tomcat/server.xml
      group: tomcat
      mode: 0664
  - name : copy atomikos-integration-extension
    copy:
      src: atomikos-integration-extension-3.7.1-20120529.jar
      dest: /usr/share/tomcat/ext-libs/
      group: tomcat
      mode: 0664
  notify: restart tomcat
Run Code Online (Sandbox Code Playgroud)