2

I have an apache playbook which needs to run on centos 7 and centos 6. I want the handler triggered based on distribution major version. I have a handler called restart apache on 7 and another one restart apache on 6.

My handlers/main.yml looks like this

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

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

I have tried doing the following in my tasks/main.ymlbut seem to be running into syntax issues pertaining to the notify script.

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

Any clues on how to write the notify statement to achieve my goal?

absurd
  • 1,035
  • 3
  • 14
  • 35
crusadecoder
  • 651
  • 1
  • 11
  • 26

1 Answers1

3

For a general solution, you can use topics, but I'm not sure why you would want to use systemd module instead of service - the latter should cover CentOS 6 and 7 without any difference.

Also use an arithmetic comparison for ansible_distribution_major_version instead of the string comparison as in your example.


Handlers listening to topics:

---
- 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"

and the task notifying them:

- name: Copy custom configs
  copy:
    dest: /etc/httpd/conf/
    src: httpd.conf
    owner: root 
  notify: "restart apache"
U880D
  • 8,601
  • 6
  • 24
  • 40
techraf
  • 64,883
  • 27
  • 193
  • 198