# Is this a register {{ variable }} bug?

**URL:** <https://forum.ansible.com/t/is-this-a-register-variable-bug/14423>\
**Category:** Ansible Developer\
**Created:** [January 25, 2014, 12:50am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423 "2014-01-25T00:50:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [January 25, 2014, 12:50am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/1 "2014-01-25T00:50:17Z")

</div>

So I have two tasks, that, based on a conditional run a command and register the output to a variable name. It’s only possible to execute one task or the other – not both…

but

since both try to register the same variable name, it appears that both registers still happen… if the 2nd task is the one I’m executing, then everything works fine. If it’s the first task, then it appears that the 2nd task clears the registered variable, and causes subsequent logic to fail.

I’ve seen this behavior on 1.4.4 and HEAD (as of now-ish)

Below is a sample playbook you can run to demonstrate the situation. If you run:

ansible-playbook test.yml -e todo=add

then things work as expected. If you run:

ansible-playbook test.yml -e todo=revoke

then you get an error about “One or more undefined variables: ‘dict object’ has no attribute ‘stdout’”

test.yml:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 25, 2014, 1:04am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/2 "2014-01-25T01:04:40Z")

</div>

“then you get an error about “One or more undefined variables: ‘dict object’ has no attribute ‘stdout’””

for which task again?

I’m having difficulty following the purpose of the example.

I will remind the list that I have a pet peeve about “doesn’t work” in which case I don’t know what “doesn’t work” means. What is the failure mode, etc?

Do you mean the example with the “revoke” is the only one that does things?

(Also, I’d suggest --tags over “-e”, makes for clearer playbooks!)

Another tip:

debug: msg=“{{ victimOutput.stdout }}”

debug: var=victimOutput.stdout

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [January 25, 2014, 1:07am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/3 "2014-01-25T01:07:48Z")

</div>

The task “success output” would fail, because victimOutput is undefined.

The pasted playbook and commands are light enough to run and demonstrate what I’m talking about.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [January 25, 2014, 1:14am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/4 "2014-01-25T01:14:17Z")

</div>

Okay, I’ve modified the demo playbook to look like this:

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [January 25, 2014, 1:17am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/5 "2014-01-25T01:17:44Z")

</div>

rereading this one more time, I see that I still had the when: todo == conditionals still in the playbook. Removing those two lines appear to make the playbook run as expected.

Does that indicate there might be some conflict between “when” and “register” functions?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 25, 2014, 1:31am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/6 "2014-01-25T01:31:49Z")

</div>

“The pasted playbook and commands are light enough to run and demonstrate what I’m talking about.”

Yes, but it’s requiring everyone on the mailing list to run them to see what you mean.

It’s like sharing a connect-the-dots picture without the dots connected.

Which, with the amount of time I personally have, is not so good 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 25, 2014, 1:33am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/7 "2014-01-25T01:33:34Z")

</div>

It’s registering a null when the task is skipped.

Solution would be to use different variables.

Consider that when running against an array of 500 hosts and keying off, say, the OS type, not all of them may be the same OS type.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston1](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@Brent\_Langston1](https://forum.ansible.com/u/Brent_Langston1)\
**Post date:** [January 25, 2014, 1:39am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/8 "2014-01-25T01:39:44Z")

</div>

Different variables would cause me to handle the success and failure notifications multiple times. I swapped over to using tags, as you suggested, and now avoid the situation all together.

It might be worth noting somewhere that a skipped task can still register a variable. That’s not what I was expecting. I dunno… maybe that’s too esoteric.

---

<div class="post-metadata">

**Author:** ![ralovely](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@ralovely](https://forum.ansible.com/u/ralovely)\
**Post date:** [February 18, 2014, 5:41am UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/9 "2014-02-18T05:41:29Z")

</div>

> It might be worth noting somewhere that a skipped task can still register a variable. That’s not what I was expecting. I dunno… maybe that’s too esoteric.

As a good practice, I set a default value for every registered variable that I expect. i.e.

# In my role defaults/main.yml file

my\_registered\_var.changed: false

A bit tedious, but makes for more independent tasks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 18, 2014, 12:05pm UTC](https://forum.ansible.com/t/is-this-a-register-variable-bug/14423/10 "2014-02-18T12:05:31Z")

</div>

Hmm…

I would not expect people to do this, and also it won’t work for most people unless you are using the “merge hash behavior” config setting, which is non-standard (and often not something you want).
