Usage of a registered variable in "when" condition

72 views
Skip to first unread message

pan...@hotmail.de

unread,
Sep 8, 2020, 7:13:13 AM9/8/20
to Ansible Project
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!

Stefan Hornburg (Racke)

unread,
Sep 8, 2020, 7:20:12 AM9/8/20
to ansible...@googlegroups.com
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

>
> Thank you advance!
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c66990f7-be94-40c5-a4fe-346c76ec8655n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/c66990f7-be94-40c5-a4fe-346c76ec8655n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Peter Gebirgslok

unread,
Sep 8, 2020, 7:20:51 AM9/8/20
to Ansible Project
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"

Stefan Hornburg (Racke)

unread,
Sep 8, 2020, 7:26:35 AM9/8/20
to ansible...@googlegroups.com
You can't edit your post of course. Change that line to:

when: inventory_hostname == redundancynode

Regards
Racke

> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1b42b037-424a-4028-a742-703be574f215o%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1b42b037-424a-4028-a742-703be574f215o%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

pan...@hotmail.de

unread,
Sep 8, 2020, 7:47:53 AM9/8/20
to Ansible Project
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""

Stefan Hornburg (Racke)

unread,
Sep 8, 2020, 8:14:54 AM9/8/20
to ansible...@googlegroups.com
On 9/8/20 1:47 PM, pan...@hotmail.de wrote:
> 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

>
>
> brae...@gmail.com schrieb am Dienstag, 8. September 2020 um 13:26:35 UTC+2:
>
> On 9/8/20 1:20 PM, Peter Gebirgslok wrote:
> > 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
>
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/1b42b037-424a-4028-a742-703be574f215o%40googlegroups.com
> >
> <https://groups.google.com/d/msgid/ansible-project/1b42b037-424a-4028-a742-703be574f215o%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/016641c5-92b0-4940-93c5-3fe154f368d3n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/016641c5-92b0-4940-93c5-3fe154f368d3n%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

pan...@hotmail.de

unread,
Sep 8, 2020, 8:35:45 AM9/8/20
to Ansible Project
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?

pan...@hotmail.de

unread,
Sep 8, 2020, 8:44:51 AM9/8/20
to Ansible Project
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 :)
Reply all
Reply to author
Forward
0 new messages