# 'empty()' Jinja test?

**URL:** <https://forum.ansible.com/t/empty-jinja-test/16837>\
**Category:** Ansible Project\
**Created:** [November 20, 2014, 7:21pm UTC](https://forum.ansible.com/t/empty-jinja-test/16837 "2014-11-20T19:21:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [November 20, 2014, 7:21pm UTC](https://forum.ansible.com/t/empty-jinja-test/16837/1 "2014-11-20T19:21:59Z")

</div>

Hi,

several times now I came across situation where one of the modules returns ‘empty list’ in variable and it would be handy to have a simple test like:

{% if foo is empty %}

which is not the same as

{% if foo is defined %}

because former case covers also “”, , {} and None as well as “undefined”. Sort of like “empty()” call in PHP.

Right now I’ve been managing situation with crutches like:

when: foo\_list|join(‘’) != ‘’

but that is non-descriptive and hackish IMO. Is there an interest of adding this feature? Shall I post into Github issue tracker?

---

<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:** [November 20, 2014, 8:02pm UTC](https://forum.ansible.com/t/empty-jinja-test/16837/2 "2014-11-20T20:02:40Z")

</div>

would this not work?  
foo\>length == 0

---

<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:** [November 20, 2014, 8:17pm UTC](https://forum.ansible.com/t/empty-jinja-test/16837/3 "2014-11-20T20:17:20Z")

</div>

Since jinja2 uses python under the hood, you get the benefit of python truthiness checks. I would probably write this as:

{% if foo is defined and foo %}

In some cases this can be shortened to just:

{% if foo %}

The inverse to see if it is empty is:

{% if not foo %}
