How to Find IP address assigned to each interface on Linux Using Ansible

ansible

I am working to write playbook which should collect the basic server information before performing any task, I want to collect Interfacename/IP address/Netmask assigned to each interface.

Example – I have two interface – eth0 & eth1 and both are configured with different IP.

I using ansible facts to collect these info, It is working fine with single interface but the information losses the order if there are more interface on a a machine. I want to loop through each interface and collect these information but unable to get in order.

  Network_Info : Interface Name : {{ ansible_default_ipv4['alias'] }}
  Network_Info : IP Address     : {{ ansible_default_ipv4['address'] }}
  Network_Info : Netmask        : {{ ansible_default_ipv4['netmask']}}

Can some one please help me with this.

Thanks
Kamlesh

Best Answer

Give it a try. The ansible_interfaces fact contains this information.

tasks:
  - name: debug interface facts
    debug:
      msg: "{{ hostvars[inventory_hostname]['ansible_%s' | format(item)] }}"
    loop: "{{ ansible_interfaces }}"