2017-04-02 76 views
0

我有一個需要在centos 7和centos 6上運行的apache playbook。我希望基於分發主版本觸發處理程序。我有一個名爲restart apache on 7的處理程序和另一個restart apache on 6使用if語句的Ansible處理程序

handlers/main.yml看起來像這樣

--- 
- name: restart apache on 7 
    systemd: name=httpd state=restarted 

- name: restart apache on 6 
    service: name=httpd state=restarted 

我試圖做我tasks/main.yml以下,但似乎遇到了有關的通知腳本語法問題。

- name: Copy custom configs 
    copy: 
    dest: /etc/httpd/conf/ 
    src: httpd.conf 
    owner: root 
    notify: {%if ansible_distribution_major_version >= '7.0' %}restart apache on 7 {%else%} restart apache on 6 {%endif%} 

如何寫通知語句來實現我的目標任何線索?

回答

1

對於一個通用的解決方案,你可以使用topics,但我不知道你爲什麼會想使用systemd模塊,而不是service - 後者應支付的CentOS 6和7沒有任何的區別。

還可以使用ansible_distribution_major_version的算術比較來替代字符串比較,如您的示例中所示。


處理程序聽主題:

--- 
- name: restart apache on 7 
    systemd: name=httpd state=restarted 
    when: ansible_distribution_major_version >= 7 
    listen: "restart apache" 

- name: restart apache on 6 
    service: name=httpd state=restarted 
    when: ansible_distribution_major_version < 7 
    listen: "restart apache" 

和任務,通知他們:

- name: Copy custom configs 
    copy: 
    dest: /etc/httpd/conf/ 
    src: httpd.conf 
    owner: root 
    notify: "restart apache"