This task collection doesn\'t work as I hoped it would:
- name: Find out whether syslog-ng is installed (yum)
tags: syslog_forwarding
command: yum -q list in
Why not just slightly reorder your tasks to make your "configure" task immediately follow the test ("find out..") as a pair, i.e.:
- name: Find out whether syslog-ng is installed (yum)
- name: Configure syslog-ng to forward all logs to syslog servers (yum)
- name: Find out whether syslog-ng is installed (yum)
- name: Configure syslog-ng to forward all logs to syslog servers (apt)
That way even if the other test resets syslog_ng_check you don't care because the you've already used it.
Putting the find/configure pair for each of the two pkg managers in a block guarded by when: ansible_pkg_mgr == ... would be another way, and would make it more obvious that the ordering/grouping was important.
There are only workarounds. Skipped tasks will always overwrite registered variable.
One possible way:
- name: Find out whether syslog-ng is installed (yum)
tags: syslog_forwarding
command: yum -q list installed syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'yum'
- set_fact:
syslog_ng_flag: True
when: syslog_ng_check.rc == 0
- name: Find out whether syslog-ng is installed (apt)
tags: syslog_forwarding
command: dpkg -s syslog-ng
register: syslog_ng_check
failed_when: False
changed_when: False
when: ansible_pkg_mgr == 'apt'
- set_fact:
syslog_ng_flag: True
when: syslog_ng_check.rc == 0
- name: Configure syslog-ng to forward all logs to syslog servers (apt)
tags: syslog_forwarding
template:
src: syslog_ng_forward_all.conf.j2
dest: /etc/syslog-ng/conf.d/syslog_forward.conf
notify: restart syslog_ng
when: syslog_ng_flag | default(false)