To list the target host's name and it's IP address using facts

I need to list the target host’s name and IP address using set_fact. Below is my playbook, but it’s giving error.

hosts: all
tasks:

  • set_fact:
    data: “{{ ansible_all_ipv4_addresses }}”

  • set_fact:
    ip: “{{ data.split(‘,’) }}”

  • name: Show the devices
    shell: echo {{ item }} >> /tmp/ips
    with_items: “{{ blockdevices }}”

Desired o/p is
host1 : 172.168.1.21
system2: 172.168.1.22

Anybody help me to fix this pls ? thanks in advance

Assuming each host has only one ip address. how about this?

for linux

- name: display ansible_default_ipv4.address

debug:

var: ansible_default_ipv4.address

for windows

  • name: display ansible_ip_addresses
    debug:
    var: ansible_ip_addresses

  • name: ansible_hostname
    debug:
    var: ansible_hostname

Each host has multiple Ip's. And i need to list those Ip's.