As per the Jinja documentation:

Beside “filters, there are also so-called “tests” available. Tests can be used to test a variable against a common expression.

Filters such as selectattr() can accept tests as a parameter. As per the documentation:

“selectattr() filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. If no test is specified, the attribute’s value will be evaluated as a boolean.

We seen an example of this on the selectattr () page where the undefined Test is passed as a parameter:

- set_fact:
    emails: "{{ users | selectattr('password', 'undefined') | map(attribute='email') | list }}"

Jinja & Ansible Tests

While Jinja provides numerous tests, Ansible brings some of its own to the table too. You can find their documentation here:

As always, if you have any questions or have a topic that you would like me to discuss, please feel free to post a comment at the bottom of this blog entry, e-mail at, or drop me a message on Reddit (OzNetNerd).

Note: The opinions expressed in this blog are my own and not those of my employer.

Leave a comment