Ansible – Register Variable in Multiple Tasks

ansibleansible-playbook

I'm using the following tasks in an ansible script that installs updates on various servers. These tasks are for CentOS machines:

- name: Check for outstanding reboot
  shell: needs-restarting > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int < 7
  register: result
- name: Check for outstanding reboot
  shell: needs-restarting -r > /dev/null || echo Reboot required
  when: ansible_distribution_major_version|int >= 7
  register: result
- name: Report reboot
  debug: msg="{{ result.stdout_lines }}"

The result:

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [host1]
skipping: [host2]
skipping: [host5]
changed: [host3]
changed: [host4]

TASK [Check for outstanding reboot] ***********************************************************************************
skipping: [host3]
skipping: [host4]
changed: [host2]
changed: [host1]
changed: [host5]

TASK [Report reboot] **************************************************************************************************
fatal: [host3]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [host1] => {
    "msg": [
        "Reboot required"
    ]
}
fatal: [host4]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout_lines'\n\nThe error appears to have been in '/path/to/updates.yml': line 52, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    register: result\n  - name: Report reboot\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout_lines'"}
ok: [host2] => {
    "msg": [
        "Reboot required"
    ]
}
ok: [host5] => {
    "msg": [
        "Reboot required"
    ]
}
        to retry, use: --limit @/path/to/updates.retry

As you can see both check tasks store the result in a variable called "result", but only the variables for the hosts in the second task (with ansible_distribution_major_version|int >= 7) are filled, resulting in the error messages. It seems that the second check tasks unsets the results of the previous task.

Is it possible to keep the results of both tasks? Or do I have to copy the report task and add the version check to both? I'd rather have the report in one place.

Ansible version is 2.4.4.0

Best Answer

This happens because Ansible will store the result even when the task is skipped:

If a task fails or is skipped, the variable still is registered with a failure or skipped status, the only way to avoid registering a variable is using tags.

Ansible user guide on registered-variables

So, no it is not possible to keep the result of both tasks.

I would, as you already suggested, copy the report task and add the version check to both.