# Get all hosts with a hostvar defined

**URL:** <https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956>\
**Category:** Get Help\
**Created:** [December 4, 2025, 8:12pm UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956 "2025-12-04T20:12:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [December 4, 2025, 8:12pm UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956/1 "2025-12-04T20:12:17Z")

</div>

Hello,

I would like to have a dictionary with all hosts, that have “restic\_pull” defined:

```yaml
restic_pull_config: "{{ hostvars | json_query(*.restic_pull) }}"

# result:
TASK [restic_pull : Debug] *****************************************************************************
ok: [test-restic] => {
    "restic_pull_config": [
        {
            "high_prio": [
                "/etc",
                "/root"
            ]
        },
        {
            "high_prio": [
                "/config",
                "/root"
            ]
        }
    ]
}

```

Unfortunately, the hostname is missing… Any hint how to get the hostname as well?

Thanks a lot!  
Thomas

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [December 4, 2025, 8:14pm UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956/2 "2025-12-04T20:14:24Z")

</div>

You can create a group of those hosts based on that condition, with the `group_by` action

> **[ansible.builtin.group\_by module – Create Ansible groups based on facts —...](https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/group_by_module.html)**

---

<div class="post-metadata">

**Author:** ![VannTen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vannten/32/9911_2.png) [@VannTen](https://forum.ansible.com/u/VannTen)\
**Post date:** [December 5, 2025, 8:59am UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956/3 "2025-12-05T08:59:31Z")

</div>

You can probably do something with selectattr:

`{{ hostvars | dict2items | selectattr('value.restic_pull', 'defined') }}` (optionally add items2dict if you want to get back the original structure)

---

<div class="post-metadata">

**Author:** ![DengelFred](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Post date:** [December 5, 2025, 5:08pm UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956/4 "2025-12-05T17:08:06Z")

</div>

Thanks for your ideas 🙂 Solved it now with json\_query():

```yaml
    restic_pull_config:
      hosts: >-
        {{ hostvars | dict2items |
           json_query('[?value.restic_pull].{hostname: key, backup_sources: value.restic_pull}')
        }}

```

---

<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:** [January 4, 2026, 5:08pm UTC](https://forum.ansible.com/t/get-all-hosts-with-a-hostvar-defined/44956/5 "2026-01-04T17:08:53Z")

</div>

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