# GET IP AND HOST NAME AND SAVE IT TO FILE IN LOCALHOST

**URL:** <https://forum.ansible.com/t/get-ip-and-host-name-and-save-it-to-file-in-localhost/34185>\
**Category:** Ansible Developer\
**Created:** [November 12, 2020, 12:23pm UTC](https://forum.ansible.com/t/get-ip-and-host-name-and-save-it-to-file-in-localhost/34185 "2020-11-12T12:23:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gr00t](https://avatars.discourse-cdn.com/v4/letter/g/65b543/32.png) [@gr00t](https://forum.ansible.com/u/gr00t)\
**Post date:** [November 12, 2020, 12:23pm UTC](https://forum.ansible.com/t/get-ip-and-host-name-and-save-it-to-file-in-localhost/34185/1 "2020-11-12T12:23:32Z")

</div>

HI,

I am trying to create a task that will get the host target IP address and host-name that are in the inventory then save it to a file in the local directory.

Would love to save the data like this:

**hostname:ip-address**

Kindly assist, don’t know where to start?

/gr00t

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [November 12, 2020, 3:33pm UTC](https://forum.ansible.com/t/get-ip-and-host-name-and-save-it-to-file-in-localhost/34185/2 "2020-11-12T15:33:51Z")

</div>

Something like this will do the trick:

- name: Content  
hosts: elkmaster  
gather\_facts: no  
vars:  
report\_file: “/home/jrua/report.txt”  
tasks:

- name: create file  
file:  
dest: “{{ report\_file }}”  
state: touch  
delegate\_to: localhost

- name: copy content  
lineinfile:  
line: “{{ hostvars[inventory\_hostname][‘inventory\_hostname\_short’] + ‘:’ + hostvars[inventory\_hostname][‘ip’] }}”  
dest: “{{ report\_file }}”  
insertafter: EOF  
delegate\_to: localhost  
loop: “{{ groups[‘elkmaster’] }}”

Using as inventory :

[elkmaster]  
es\_master ip=‘1.1.1.1’  
es\_data01 ip=‘2.3.3.4’

[elkdata]  
es\_data01 ip=‘2.3.3.4’  
es\_data02 ip=‘21.21.21.1’

Results in:

es\_master:1.1.1.1  
es\_data01:2.3.3.4

HTH,  
Regards
