2017-04-02 77 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"