This post discusses how to solve the Ansible error " 'dict object' has no attribute 'stdout_lines" .
This error appears when you are trying to iterate through a list or dictionary incorrectly.
In this post, we will discuss how to iterate through the dictionary and list in the right way and solve your "dict object has no attribute" error.
Ansible will throw this error when you are trying to display the stdout (or) stderr of a task during playbook runtime.
Generally, we display the stdout (or) stderr of the task at runtime by calling the corresponding task's register within the debug module and with the help of msg as shown below.
and the output of that debug task would look something like this, as it runs.
But sometimes, we will get the exception while trying to do the same thing. ( not exactly the same)
Which is trying to display the stdout of the task by invoking the
look at the modified version of the same task with loop iteration.
What do you think the output of this will be ?.
An Error message states that dict object' has no attribute 'stderr'
Why this error?
To understand why this error is occurring and how Ansible is saying that we are trying to print the dictionary object.
Let us make little modification in our task to print the complete register variable instead of a property (stderr (or) stdout)
The modified playbook with debug module
As mentioned in the preceding image,
"results" : [ is a key and value of a dictionar
The Reason the output came in the dictionary format is that we have used "with_items" loop along with register so that we can get the output for each loop iteration (or) item.
Refer this link here for more info
How to resolve
stderrcomes inside the list and since the list cannot be parsed with the "key" only option is to iterate with the list with the help of loop module.
The Resolved Playbook - A Right way to process dict object
As you could notice, we are using the loop module to parse the
register.results and accessing the stderr of the register as
item.stderr As we have a loop with the register, we will the stderr of each item.
In other words, we will see the
stderr of both java commands executed on both machines
The output will clarify the same.
Note*: If you want to access the stdout (or) stdout_lines. Just change the
You could see that we were able to print the
stderrof the task without any dictionary related error.
We learnt the correct way to process the loop variables and its contents and how to avoid dict object' has no attribute 'stdout_lines error message
Hope it helps