Checking if a list is empty...

42,316 views
Skip to first unread message

Gonzalo Servat

unread,
Nov 21, 2013, 1:16:20 AM11/21/13
to ansible-project
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


Kahlil Hodgson

unread,
Nov 21, 2013, 1:18:51 AM11/21/13
to ansible...@googlegroups.com
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
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

Gonzalo Servat

unread,
Nov 21, 2013, 1:21:00 AM11/21/13
to ansible-project
Thanks for the mega-fast response :) That works!

- GS

Serge van Ginderachter

unread,
Nov 21, 2013, 3:34:47 AM11/21/13
to ansible...@googlegroups.com

On 21 November 2013 07:16, Gonzalo Servat <gse...@gmail.com> wrote:
​​
when: listen_ips > 0

when: listen_ips|length > 0

C. Morgan Hamill

unread,
Nov 21, 2013, 9:17:55 AM11/21/13
to ansible-project
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.
--
Morgan Hamill

Gonzalo Servat

unread,
Nov 21, 2013, 5:49:52 PM11/21/13
to ansible-project
Thanks Serge.

- GS

Gonzalo Servat

unread,
Nov 21, 2013, 5:50:35 PM11/21/13
to ansible-project
This is the first thing that I tried, but it gave me an error about being unable to evaluate the conditional.

- GS

Michael DeHaan

unread,
Nov 21, 2013, 6:46:07 PM11/21/13
to ansible...@googlegroups.com
"when: listen_ips|length > 0"

this.


--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Reply all
Reply to author
Forward
0 new messages