2017-01-12 37 views
4

是否可以爲幾個任務定義一個notify塊?在Ansible中爲幾個任務定義一個通知塊

在下面的代碼片段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 

回答

6

不,你不能名單。

通知根據任務的狀態設置觸發器以運行指定的處理程序。 Ansible中沒有「任務塊狀態」,因此您無法爲塊定義notify

此外,它不會在功能上改變任何東西,只會影響視覺吸引力(並且我會通過模糊東西而不是簡化來主張)。無論有多少任務觸發它,處理程序只運行一次。

-2

一個小的解決方法,如果你想特別想要一個處理程序的三個任務。

在你的主要任務

- include: role2.yml 

在角色2

- 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 

- name: running handler for above tasks 
    debug: msg='running handler for all three' 
    notify: restart tomcat 
+1

處理程序絕不會與此代碼運行。包括任務也無濟於事。在解釋事物時不要使用混淆名稱 - 它與角色無關。 – techraf

+0

處理程序的想法是通知任何其操作需要處理程序運行的任務 - 例如,更改服務的配置或安裝新的服務代碼需要重新啓動服務。該解決方案打破了該模式。 – RichVel

+0

同意..只是故意有點黑客,沒有進攻 –

相關問題