Fetching IP address of remote Host and add to inventory File

This post was originally published on this site

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).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.