Ansible Pilot

Filter A List By Its Attributes - Ansible selectattr filter

How to list only the enabled features in a network interface using Ansible System Information (Facts) and selectattr filter.

How to Filter A List By Its Attributes in an Ansible Playbook?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

selectattr filter in Ansible Playbook?

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.


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).


- name: selectattr demo
  hosts: all
  gather_facts: true
    - name: all features
        var: 'ansible_facts.eth1.features'
    - name: filter enabled
        msg: "{{ (ansible_facts.eth1.features | dict2items | selectattr('value', 'match', 'on') ) }}"


ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/selectattr.yml
PLAY [selectattr demo] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: []
TASK [all features] *******************************************************************************
ok: [] => {
    "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_rx_offload": "off [fixed]",
        "tls_hw_tx_offload": "off [fixed]",
        "tx_checksum_fcoe_crc": "off [fixed]",
        "tx_checksum_ip_generic": "on",
        "tx_checksum_ipv4": "off [fixed]",
        "tx_checksum_ipv6": "off [fixed]",
        "tx_checksum_sctp": "off [fixed]",
        "tx_checksumming": "on",
        "tx_esp_segmentation": "off [fixed]",
        "tx_fcoe_segmentation": "off [fixed]",
        "tx_gre_csum_segmentation": "off [fixed]",
        "tx_gre_segmentation": "off [fixed]",
        "tx_gso_list": "off [fixed]",
        "tx_gso_partial": "off [fixed]",
        "tx_gso_robust": "off [fixed]",
        "tx_ipxip4_segmentation": "off [fixed]",
        "tx_ipxip6_segmentation": "off [fixed]",
        "tx_lockless": "off [fixed]",
        "tx_nocache_copy": "off",
        "tx_scatter_gather": "on",
        "tx_scatter_gather_fraglist": "off [fixed]",
        "tx_sctp_segmentation": "off [fixed]",
        "tx_tcp6_segmentation": "off [fixed]",
        "tx_tcp_ecn_segmentation": "off [fixed]",
        "tx_tcp_mangleid_segmentation": "off",
        "tx_tcp_segmentation": "on",
        "tx_tunnel_remcsum_segmentation": "off [fixed]",
        "tx_udp_segmentation": "off [fixed]",
        "tx_udp_tnl_csum_segmentation": "off [fixed]",
        "tx_udp_tnl_segmentation": "off [fixed]",
        "tx_vlan_offload": "on [fixed]",
        "tx_vlan_stag_hw_insert": "off [fixed]",
        "vlan_challenged": "off [fixed]"
TASK [filter enabled] *****************************************************************************
ok: [] => {
    "msg": [
            "key": "tx_checksumming",
            "value": "on"
            "key": "tx_checksum_ip_generic",
            "value": "on"
            "key": "scatter_gather",
            "value": "on"
            "key": "tx_scatter_gather",
            "value": "on"
            "key": "tcp_segmentation_offload",
            "value": "on"
            "key": "tx_tcp_segmentation",
            "value": "on"
            "key": "generic_segmentation_offload",
            "value": "on"
            "key": "generic_receive_offload",
            "value": "on"
            "key": "rx_vlan_offload",
            "value": "on"
            "key": "tx_vlan_offload",
            "value": "on [fixed]"
            "key": "rx_vlan_filter",
            "value": "on [fixed]"
PLAY RECAP ****************************************************************************************           : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

code with ❤️ in GitHub


Now you know how to Filter A List By Its Attributes in an Ansible Playbook. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.


Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Follow me

Subscribe not to miss any new releases

May 9, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress