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

110 views
Skip to first unread message

Matthias Steffens

unread,
Feb 12, 2020, 4:00:55 PM2/12/20
to Ansible Project
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

Vladimir Botka

unread,
Feb 12, 2020, 5:46:21 PM2/12/20
to Matthias Steffens, ansible...@googlegroups.com
On Wed, 12 Feb 2020 13:00:55 -0800 (PST)
Matthias Steffens <matthia...@gmail.com> wrote:

> 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

Matthias Steffens

unread,
Feb 13, 2020, 7:34:48 AM2/13/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Feb 13, 2020, 7:57:51 AM2/13/20
to ansible...@googlegroups.com
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

>
> Regards,
>
> Matthias
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Matthias Steffens

unread,
Feb 13, 2020, 8:43:38 AM2/13/20
to Ansible Project
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

Stefan Hornburg (Racke)

unread,
Feb 13, 2020, 8:59:57 AM2/13/20
to ansible...@googlegroups.com
On 2/13/20 2:43 PM, Matthias Steffens wrote:
> 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
> > ansible...@googlegroups.com <javascript:> <mailto:ansible-proje...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

Matthias Steffens

unread,
Feb 13, 2020, 9:11:44 AM2/13/20
to Ansible Project
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
 
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

>     > ansible...@googlegroups.com <javascript:> <mailto:ansible-project+unsub...@googlegroups.com <javascript:>>.
>     > To view this discussion on the web visit
>     > https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com
>     <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com>
>     >
>     <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer
>     <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
>     --
>     Ecommerce and Linux consulting + Perl and web application programming.
>     Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

Stefan Hornburg (Racke)

unread,
Feb 13, 2020, 9:17:43 AM2/13/20
to ansible...@googlegroups.com
On 2/13/20 3:11 PM, Matthias Steffens wrote:
> 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
> >     > ansible...@googlegroups.com <javascript:> <mailto:ansible-proje...@googlegroups.com <javascript:>
> <javascript:>>.
> >     > To view this discussion on the web visit
> >     > https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com>
> >     <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com>>
> >     >
> >    
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer>
>
> >    
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/5ea08527-3526-4efe-ac72-0dac1a09ddce%40googlegroups.com?utm_medium=email&utm_source=footer>>>.
>
> >
> >
> >
> >     --
> >     Ecommerce and Linux consulting + Perl and web application programming.
> >     Debian and Sympa administration. Provisioning with Ansible.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible...@googlegroups.com <javascript:> <mailto:ansible-proje...@googlegroups.com <javascript:>>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com>
> >
> <https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/1d5fe70c-2b07-4579-953e-4252e4c84c3c%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1261177d-19ba-41dc-8fed-94773659e6d3%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1261177d-19ba-41dc-8fed-94773659e6d3%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

Vladimir Botka

unread,
Feb 13, 2020, 12:04:02 PM2/13/20
to Matthias Steffens, ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages