Highlighted
Enthusiast
Enthusiast

Fetching IP address of remote Host and add to inventory File

Hi everyone,

can anyone know how to fetch the IP-address of a remote host and add to the inventory file?

#insert hostname  into inventory

  - name: add to ansible hosts file

    lineinfile:

      dest: /etc/ansible/inventory

      insertbefore: 'product'

      line: '{{item.vmname }}   {{item.variable}}={{item.ansible_host}}'

      state: present

    with_items:

      - {vmname: '{{vmname}}', variable: "ansible_host", ansible_host: " {{ansible_default_ipv4.address}} "}

    run_once: true

    delegate_to: localhost

this play works perfect but it writes the IP-address of ansible host into inventory file not the IP-address of remote-host (vmname).

Tags (2)
0 Kudos
0 Replies