# Usage of a registered variable in "when" condition

**URL:** <https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762>\
**Category:** Ansible Project\
**Created:** [September 8, 2020, 11:13am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762 "2020-09-08T11:13:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [September 8, 2020, 11:13am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/1 "2020-09-08T11:13:13Z")

</div>

Hello guys,

I have the following problem:  
I’m reading a simple json file which contains some names of my hosts.  
The file looks like this:  
{ “redundancynode”: “node4”, “service”: “dhcp”, “servicenode” “node1” }

Now i have a task to read the contents of that file:

vars:

file\_content: "{{ lookup(‘file’, ‘/tmp/info’) }}

tasks:

- name: Read from local file /tmp/info  
debug:  
msg: “{{ file\_content }}”

The Output of Ansible for that file looks like this:

ok: [127.0.0.1] =\> {  
“msg”: {  
“redundancynode”: “node4”,  
“service”: “dhcp”,  
“servicenode”: “node1”  
}

}  
I used set\_fact to get the actual nodes for servicenode and redundancynode:

- set\_fact: redundancynode=“{{output.msg.redundancynode}}”

- set\_fact: servicenode=“{{ output.msg.servicenode }}”

debggung for example redundancynode gives me as expected the following output:

ok: [127.0.0.1] =\> {  
“msg”: “node4”  
}

Now, I want to use the content of “redundancy node”, which is “node4” in another task as condition:  
when: inventory\_hostname == “redundanynode”

But this does not work. It just skips the task for all nodes.

(BTW: node4 does exist in my inventory file of course)

What am I missing? Im searching for a solution since hours.

Thank you advance!

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 8, 2020, 11:19am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/2 "2020-09-08T11:19:30Z")

</div>

> Hello guys,
> 
> I have the following problem:  
> I'm reading a simple json file which contains some names of my hosts.  
> The file looks like this:  
> { "redundancynode": "node4", "service": "dhcp", "servicenode" "node1" }
> 
> Now i have a task to read the contents of that file:
> 
> vars:  
> file\_content: "{{ lookup('file', '/tmp/info') }}
> 
> tasks:  
> - name: Read from local file /tmp/info  
> debug:  
> msg: "{{ file\_content }}"
> 
> The Output of Ansible for that file looks like this:
> 
> ok: [127.0.0.1] =\> {  
> "msg": {  
> "redundancynode": "node4",  
> "service": "dhcp",  
> "servicenode": "node1"  
> }  
> }  
> I used set\_fact to get the actual nodes for servicenode and redundancynode:
> 
> - set\_fact: redundancynode="{{output.msg.redundancynode}}"
> 
> - set\_fact: servicenode="{{ output.msg.servicenode }}"
> 
> debggung for example redundancynode gives me as expected the following output:
> 
> ok: [127.0.0.1] =\> {  
> "msg": "node4"  
> }
> 
> Now, I want to use the content of "redundancy node", which is "node4" in another task as condition:  
> when: inventory\_hostname == "redundanynode"
> 
> But this does not work. It just skips the task for all nodes.
> 
> (BTW: node4 does exist in my inventory file of course)
> 
> What am I missing? Im searching for a solution since hours.

1. you have a typo in the when statement  
2. putting the variable name in "" makes it a literal string, so it evaluates to redundanynode

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [September 8, 2020, 11:20am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/3 "2020-09-08T11:20:51Z")

</div>

I dont know how to edit my post, but there is also no typo in the when condition

this i how the when condition looks like.

when: inventory\_hostname == “redundancynode”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 8, 2020, 11:25am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/4 "2020-09-08T11:25:52Z")

</div>

> I dont know how to edit my post, but there is also no typo in the when condition
> 
> this i how the when condition looks like.
> 
> when: inventory\_hostname == "redundancynode"

You can't edit your post of course. Change that line to:

when: inventory\_hostname == redundancynode

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [September 8, 2020, 11:47am UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/5 "2020-09-08T11:47:53Z")

</div>

I tried this, but it just gives me error:  
“The conditional check ‘inventory\_hostname == redundancynode’ failed. The error was: error while evaluating conditional (inventory\_hostname == redundancynode): ‘redundancynode’ is undefined”

It is also not possible to use jinja2 for that

when: inventory\_hostname == {{ redundancynode }} or

when: inventory\_hostname == “{{ redundancynode }}”

Can the problem be that redundancynode is already in quotes?

So that ansible is reading this as:  
when: inventory\_hostname == "“node4"”

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 8, 2020, 12:14pm UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/6 "2020-09-08T12:14:01Z")

</div>

> I tried this, but it just gives me error:  
> "The conditional check 'inventory\_hostname == redundancynode' failed. The error was: error while evaluating conditional  
> (inventory\_hostname == redundancynode): 'redundancynode' is undefined"

You have to set this variable, otherwise your playbook doesn't work.

> It is also not possible to use jinja2 for that
> 
> when: inventory\_hostname == {{ redundancynode }} or  
> when: inventory\_hostname == "{{ redundancynode }}"

when conditions are automatically wrapped in curly braces, so it is Jinja already.

> Can the problem be that redundancynode is already in quotes?
> 
> So that ansible is reading this as:  
> when: inventory\_hostname == ""node4""

See above.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [September 8, 2020, 12:35pm UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/7 "2020-09-08T12:35:45Z")

</div>

Thank you for your Input.

But what do you mean by setting the variable?

I thought im setting the variable by using set\_fact:

- set\_fact: redundancynode=“{{output.msg.redundancynode}}”

- set\_fact: servicenode=“{{ output.msg.servicenode }}”

By using this task:

- debug:  
msg: “{{ redundancynode }}”

I already get the correct output

ok: [127.0.0.1] =\> {  
“msg”: “node4”  
}

So the variable redundancynode should contain “node4”, isn’t it?

---

<div class="post-metadata">

**Author:** ![Peter\_Gebirgslok](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Gebirgslok](https://forum.ansible.com/u/Peter_Gebirgslok)\
**Post date:** [September 8, 2020, 12:44pm UTC](https://forum.ansible.com/t/usage-of-a-registered-variable-in-when-condition/33762/8 "2020-09-08T12:44:50Z")

</div>

I think i found the solution.

i have to set the variable in vars:

vars:  
file\_content: “{{ lookup(‘file’, ‘/tmp/dhcpinfo’) }}”  
redundancynode: “{{file\_content.redundancynode}}”

now it is working by using:

when: inventory\_hostname == redundancynode

You probably meant this by setting the variable.

thank you very much 🙂
