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

201 views
Skip to first unread message

Pandu jh

unread,
Sep 6, 2018, 10:57:57 AM9/6/18
to Ansible Project
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

Tony Chia

unread,
Sep 6, 2018, 2:41:40 PM9/6/18
to Ansible Project
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

Pandu jh

unread,
Sep 6, 2018, 10:41:47 PM9/6/18
to Ansible Project
Each host has multiple Ip's. And i need to list those Ip's.
Reply all
Reply to author
Forward
0 new messages