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

**URL:** <https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773>\
**Category:** Ansible Project\
**Created:** [April 24, 2020, 7:43am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773 "2020-04-24T07:43:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PMP](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PMP](https://forum.ansible.com/u/PMP)\
**Post date:** [April 24, 2020, 7:43am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/1 "2020-04-24T07:43:14Z")

</div>

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,

---

<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:** [April 24, 2020, 7:49am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/2 "2020-04-24T07:49:29Z")

</div>

> 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](https://docs.ansible.com/ansible/latest/plugins/inventory.html#plugin-list)?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![PMP](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PMP](https://forum.ansible.com/u/PMP)\
**Post date:** [April 24, 2020, 8:10am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/3 "2020-04-24T08:10:48Z")

</div>

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,

---

<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:** [April 24, 2020, 8:17am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/4 "2020-04-24T08:17:10Z")

</div>

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

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)

&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: my\_ips.txt.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: my\_ips.txt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

&nbsp;&nbsp;\> cat my\_ips.txt.j2  
&nbsp;&nbsp;{% for my\_host in groups.all %}  
&nbsp;&nbsp;{{ hostvars[my\_host].ansible\_all\_ipv4\_addresses.0 }}  
&nbsp;&nbsp;{% endfor %}

For example, with the inventory

&nbsp;&nbsp;shell \> cat hosts  
&nbsp;&nbsp;test\_01 ansible\_host=10.1.0.51  
&nbsp;&nbsp;test\_02 ansible\_host=10.1.0.52  
&nbsp;&nbsp;test\_03 ansible\_host=10.1.0.53  
&nbsp;&nbsp;test\_06 ansible\_host=10.1.0.56  
&nbsp;&nbsp;test\_09 ansible\_host=10.1.0.59

the playbook gives

&nbsp;&nbsp;\> cat my\_ips.txt  
&nbsp;&nbsp;10.1.0.51  
&nbsp;&nbsp;10.1.0.52  
&nbsp;&nbsp;10.1.0.53  
&nbsp;&nbsp;10.1.0.56  
&nbsp;&nbsp;10.1.0.59

Notes to experiment and customise the playbook:

&nbsp;&nbsp;\* Change "hosts"  
&nbsp;&nbsp;\* Change the list of hosts "groups.all"  
&nbsp;&nbsp;\* Take all items of the list "ansible\_all\_ipv4\_addresses"  
&nbsp;&nbsp;\* "gather\_facts: true" (default) is needed to collect  
&nbsp;&nbsp;&nbsp;&nbsp;"ansible\_all\_ipv4\_addresses"  
&nbsp;&nbsp;\* "run\_once: true" is needed to run the task only once  
&nbsp;&nbsp;\* "delegate\_to: localhost" is needed to store the file at controller

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![PMP](https://avatars.discourse-cdn.com/v4/letter/p/b77776/32.png) [@PMP](https://forum.ansible.com/u/PMP)\
**Post date:** [April 24, 2020, 3:56pm UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/5 "2020-04-24T15:56:43Z")

</div>

Hi Vladimir,

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

Thanks,

---

<div class="post-metadata">

**Author:** ![Amir\_Khalili](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Amir\_Khalili](https://forum.ansible.com/u/Amir_Khalili)\
**Post date:** [April 25, 2020, 11:47am UTC](https://forum.ansible.com/t/playbook-that-takes-ip-address-of-host-machines-and-store-these-ips/32773/6 "2020-04-25T11:47:52Z")

</div>

Hi,

Have you try that?

Thanks,  
Amir
