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 hosts parameter specifies the target hosts on which this playbook will run. In this case, it is set to localhost, meaning the playbook will run on the local machine.

  • Line 4: Here, we set gather_facts to false, which means we won't collect any facts about the hosts before running the playbook.

  • Line 5: The vars section defines variables used in the playbook. In this case, we define a variable called people.

  • Line 6–15: The people variable is a list of dictionaries, each representing a person with attributes such as name, age, and gender.

  • Line 17: The tasks section 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 the debug module to display the output.

  • Line 20: Inside the task, we define a new variable called filtered_people.

  • Line 21–22: The value of filtered_people is determined by filtering the people variable using the selectattr filter twice:

    • people | selectattr('age', '>', 30) filters the people list to select only items where the age attribute is greater than 30.

    • The result of the previous filter is then further filtered using selectattr('gender', 'eq', 'male'), which selects only items with the gender attribute set to male.

    • Finally, the filtered result is converted into a list and assigned to the filtered_people variable.

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

Copyright ©2026 Educative, Inc. All rights reserved