On Thu, 13 Feb 2020 06:11:43 -0800 (PST)
Matthias Steffens <
matthia...@gmail.com> wrote:
> ... grep for the IP-address ... on every host with ansible and
> get an list of hosts where it's configured or not.
Try this
- set_fact:
my_hosts: "{{ hostvars|dict2items|json_query(query) }}"
vars:
query: "[?value.ansible_all_ipv4_addresses.contains(@,
'{{ ipv4_address }}')].value.ansible_hostname"
The variable "my_hosts" should keep the list of "ansible_hostname" with the
"ipv4_address" among "ansible_all_ipv4_addresses".
For example with the inventory
$ cat hosts
test_01 ansible_host=10.1.0.51
test_02 ansible_host=10.1.0.52
test_03 ansible_host=10.1.0.53
test_06 ansible_host=10.1.0.56
test_09 ansible_host=10.1.0.59
the playbook
- hosts: all
gather_facts: true
vars:
ipv4_address: 10.1.0.51
tasks:
- set_fact:
my_hosts: "{{ hostvars|dict2items|json_query(query) }}"
vars:
query: "[?value.ansible_all_ipv4_addresses.contains(@,
'{{ ipv4_address }}')].value.ansible_hostname"
run_once: true
- debug:
var: my_hosts
run_once: true
give
ok: [test_01] => {
"my_hosts": [
"test_01"
]
}
HTH,
-vlado