How can I search for an specific IP-Address on all of my Ansible-connected hosts

Hi everyone!

I’m looking for an method by which I can have a look on all of my ‘Ansible-Clients’ for an specific IP-Address.

I thought about grepping for this in my root-directory (/) but is this possible or are there any other better methods?

Hope you can help me to find a good solution.

Best regards,

Matthias

Try this

- hosts: all
  gather_facts: true
  tasks:
    - debug:
        var: ansible_all_ipv4_addresses

HTH,

  -vlado

Hi vlado,

thanks for your reply. But I’m looking for an method to grep for an Ip-address which is set in any configuration file on every of my 'Ansible-Clients.

Do you also have an idea how I can configure this with Ansible?

Regards,

Matthias

    > I'm looking for an method by which I can have a look on all of my
    > 'Ansible-Clients' for an specific IP-Address.

    Try this

    - hosts: all
     gather_facts: true
     tasks:
     - debug:
     var: ansible_all_ipv4_addresses

    HTH,

     -vlado

Hi vlado,

thanks for your reply. But I'm looking for an method to grep for an Ip-address which is set in any configuration file on
every of my 'Ansible-Clients.

Do you also have an idea how I can configure this with Ansible?

Hello Matthias,

it is unclear what you want to achieve with these ip addresses, but you can get a list of the configuration files with
the find module. Than you can use the command module to retrieve IP addresses with Unix grep command.

Be aware that you likely need regular expressions for both IPv4 and IPv6 adresses. Also you probably get false positives.

Regards
       Racke

Hi Racke,

thanks for your fast reply and sorry for the misleading problem. My task is to find out if an old relay-host with the IP-Address “192.168.110.45” is configured on
any of the hosts which are in my hosts-List configured. And therefore I thought that I can do it with an ‘grep’ on every system.

But it seems not to be so easy :(…

Hope, it’s now clearer what I wanna configure and someone can help me!?

Regards,
Matthias

Hello Matthias,

it is unclear what you want to achieve with these ip addresses, but you can get a list of the configuration files with
the find module. Than you can use the command module to retrieve IP addresses with Unix grep command.

Be aware that you likely need regular expressions for both IPv4 and IPv6 adresses. Also you probably get false positives.

Regards

Racke

Hi Racke,

thanks for your fast reply and sorry for the misleading problem. My task is to find out if an old relay-host with the
IP-Address "192.168.110.45" is configured on
any of the hosts which are in my hosts-List configured. And therefore I thought that I can do it with an 'grep' on every
system.

But it seems not to be so easy :(....

Hope, it's now clearer what I wanna configure and someone can help me!?

Can you give an example how such an entry looks like in the configuration file?

And a relay host can be configured as domain name instead of an IP.

Regards
           Racke

Hm, I don’t know how such an entry could look like. I’m only looking for this IP-address and if necessary for the FQDN as you’ve

correctly observed.

Therefore I thought I can grep for the IP-address in every file on every host with ansible and get an list of hosts where it’s configured or not…

Regards,
Matthias

Hm, I don't know how such an entry could look like. I'm only looking for this IP-address and if necessary for the FQDN
as you've
correctly observed.

Therefore I thought I can grep for the IP-address in every file on every host with ansible and get an list of hosts
where it's configured or not....

Seriously? At any rate this is now off topic IMHO.

Regards
         Racke

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