Playbook that takes IP address of host machines and store these IPs

43 views
Skip to first unread message

PMP

unread,
Apr 24, 2020, 3:43:14 AM4/24/20
to Ansible Project
Hi,

Can anyone help me to get the playbook that collects the IP addresses of different host machines and store the output to a file.

Thanks,

Stefan Hornburg (Racke)

unread,
Apr 24, 2020, 3:49:55 AM4/24/20
to ansible...@googlegroups.com
Which method do you want to use to collect the IP addresses? Maybe you can pick from one of these inventory plugins at
https://docs.ansible.com/ansible/latest/plugins/inventory.html#plugin-list?

Regards
Racke

> Thanks,
>
> --
> 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/c7a90fe8-af8c-4bcd-99ca-ea051ed1b892%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/c7a90fe8-af8c-4bcd-99ca-ea051ed1b892%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

PMP

unread,
Apr 24, 2020, 4:10:48 AM4/24/20
to Ansible Project
Hi Stefan,

Thanks for the quick reply. Actually my requirement is to collect the all IP Addresses of different Host machines and store those IP's to a file in the controller.

Thanks,

On Friday, April 24, 2020 at 1:19:55 PM UTC+5:30, Stefan Hornburg (Racke) wrote:
On 4/24/20 9:43 AM, PMP wrote:
> Hi,
>
> Can anyone help me to get the playbook that collects the IP addresses of different host machines and store the output to
> a file.
>

Which method do you want to use to collect the IP addresses? Maybe you can pick from one of these inventory plugins at
https://docs.ansible.com/ansible/latest/plugins/inventory.html#plugin-list?

Regards
        Racke

> Thanks,
>
> --
> 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

Vladimir Botka

unread,
Apr 24, 2020, 4:17:28 AM4/24/20
to PMP, ansible...@googlegroups.com
The playbook and the template below stores first IP found of all hosts into
the file my_ips.txt in the current directory of the controller (localhost)

shell> cat pb.yml
- hosts: all
gather_facts: true
tasks:
- template:
src: my_ips.txt.j2
dest: my_ips.txt
run_once: true
delegate_to: localhost

shell> cat my_ips.txt.j2
{% for my_host in groups.all %}
{{ hostvars[my_host].ansible_all_ipv4_addresses.0 }}
{% endfor %}

For example, with the inventory

shell > 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 gives

shell> cat my_ips.txt
10.1.0.51
10.1.0.52
10.1.0.53
10.1.0.56
10.1.0.59

Notes to experiment and customise the playbook:

* Change "hosts"
* Change the list of hosts "groups.all"
* Take all items of the list "ansible_all_ipv4_addresses"
* "gather_facts: true" (default) is needed to collect
"ansible_all_ipv4_addresses"
* "run_once: true" is needed to run the task only once
* "delegate_to: localhost" is needed to store the file at controller


HTH,

-vlado

PMP

unread,
Apr 24, 2020, 11:56:44 AM4/24/20
to Ansible Project
Hi Vladimir,

Thank you so much for the script, i will try it and will update you the results.

Thanks,

Amir Khalili

unread,
Apr 25, 2020, 7:47:53 AM4/25/20
to Ansible Project
Hi,

Have you try that?

Thanks,
Amir
Reply all
Reply to author
Forward
0 new messages