# For 2.0 How to test if variable set to null in when clause

**URL:** <https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273>\
**Category:** Ansible Project\
**Created:** [January 19, 2016, 2:43pm UTC](https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273 "2016-01-19T14:43:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Baydoun](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Michael\_Baydoun](https://forum.ansible.com/u/Michael_Baydoun)\
**Post date:** [January 19, 2016, 2:43pm UTC](https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273/1 "2016-01-19T14:43:51Z")

</div>

pre 2.0

set\_fact:  
uberHost: “{{ ansible\_ssh\_host }}”  
when: ansible\_ssh\_host is defined

set fact:  
uberHost: “{{ inventory\_hostname }}”  
when: ansible\_ssh\_host is not defined

Post 2.0, changed to use ansible\_host  
uberHost is always getting set to null  
ansible\_host is defined and set to null even when it’s not present in the inventory file  
I haven’t figured out how to test to see if ansible\_host is null in a when clause.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [January 19, 2016, 2:56pm UTC](https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273/2 "2016-01-19T14:56:21Z")

</div>

It should be enough to just check:

when: ansible\_ssh\_host

However it may be good to come at it from a little different direction and use something like:

set\_fact:  
uberHost: “{{ ansible\_ssh\_host|default(inventory\_hostname, True) }}”

The True in default() will tell jinja2 to use pythons truthyness checks, so an empty string evaluates as false as well.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 19, 2016, 3:03pm UTC](https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273/3 "2016-01-19T15:03:44Z")

</div>

you can even chain them and make it future proof  
set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;uberHost: "{{  
ansible\_host|default(ansible\_ssh\_host|default(inventory\_hostname,  
True), True) }}"

---

<div class="post-metadata">

**Author:** ![Michael\_Baydoun](https://avatars.discourse-cdn.com/v4/letter/m/aeb1de/32.png) [@Michael\_Baydoun](https://forum.ansible.com/u/Michael_Baydoun)\
**Post date:** [January 19, 2016, 4:38pm UTC](https://forum.ansible.com/t/for-2-0-how-to-test-if-variable-set-to-null-in-when-clause/20273/4 "2016-01-19T16:38:18Z")

</div>

when: ansible\_host still caused the set\_fact to execute when it was not present / null

However, the other suggestions worked great, thanks!
