Clarification on how to use Registered Variables

This confusion is probably due to me not being a python programmer. So, for any other non-python users, hopefully this thread will be helpful.

Re: http://ansibleworks.com/docs/playbooks_variables.html#registered-variables

We see a variable registered:

register: foo_result

But then when it’s used, we see:

when: foo_result.rc == 5

This probably makes perfect sense to python guys, but as a rubyist, I had no idea what was going on here. Googling “python rc” etc was also fruitless.

So, after a quick twitter conversation with Michael DeHaan, it sounds like for any registered variables, you have to append either “.rc” or “.stdout” to the variable in order to use the registered variable. Is that true for all cases, or just for when it’s used as a conditional like in the example?

Thanks!
Matt

P.S. Despite further googling, finding any useful documentation on Python’s “rc” method is proving elusive. Is there some canonical Python reference where I should look?

I’m not a Python programmer either but from my understanding this is not a Python issue at all.

What happens is that foo_result is an array of variables. This array contains keyed values for several components of the command line feedback and some of the keys are:

rc: this is the result code from the command executed
stdout: this contains the output to the standard output from the command executed. Often this is again an array of lines.
stderr: like stdout but for the standard error output

There are many more and you can make them visible when you execute your playbook with the command line parameter -vvv, then you can see all included stuff in that variable.

Very helpful! Thanks Jürgen!

Here’s a tip.

When you register something, run that playbook with “-v” and you can see all that is registered in the hash/dictionary – it’s actually usually not an array.

  • shell: /usr/bin/foo
    register: foo

If you run with “-v” you will see all of the values of the subelements.

In 1.4, it’s also super easy to do this:

  • shell: /usr/bin/foo
    register: foo

  • debug: var=foo

But if you are running an earlier version, you have to do:

  • debug: msg=“the value of foo is {{ foo }}”

Foo will contain many more things than rc and stdout.

It’s on our list to make sure the documentation for each module shows all the attributes it returns so you don’t have to run “-v” or use “debug” to look, though I will say in all fairness this may not be immediately accomplished.

Great to know, I have a much better understanding now. Thanks!