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
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
Thanks for the mega-fast response That works!
when: listen_ips|length > 0
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.
This is the first thing that I tried, but it gave me an error about being unable to evaluate the conditional.
“when: listen_ips|length > 0”
this.