How to filter a list by its attributes in Ansible
Ansible is an open-source automation tool that allows you to automate tasks, configuration management, and application deployment across your IT infrastructure. It uses human-readable YAML files called playbooks to define tasks, making it easy to understand and use.
Filter a list by its attributes
To filter a list in Ansible by its attributes, you can use the selectattr filter. selectattr is a filtering function in Ansible used to select items from a list of dictionaries based on the attributes' values of those dictionaries. It allows you to filter elements within a list according to specific criteria. The basic syntax of selectattr is as follows:
sequence | selectattr(attribute, operator, value)
sequence: The input sequence, usually a list of dictionaries.
attribute: The attribute of each dictionary within the sequence that you want to filter on.
operator: The comparison operator to be used for filtering. Common operators are==,!=,>,>=,<, and<=.
value: The value you want to compare the attribute against.
Here's an example of how to use selectattr in Ansible:
---
- name: Filter a list in Ansible by attributes
hosts: localhost
gather_facts: false
vars:
people:
- name: John
age: 25
gender: male
- name: Jane
age: 35
gender: female
- name: Bob
age: 40
gender: male
tasks:
- name: Filter people by attributes
debug:
var: filtered_people
vars:
filtered_people: "{{ people | selectattr('age', '>', 30) | selectattr('gender', 'eq', 'male') | list }}"
Code explanation
Line 2: This line defines the name of the playbook, which is
Filter a list in Ansible by attributes.Line 3: The
hostsparameter specifies the target hosts on which this playbook will run. In this case, it is set tolocalhost, meaning the playbook will run on the local machine.Line 4: Here, we set
gather_factstofalse, which means we won't collect any facts about the hosts before running the playbook.Line 5: The
varssection defines variables used in the playbook. In this case, we define a variable calledpeople.Line 6–15: The
peoplevariable is a list of dictionaries, each representing a person with attributes such asname,age, andgender.Line 17: The
taskssection contains the list of tasks to be executed on the target hosts.Line 18–19: The task is named
'Filter people by attributes', and it uses thedebugmodule to display the output.Line 20: Inside the task, we define a new variable called
filtered_people.Line 21–22: The value of
filtered_peopleis determined by filtering thepeoplevariable using theselectattrfilter twice:people | selectattr('age', '>', 30)filters thepeoplelist to select only items where theageattribute is greater than30.The result of the previous filter is then further filtered using
selectattr('gender', 'eq', 'male'), which selects only items with thegenderattribute set tomale.Finally, the filtered result is converted into a list and assigned to the
filtered_peoplevariable.
Expected output
When you run this Ansible playbook, it will print the filtered_people variable containing the filtered list of people who are older than 30 and are male. The output will look like this:
TASK [Filter people by attributes] ***************************************ok: [localhost] => {"filtered_people": [{"age": 40,"gender": "male","name": "Bob"}]}
As you can see, only Bob's details are present in the filtered_people variable as he is the only person in the people list who is older than 30 and is male.
Free Resources