What is the best way to get the main network interface name for a Linux server with Ansible? This is often/usually eth0
but we can't always assume this is the case and it would be better to identify this dynamically.
We are configuring the firewall with Ansible so we need to be able to issue the interface name as part of the commands that we are using.
1. Log in to the system as root and run ifconfig -a plumb in a command shell. The command discovers all installed network interfaces.
The “ifconfig” command is used for displaying current network configuration information, setting up an ip address, netmask, or broadcast address to a network interface, creating an alias for the network interface, setting up hardware address, and enable or disable network interfaces.
You can use the ifconfig command to list the network interfaces available in your system. Instead of typing ifconfig, type the command /sbin/ifconfig to list the network interfaces in your system.
But (as far as I know) there is no obvious way in Ansible to query the interface based on an IP. We could use the shell module and call some ip a | sed | foo command to figure out the network interface but after searching the web for a while I finally figured out a way solve this directly in Ansible:
Without this setting, Ansible would attempt to use ssh to connect to the remote and execute the Python script on the network device, which would fail because Python generally isn’t available on network devices. Informs Ansible which Network platform this hosts corresponds to.
The ansible_interfaces fact lists all of the existing network interfaces. Show activity on this post. Some hints to get an interface when we know some information: Show activity on this post. I understand where Neik is coming from so after a bit of experimenting, I think I found a task that will do the trick.
ansible_connection: Ansible uses the ansible-connection setting to determine how to connect to a remote device. When working with Ansible Networking, set this to network_cli so Ansible treats the remote node as a network device with a limited execution environment.
That should be {{ ansible_default_ipv4.interface }}
. This is a system fact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With