Ansible variable register in two tasks either one or the other

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?

1 Like

Put the two fetch tasks into separate files and then include_tasks using the when condition?

2 Likes

Personally id reduce this a bit.

I’d use the shell module to check which of the versions exist, and then assign that as the variable

3 Likes

F.Y.I.

If a task fails or is skipped, Ansible still registers a variable with a failure or skipped status, unless the task is skipped based on tags.
Using Variables — Ansible Documentation

2 Likes

You’re already using shell: so keep the complexity in one place.

- name: Register the Zabbix agent
  ansible.builtin.shell: |
    if [ -x "$(which zabbix_agent2)" ] ; then
      echo zabbix_agent2
      echo "$(zabbix_agent2 -V | grep "zabbix_agent" | cut -d' ' -f3)"
    elif [ -x "$(which zabbix_agentd)" ] ; then
      echo zabbix_agentd
      echo "$(zabbix_agentd -V | grep "zabbix_agent" | cut -d' ' -f4)"
    else
      echo none
      echo none
    fi
  ignore_errors: true
  register: zabbix_agent

After that, {{ zabbix_agent.stdout_lines[0] }} is the agent name,
and {{ zabbix_agent.stdout_lines[1] }} is the version.

2 Likes

I understand that variables are registered with skipped status. My attempt was to bypass that somehow. It appears that it may not be the most practical solution after all.

@chris, I have to say that I appreciate the include_tasks option, which I find quite appealing. However, I liked @utoddl’s suggestion even more. :grin:
It seems a more straightforward approach.

Here is the resulting Yaml:

- name: Register the Zabbix agent
  ansible.builtin.shell: |
    if [ -x "$(which zabbix_agent2)" ] ; then
      echo "Zabbix Agent 2"
      echo "$(zabbix_agent2 -V | grep "zabbix_agent" | cut -d' ' -f3)"
    elif [ -x "$(which zabbix_agentd)" ] ; then
      echo "Zabbix Agent"
      echo "$(zabbix_agentd -V | grep "zabbix_agent" | cut -d' ' -f4)"
    else
      echo none
      echo none
    fi
  ignore_errors: true
  register: zabbix_agent

- name: Stop if there is no Agent installed
  ansible.builtin.fail:
    msg: |
      Zabbix Agent is not installed.
  when: zabbix_agent.stdout_lines[0] == "none" and zabbix_component == "agent"

Also, the results from some test I made, which includes the 3 scenarios, Agent 2, Agent D and none.

TASK [zabbix_ansible : Register the Zabbix agent] ***********************************************************************************************************************************************************************************
changed: [zbxproxy]
changed: [ldap]
changed: [redhat]

TASK [zabbix_ansible : Stop if there is no Agent installed] *************************************************************************************************************************************************************************
skipping: [zbxproxy]
skipping: [ldap]
fatal: [redhat]: FAILED! => changed=false
  msg: |-
    Zabbix Agent is not installed.

(…)

TASK [zabbix_ansible : Display Zabbix Agent current version] ************************************************************************************************************************************************************************
ok: [zbxproxy] =>
  msg: |-
    PLAYBOOK FINISHED
    Zabbix Agent 2 version 6.4.6
ok: [ldap] =>
  msg: |-
    PLAYBOOK FINISHED
    Zabbix Agent version 6.4.6

Thanks for the invaluable feedback guys.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.