Usage of a registered variable in "when" condition

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!

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
        Racke

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”

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
          Racke

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"”

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
         Racke

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?

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 :slight_smile: