How to enumerate network interfaces in Ansible

Neik asked:

I’d like to get an ordered list of the network interfaces on a machine using Ansible. Modern Linux systems don’t use eth0, eth1, etc. So the names are unpredictable. On our network we connect the lowest numbered interface to the LAN and the highest to the WAN, so I can use the position of the interface in an ordered list to determine its function.

I am looking for the canonical way to do this in Ansible. So that I can use something like {{ansible_eth0.ipv4.address}}. (Where eth0 is some other name).

Even if I manually set a variable with the name of the interface there seems no way to get the IP of that interface (using the contents of the variable).

I’d like to process the Ansible facts to get what I want rather than running a shell script on the remote system.

My answer:

The ansible_interfaces fact lists all of the existing network interfaces.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.