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

22 views
Skip to first unread message

gr00t

unread,
Nov 12, 2020, 7:23:32 AM11/12/20
to Ansible Development
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

Jorge Rúa

unread,
Nov 12, 2020, 10:34:11 AM11/12/20
to gr00t, Ansible Development
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






--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/5d83bbf6-23d7-4c31-a7f6-52631e14816cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages