Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Linux network interface name with Ansible?

Tags:

linux

ansible

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.

like image 340
Chris Avatar asked Aug 24 '15 18:08

Chris


People also ask

How do I find my network interface name in Linux?

1. Log in to the system as root and run ifconfig -a plumb in a command shell. The command discovers all installed network interfaces.

What is network interface command in Linux?

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.

How do I find my network interface Debian?

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.

How to query the interface based on IP in Ansible?

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:

Why does Ansible fail to run Python scripts on my Device?

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.

What does Ansible_interfaces do?

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.

How do I connect Ansible to a remote device?

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.


1 Answers

That should be {{ ansible_default_ipv4.interface }}. This is a system fact.

like image 86
udondan Avatar answered Oct 04 '22 18:10

udondan