# Filter list of variables by their value

**URL:** <https://forum.ansible.com/t/filter-list-of-variables-by-their-value/40698>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [February 28, 2025, 8:45am UTC](https://forum.ansible.com/t/filter-list-of-variables-by-their-value/40698 "2025-02-28T08:45:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [February 28, 2025, 8:45am UTC](https://forum.ansible.com/t/filter-list-of-variables-by-their-value/40698/1 "2025-02-28T08:45:36Z")

</div>

Hi,  
I’m trying to filter a list of variables by their values, and I want to do this without looping, so only with filters.

I have these variables:

```yaml
    # flags
    foo_service_enable: true
    bar_service_enable: true
    boz_service_enable: false
    db_service_enable: false

    # group of services
    customer_services:
      - foo_service_enable
      - bar_service_enable
      - boz_service_enable
      - db_service_enable

```

I am looking to find the list of enabled services, so:

```yaml
enabled_stuff:
  - foo_service_enable
  - bar_service_enable

```

I can lookup the values with:

```auto
customer_services|map('extract',vars)

```

but that is just a list of booleans.  
How can I use that to select only the enable services?

FYI i use this demo playbook:

```yaml
---
- hosts: localhost
  connection: local
  gather_facts: no

  tasks:

    - debug: var=customer_services
    - debug: var=customer_services|map('extract',vars)

  vars:
    # flags
    foo_service_enable: true
    bar_service_enable: true
    boz_service_enable: false
    db_service_enable: false

    # group of services
    customer_services:
      - foo_service_enable
      - bar_service_enable
      - boz_service_enable
      - db_service_enable

```

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [February 28, 2025, 9:49am UTC](https://forum.ansible.com/t/filter-list-of-variables-by-their-value/40698/2 "2025-02-28T09:49:35Z")

</div>

You could use [zip filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/zip_filter.html) to combine the keys and value into list of tuples and then filter and map the keys.

```auto
customer_services | zip(customer_services | map('extract', vars)) | selectattr('1') | map(attribute='0')

```

Although for sanity, I’d suggest to use dictionary instead.

```auto
    - ansible.builtin.debug:
        msg: "{{ customer_services | dict2items | selectattr('value') | map(attribute='key') }}"
      vars:
        customer_services:
          foo_service_enable: true
          bar_service_enable: true
          boz_service_enable: false
          db_service_enable: false

```

If you really need the variables to be top-level, you could proxy the values:

```auto
      customer_services:
        foo_service_enable: "{{ foo_service_enable }}"
        bar_service_enable: "{{ bar_service_enable }}"
        boz_service_enable: "{{ boz_service_enable }}"
        db_service_enable: "{{ db_service_enable }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 30, 2025, 9:50am UTC](https://forum.ansible.com/t/filter-list-of-variables-by-their-value/40698/3 "2025-03-30T09:50:21Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
