Ansible selectattr Filter: Filter Lists by Attributes (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

How to use Ansible selectattr filter to filter lists by attributes. Select items matching conditions, combine with map and reject filters with examples.

How to Filter A List By Its Attributes in an Ansible Playbook? I'm going to show you a live Playbook with some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.

selectattr filter in Ansible Playbook? • {{ users|selectattr("is_active") }} • {{ users|selectattr("email", "none") }}

Today we're talking about Ansible selectattr Jinja filter. 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. The two examples explain how-to for a specific attribute or value using a users list example.

LinksData manipulationJinja selectattr

## Playbook

How to filter a list by its attributes in an Ansible Playbook. I'm going to use the selectattr filter to select only one information from Ansible System Information (Facts). Specifically, I'm going to filter only for enabled features in a network interface (eth1).

code

execution

code with ❤️ in GitHub

Conclusion Now you know how to Filter A List By Its Attributes in an Ansible Playbook.

Basic selectattr

Test Operators

| Operator | Example | Description | |----------|---------|-------------| | eq / equalto | selectattr('role', 'eq', 'admin') | Equals | | ne | selectattr('status', 'ne', 'disabled') | Not equals | | match | selectattr('name', 'match', '^web') | Regex match | | search | selectattr('desc', 'search', 'important') | Regex search | | in | selectattr('env', 'in', ['prod', 'staging']) | In list | | defined | selectattr('email', 'defined') | Attribute exists | | undefined | selectattr('phone', 'undefined') | Attribute missing | | truthy | selectattr('active') | Truthy value |

rejectattr (Inverse)

Chain with map

Complex Filtering

Practical Examples

Find hosts needing updates

Filter packages by state

Find matching strings in list

FAQ

selectattr vs select? • selectattr: Filter list of dicts by attribute • select: Filter simple list by test

Why do I need | list at the end?

selectattr returns a generator. Pipe to list to materialize it, especially when using length or accessing by index.

Can I use selectattr in when conditions?

Basic selectattr

Common Tests

selectattr + map

rejectattr (Inverse)

Practical: Filter Services

Practical: Filter Inventory

Chain Multiple Filters

With Loops

FAQ

selectattr vs json_query?

selectattr is simpler — use for single-attribute filtering. json_query (JMESPath) handles complex nested queries. Start with selectattr, use json_query when needed.

How to filter by nested attribute?

selectattr doesn't support dot notation. Flatten the data first or use json_query:

selectattr returns a generator?

Yes — always add | list at the end to materialize the result.

Related ArticlesAnsible Template GuideAnsible Inventory GuideAnsible Loops Guide

Category: installation

Watch the video: Ansible selectattr Filter: Filter Lists by Attributes (Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home