Playbook that takes IP address of host machines and store these IPs

Hi,

Can anyone help me to get the playbook that collects the IP addresses of different host machines and store the output to a file.

Thanks,

Hi,

Can anyone help me to get the playbook that collects the IP addresses of different host machines and store the output to
a file.

Which method do you want to use to collect the IP addresses? Maybe you can pick from one of these inventory plugins at
https://docs.ansible.com/ansible/latest/plugins/inventory.html#plugin-list?

Regards
        Racke

Hi Stefan,

Thanks for the quick reply. Actually my requirement is to collect the all IP Addresses of different Host machines and store those IP’s to a file in the controller.

Thanks,

Can anyone help me to get the playbook that collects the IP addresses of
different host machines and store the output to a file.

The playbook and the template below stores first IP found of all hosts into
the file my_ips.txt in the current directory of the controller (localhost)

  > cat pb.yml
  - hosts: all
    gather_facts: true
    tasks:
      - template:
          src: my_ips.txt.j2
          dest: my_ips.txt
        run_once: true
        delegate_to: localhost

  > cat my_ips.txt.j2
  {% for my_host in groups.all %}
  {{ hostvars[my_host].ansible_all_ipv4_addresses.0 }}
  {% endfor %}

For example, with the inventory

  shell > cat hosts
  test_01 ansible_host=10.1.0.51
  test_02 ansible_host=10.1.0.52
  test_03 ansible_host=10.1.0.53
  test_06 ansible_host=10.1.0.56
  test_09 ansible_host=10.1.0.59

the playbook gives

  > cat my_ips.txt
  10.1.0.51
  10.1.0.52
  10.1.0.53
  10.1.0.56
  10.1.0.59

Notes to experiment and customise the playbook:

  * Change "hosts"
  * Change the list of hosts "groups.all"
  * Take all items of the list "ansible_all_ipv4_addresses"
  * "gather_facts: true" (default) is needed to collect
    "ansible_all_ipv4_addresses"
  * "run_once: true" is needed to run the task only once
  * "delegate_to: localhost" is needed to store the file at controller

HTH,

  -vlado

Hi Vladimir,

Thank you so much for the script, i will try it and will update you the results.

Thanks,

Hi,

Have you try that?

Thanks,
Amir