Skip to content
Advertisement

Encountering ‘dict object’ has no attribute ‘results’ with Ansible despite previous checks indicating it does exist

I am using Ansible and I am encountering an issue that I do not understand or know how to solve. Hopefully someone can help me.

The problem occurs with a failed_when conditional:

JavaScript

My task is:

JavaScript

In attempting to debug, I have written the following:

JavaScript

where, I think, the important information here is:

JavaScript

I am new to Ansible, perhaps that is why I am not seeing it, but with the wording 'dict object' has no attribute 'results' it suggests that result, which is a dict, does not have the attribute results, which my debugging suggests it does. I have been able to traverse to result.results[0].json & result.results[0].json.localizedMessage so it looks OK to me.

Can anyone advise? I would like to understand where I am going wrong, alternatively I’ll take suggestions on performing the failed_when check using some other approach.

Advertisement

Answer

Your issue is coming from the fact that Ansible is creating results in a very peculiar way:

  1. The items are registered as if you where using no loop at all, and you can reference the previous element via your registered variable.
  2. When you exist the loop, then the results key is created in the dictionary and then populated from all the results.

This is described in a really short fashioned way in this sentence and example:

During iteration, the result of the current item will be placed in the variable:

JavaScript

Source: https://docs.ansible.com/ansible/2.9/user_guide/playbooks_loops.html#registering-variables-with-a-loop


So if you want a self registered task that check for its own items status you will just have to do something like:

JavaScript

Here are two example playbooks:

  1. The playbook
    JavaScript
    That yields the recap:
    JavaScript
  2. The playbook
    JavaScript
    That yields the recap:
    JavaScript
Advertisement