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

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

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