# Odd case where I cannot figure out how to eliminate {{ }} in a when: clause

**URL:** <https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861>\
**Category:** Ansible Project\
**Created:** [July 24, 2019, 5:48pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861 "2019-07-24T17:48:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [July 24, 2019, 5:48pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/1 "2019-07-24T17:48:39Z")

</div>

All - I’m trying to dynamically set facts in a loop. (Long story short, for a list of keys, if the key isn’t defined, I want to replace the key with the value from a register variable.) I have a playbook that works, but it requires the use of {{ }} in a when: clause, and Ansible is complaining. Can anyone figure out how to rewrite the when: without {{ }} ?

`  
$ cat y.yml

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 25, 2019, 6:51am UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/2 "2019-07-25T06:51:50Z")

</div>

Since you variable name is in a variable you need to lookup the variable

&nbsp;&nbsp;&nbsp;when: hostvars[inventory\_hostname][item.key] is not defined

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 25, 2019, 2:09pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/3 "2019-07-25T14:09:24Z")

</div>

You are actually the 'exception' so it is fine for your use {{ }} in  
when because you actually mean to do double interpolation.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 25, 2019, 3:17pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/4 "2019-07-25T15:17:43Z")

</div>

He also wanted to remove the that Ansible is complaining as I understood it, and then {{ }} need to be removed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 25, 2019, 9:12pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/5 "2019-07-25T21:12:31Z")

</div>

yes, but your example only handles 'hostvars', which the ones he is  
trying to interpolate happen not to be, you need the 'vars' lookup  
instead.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 25, 2019, 11:00pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/6 "2019-07-25T23:00:13Z")

</div>

I always use vars since it works great but I know you are not very fond of that.

I was initially going to post this  
&nbsp;&nbsp;when: vars[item.key] is not defined

but just before I posted changed it to  
&nbsp;&nbsp;when: hostvars[inventory\_hostname][item.key] is not defined  
and as you say, it doesn't work.

The problem with the vars lookup is it fails if the variable is not defined and the default=None is not working.

&nbsp;&nbsp;- debug: msg="{{ lookup('vars', 'no\_existing\_var', default=None) }}"

gives error  
&nbsp;&nbsp;The task includes an option with an undefined variable. The error was: No variable found with this name: no\_existing\_var

This means you need to put a string that you would never use as default, something like this  
&nbsp;&nbsp;when: lookup('vars', item.key, default='the varible is not defined') == 'the varible is not defined'

This works but is not a very elegant solution as the test "is not defined"

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [July 30, 2019, 6:56pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/7 "2019-07-30T18:56:35Z")

</div>

Thank you both, and apologies for not responding sooner (got yanked onto something else). vars worked great.

Rob

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 1, 2019, 7:58pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/8 "2019-08-01T19:58:08Z")

</div>

my issue with 'vars' is that it is not templated for all values, it  
was never meant for direct use and we don't even use internally  
anymore and we hope to remove soon.

the `vars` lookup does template values correctly and was created for  
that purpose.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 1, 2019, 8:08pm UTC](https://forum.ansible.com/t/odd-case-where-i-cannot-figure-out-how-to-eliminate-in-a-when-clause/30861/9 "2019-08-01T20:08:20Z")

</div>

vars lookup is not a very good substitute IMHO since it can't handle undefined variable very well.  
So that leaves us with vars for the foreseeable future.
