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

**URL:** <https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216>\
**Category:** Ansible Project\
**Created:** [February 12, 2020, 9:00pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216 "2020-02-12T21:00:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias\_Steffens](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthias\_Steffens](https://forum.ansible.com/u/Matthias_Steffens)\
**Post date:** [February 12, 2020, 9:00pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/1 "2020-02-12T21:00:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 12, 2020, 10:46pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/2 "2020-02-12T22:46:04Z")

</div>

Try this

- hosts: all  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_all\_ipv4\_addresses

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Matthias\_Steffens](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthias\_Steffens](https://forum.ansible.com/u/Matthias_Steffens)\
**Post date:** [February 13, 2020, 12:34pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/3 "2020-02-13T12:34:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 13, 2020, 12:57pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/4 "2020-02-13T12:57:20Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;\> I'm looking for an method by which I can have a look on all of my  
> &nbsp;&nbsp;&nbsp;&nbsp;\> 'Ansible-Clients' for an specific IP-Address.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Try this
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- hosts: all  
> &nbsp;&nbsp;&nbsp;&nbsp; gather\_facts: true  
> &nbsp;&nbsp;&nbsp;&nbsp; tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp; - debug:  
> &nbsp;&nbsp;&nbsp;&nbsp; var: ansible\_all\_ipv4\_addresses
> 
> &nbsp;&nbsp;&nbsp;&nbsp;HTH,
> 
> &nbsp;&nbsp;&nbsp;&nbsp; -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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Matthias\_Steffens](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthias\_Steffens](https://forum.ansible.com/u/Matthias_Steffens)\
**Post date:** [February 13, 2020, 1:43pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/5 "2020-02-13T13:43:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 13, 2020, 1:59pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/6 "2020-02-13T13:59:26Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Matthias\_Steffens](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthias\_Steffens](https://forum.ansible.com/u/Matthias_Steffens)\
**Post date:** [February 13, 2020, 2:11pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/7 "2020-02-13T14:11:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 13, 2020, 2:17pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/8 "2020-02-13T14:17:07Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 13, 2020, 5:03pm UTC](https://forum.ansible.com/t/how-can-i-search-for-an-specific-ip-address-on-all-of-my-ansible-connected-hosts/32216/9 "2020-02-13T17:03:35Z")

</div>

Try this

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts: "{{ hostvars|dict2items|json\_query(query) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: "[?value.ansible\_all\_ipv4\_addresses.contains(@,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'{{ 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  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;ipv4\_address: 10.1.0.51  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts: "{{ hostvars|dict2items|json\_query(query) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query: "[?value.ansible\_all\_ipv4\_addresses.contains(@,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'{{ ipv4\_address }}')].value.ansible\_hostname"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

give

ok: [test\_01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"my\_hosts": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"test\_01"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

HTH,

&nbsp;&nbsp;-vlado
