4

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

  • Is your YAML mis-formatted? i.e. tabs vs. spaces? – Michael Hampton May 15 '18 at 7:00
  • Just checked to be sure, no tabs, only spaces. – Gerald Schneider May 15 '18 at 7:12
3

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.

|improve this answer|||||
  • Too bad ... I couldn't find any other possibility, so I went with this method. – Gerald Schneider Jun 4 '18 at 11:22
  • After realizing that I ca use jinja statements in strings I changed my shell command to shell: "needs-restarting {% if ansible_distribution_major_version|int >= 7%}>{% endif %} /dev/null || echo Reboot required". This makes the two different queries unnecessary. – Gerald Schneider Apr 18 '19 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.