AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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

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

Watch on YouTube · Read the written article

Tutorial summary

What you'll learn

  • How to Filter A List By Its Attributes in an Ansible Playbook?
  • selectattr filter in Ansible Playbook?
  • Links
  • code
  • execution
  • Conclusion
  • Basic selectattr
  • Test Operators
  • rejectattr (Inverse)
  • Chain with map
How to Filter A List By Its Attributes in an Ansible Playbook? 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. Links - [Data manipulation](https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html) - [Jinja selectattr](https://jinja.palletsprojects.com/en/latest/templates/#jinja-filters.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 ```yaml --- - name: selectattr Playbook hosts: all gather_facts: true tasks: - name: all features ansible.builtin.debug: var: 'ansible_facts.eth1.features' - name: filter enabled ansible.builtin.debug: msg: "{{ (ansible_facts.eth1.features | dict2items | selectattr('value', 'match', 'on') ) }}" ``` execution ```bash ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/selectattr.yml PLAY [selectattr Playbook] **************************************************************************** TASK [Gathering Facts] **************************************************************************** ok: [demo.example.com] TASK [all features] ******************************************************************************* ok: [demo.example.com] => { "ansible_facts.eth1.features": { "esp_hw_offload": "off [fixed]", "esp_tx_csum_hw_offload": "off [fixed]", "fcoe_mtu": "off [fixed]", "generic_receive_offload": "on", "generic_segmentation_offload": "on", "highdma": "off [fixed]", "hw_tc_offload": "off [fixed]", "l2_fwd_offload": "off [fixed]", "large_receive_offload": "off [fixed]", "loopback": "off [fixed]", "netns_local": "off [fixed]", "ntuple_filters": "off [fixed]", "receive_hashing": "off [fixed]", "rx_all": "off", "rx_checksumming": "off", "rx_fcs": "off", "rx_gro_hw": "off [fixed]", "rx_gro_list": "off", "rx_udp_gro_forwarding": "off", "rx_udp_tunnel_port_offload": "off [fixed]", "rx_vlan_filter": "on [fixed]", "rx_vlan_offload": "on", "rx_vlan_stag_filter": "off [fixed]", "rx_vlan_stag_hw_parse": "off [fixed]", "scatter_gather": "on", "tcp_segmentation_offload": "on", "tls_hw_record": "off [fixed]", "tls_hw

About this tutorial

  • Author: Luca Berton
  • Difficulty: Advanced
  • Read time: 8 min
  • Category: installation

Topics covered

Related video tutorials