# Ansible variable register in two tasks either one or the other

**URL:** <https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071>\
**Category:** Get Help\
**Tags:** playbook, zabbix, ansible-core\
**Created:** [September 24, 2023, 5:08pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071 "2023-09-24T17:08:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![markfree](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@markfree](https://forum.ansible.com/u/markfree)\
**Post date:** [September 24, 2023, 5:08pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/1 "2023-09-24T17:08:40Z")

</div>

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.

```auto
- 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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [September 24, 2023, 6:11pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/2 "2023-09-24T18:11:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kevinwincott](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Kevinwincott](https://forum.ansible.com/u/Kevinwincott)\
**Post date:** [September 24, 2023, 7:53pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/3 "2023-09-24T19:53:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [September 25, 2023, 1:38am UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/4 "2023-09-25T01:38:09Z")

</div>

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](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#registering-variables)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 25, 2023, 8:12pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/5 "2023-09-25T20:12:48Z")

</div>

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

```yml
- 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.

---

<div class="post-metadata">

**Author:** ![markfree](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@markfree](https://forum.ansible.com/u/markfree)\
**Post date:** [September 25, 2023, 9:09pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/6 "2023-09-25T21:09:48Z")

</div>

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. 😁  
It seems a more straightforward approach.

Here is the resulting Yaml:

```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.

```auto
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.

```

**(…)**

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 25, 2023, 9:10pm UTC](https://forum.ansible.com/t/ansible-variable-register-in-two-tasks-either-one-or-the-other/1071/7 "2023-10-25T21:10:04Z")

</div>

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