If I had a list of ip addresses in Ansible, I could use the ipaddr
filter to it and only get the passing values back:
- debug:
msg: "{{ ['127.0.0.1', 'foo', '2001:db8:32c:faad::'] | ipaddr('address') }}"
Unfortunately I am working with a list of objects (hostvars of group members to be precise). I want to do some tests on the list and only keeping the entries passing - but as objects.
When reading the Jinja docs I stumbled across selectattr
. Unfortunately it seems that ipaddr
isn't a test, so it doesn't work:
- debug:
msg: "{{ [{'ip':'127.0.0.1'}, {'ip':'foo'}, {'ip':'2001:db8:32c:faad::'}] | selectattr('ip', 'ipaddr', 'address') | list}} "
results in
jinja2.exceptions.TemplateRuntimeError: no test named 'ipaddr'
Is there any way to use ipaddr
to filter a list of objects?