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