【问题标题】:Ansible handler using if statements使用 if 语句的 Ansible 处理程序
【发布时间】:2017-04-02 14:25:09
【问题描述】:

我有一个需要在 centos 7 和 centos 6 上运行的 apache 剧本。我希望根据分发主要版本触发处理程序。我有一个名为restart apache on 7 的处理程序和另一个restart apache on 6

我的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%}

关于如何编写通知语句以实现我的目标的任何线索?

【问题讨论】:

    标签: ansible ansible-handlers


    【解决方案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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      • 2017-02-17
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多