Jinja2 selectattr( ) Filter

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.

Example #1

This blog post does a great job of demonstrating how to use selectattr( ) . Let’s take a look at how it works.

First, the author creates a “users” dictionary:

Next, the author runs the following:

What we see here is:

  1. the “users” dictionary is being passed to the selectattr( )  filter.
  2. The selectattr( )  filter will cycle through all of the dictionaries (also known as objects) inside of “users”, looking for which ones do and do not have the “password” attribute specified.
  3. Because the ‘undefined’ Test has been passed as a parameter, only the objects which do not have a “password” attribute defined will be recorded.
  4. The map( ) filter is then used to return the “email” attribute of all objects that were recorded in the previous step.

Example #2

This StackOverflow response also has some great examples of how selectattr( )  can be used with both Jinja ( equalto( ) ) and Ansible ( match( )  and search( ) ) tests.

Using this dictionary:

We can obtain the “floating” value using any one of following filters:

The above lines result in the following output:

Knowledge Base

See the Ansible section of my Knowledge Base for more information.

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 will@oznetnerd.com, or drop me a message on Twitter (@OzNetNerd).

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

Leave a Reply

Your email address will not be published. Required fields are marked *