It is very convenient to filter or modify a list using list comprehension in python. The same could be done in ansible playbook. However, the official documentation does not have explicit guides for these operations.

I found a very good post explained how to do this: How to filter, join and map lists in Ansible

Filter

The select filter can be used to filter a list. Indeed the select filter is not from ansible, it is from Jinjia. We can use all the Jinjia filters in ansible playbook. This filter takes two or more arguments. The first argument is name of a test. Rest of the arguments are for the test function. List of tests we can use:

Examples

"{{ a_list_of_string | select('match', '^Start_of_string.*') | list }}"
"{{ a_list_of_string | select('search', 'subet_of_string') | list }}"

The match and search tests are explained in Ansible tests. match requires a regular expression as argument for complete match. search only requires matching a subset of the string

Map

The map filter is also from Jinjia. It takes one or more arguments. The first argument is name of another filter. A simple example:

"{{ a_list_of_string | map('upper') | list }}"