Get all hosts with a hostvar defined

Hello,

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

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

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

1 Like

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)