Preferred fallback network interfaces with Vagrant?

vagrant

Solution

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

Problem

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.

Original source