Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred fallback network interfaces with Vagrant?

Tags:

vagrant

My Vagrant boxes use public networking so they can advertise themselves over zeroconf/Bonjour. The Vagrantfile explicitly sets the bridged network interface:

  config.vm.network :public_network, :bridge => 'en2: USB Ethernet'

Most of the time everything just works, but if I'm connected via a different network and the specified interface doesn't exist, vagrant up will prompt me to pick from the available network interfaces:

[default] Specific bridge 'en2: USB Ethernet' not found. You may be asked to specify
which network to bridge to.
[default] Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) p2p0
What interface should the network bridge to?

Is there a way to tell Vagrant to choose from a list of preferred network interfaces? What I want is a graceful fallback if the primary network isn't available.

like image 686
joemaller Avatar asked Jul 18 '13 17:07

joemaller


People also ask

Which interface should the network bridge to vagrant?

Default Network Interface If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.

How do I set up a vagrant network?

Configure port forwarding This allows you to access a port on your own machine, but actually have all the network traffic forwarded to a specific port on the guest machine. To set up a forwarded port so you can access Apache on your guest, add the config. vm.network parameter to your Vagrantfile.

How do I change my IP address on Vagrant?

To configure private or host-only networking in Vagrant with static IP address, open the Vagrantfile, find the following line and uncomment it. Here, 192.168. 121.60 is the IP address of the VM. Replace this with your own IP.


1 Answers

Here's a solution I came up with that seems to be working well so far:

In Vagrantfile, add the following to the top of the file:

pref_interface = ['en2: USB Ethernet', 'en0: Wi-Fi (AirPort)']
vm_interfaces = %x( VBoxManage list bridgedifs | grep ^Name ).gsub(/Name:\s+/, '').split("\n")
pref_interface = pref_interface.map {|n| n if vm_interfaces.include?(n)}.compact
$network_interface = pref_interface[0]

Then, inside Vagrant.configure, use $network_interface to specify the bridge:

config.vm.network :public_network, :bridge => $network_interface
like image 71
joemaller Avatar answered Oct 16 '22 20:10

joemaller