I have an Ansible role that manipulates some Zabbix components. This role has this set of tasks to check the Zabbix Agent type (1 or 2) and version.
- name: Check if Zabbix Agent 2 is installed
ansible.builtin.shell:
cmd: which zabbix_agent2
register: zabbix_agent2_installed
ignore_errors: yes
- name: Check if Zabbix Agent D is installed
ansible.builtin.shell:
cmd: which zabbix_agentd
when: not zabbix_agent2_installed.stdout
register: zabbix_agentd_installed
ignore_errors: yes
- name: Stop if there is no Agent installed
ansible.builtin.fail:
msg: |
Zabbix Agent is not installed.
when: not zabbix_agent2_installed.stdout and not zabbix_agentd_installed.stdout and zabbix_component == "agent"
- name: Fetch Zabbix Agent 2 version
ansible.builtin.shell:
cmd: zabbix_agent2 -V | grep "zabbix_agent" | cut -d' ' -f3
when: zabbix_agent2_installed.stdout
register: zabbix_agent_ver_old
failed_when: zabbix_agent_ver_old.stderr
- name: Fetch Zabbix Agent D version
ansible.builtin.shell:
cmd: zabbix_agentd -V | grep "zabbix_agent" | cut -d' ' -f4
when: not zabbix_agentd_installed.skipped
register: zabbix_agent_ver_old
failed_when: zabbix_agent_ver_old.stderr
When I run the playbook, the variable zabbix_agentd_ver_old
is registered twice in the last two tasks, with the same name. Even if the last task is skipped, the variable is registered as skipped.
TASK [zabbix_ansible : Check if Zabbix Agent 2 is installed] *********************************************************************
changed: [zbxproxy]
TASK [zabbix_ansible : Check if Zabbix Agent D is installed] *********************************************************************
skipping: [zbxproxy]
TASK [zabbix_ansible : Stop if there is no Agent installed] **********************************************************************
skipping: [zbxproxy]
TASK [zabbix_ansible : Fetch Zabbix Agent 2 version] *****************************************************************************
changed: [zbxproxy]
TASK [zabbix_ansible : ansible.builtin.debug] ************************************************************************************
ok: [zbxproxy] =>
zabbix_agent_ver_old:
changed: true
cmd: zabbix_agent2 -V | grep "zabbix_agent" | cut -d' ' -f3
delta: '0:00:00.005897'
end: '2023-09-24 10:07:50.014208'
failed: false
failed_when_result: false
msg: ''
rc: 0
start: '2023-09-24 10:07:50.008311'
stderr: ''
stderr_lines: []
stdout: 6.4.6
stdout_lines:
- 6.4.6
TASK [zabbix_ansible : Fetch Zabbix Agent D version] *****************************************************************************
skipping: [zbxproxy]
TASK [zabbix_ansible : ansible.builtin.debug] ************************************************************************************
ok: [zbxproxy] =>
zabbix_agent_ver_old:
changed: false
skip_reason: Conditional result was False
skipped: true
I want to avoid changing the variable name, so I need the variable to be registered in either one of the last two tasks, not both.
How can I skip the last task altogether, including the registration, if its condition is not met?