Trying to use selectattr to filter where attribute is not an empty list

2,028 views
Skip to first unread message

Mark Gibson

unread,
Jul 5, 2016, 8:44:27 AM7/5/16
to Ansible Project
Hi

I'm trying to select a set of objects where an attribute (of type list) is not empty.

mylist:
 
- name: name1
    list
: []
 
- name: name2
    list
:
     
- item1
     
- item2


There seems to be no jinja2 test (in the documentation at least) that allows one to test on empty / notempty.

I want to be able to do something like:

mylist|selectattr( 'list', 'notempty' )

Is there a nice Ansible/Jinja2 trick that I'm missing that can do what I want?

Thanks
Mark

mco...@academicworks.com

unread,
Jun 21, 2017, 5:30:23 PM6/21/17
to Ansible Project
I just had this exact problem. Hopefully this is helpful to anyone else who comes across this thread.

An empty list is falsey, so 

mylist|selectattr('list')

should get what you're looking for, and 

mylist|rejectattr('list')

would return elements that have an empty list.
Reply all
Reply to author
Forward
0 new messages