# Checking if a list is empty...

**URL:** <https://forum.ansible.com/t/checking-if-a-list-is-empty/13951>\
**Category:** Ansible Project\
**Created:** [November 21, 2013, 6:16am UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951 "2013-11-21T06:16:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 21, 2013, 6:16am UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/1 "2013-11-21T06:16:20Z")

</div>

Hi,

My inventory script returns the correct JSON for a host including an empty list for ‘listen\_ips’, i.e:

{ “listen\_ips”: }

I want to include a task only if the listen\_ips list contains at least one element. This doesn’t work:

- { include: tasks/repo\_server.yml, when: listen\_ips \> 0 }

Any ideas?

Cheers  
GS

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [November 21, 2013, 6:18am UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/2 "2013-11-21T06:18:51Z")

</div>

maybe just check if its got the first element:

- { include: tasks/repo\_server.yml, when: listen\_ips.0 is defined }

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 21, 2013, 6:21am UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/3 "2013-11-21T06:21:00Z")

</div>

Thanks for the mega-fast response 🙂 That works!

- GS

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 21, 2013, 8:34am UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/4 "2013-11-21T08:34:47Z")

</div>

​  
​  
when: listen\_ips|length \> 0​

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [November 21, 2013, 2:17pm UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/5 "2013-11-21T14:17:55Z")

</div>

Serge's response is fine but I believe you can also just do:

> - { include: tasks/repo\_server.yml, when: listen\_ips }

Python lists have a truthiness of False when empty.

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 21, 2013, 10:49pm UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/6 "2013-11-21T22:49:52Z")

</div>

Thanks Serge.

- GS

---

<div class="post-metadata">

**Author:** ![Gonzalo\_Servat](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gonzalo\_Servat](https://forum.ansible.com/u/Gonzalo_Servat)\
**Post date:** [November 21, 2013, 10:50pm UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/7 "2013-11-21T22:50:35Z")

</div>

This is the first thing that I tried, but it gave me an error about being unable to evaluate the conditional.

- GS

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 21, 2013, 11:46pm UTC](https://forum.ansible.com/t/checking-if-a-list-is-empty/13951/8 "2013-11-21T23:46:07Z")

</div>

“when: listen\_ips|length \> 0”

this.
