Mapping of Vagrant Networks to VirtualBox Adapter Types

Mapping of Vagrant Networks to VirtualBox Adapter Types

Users of Vagrant may have noticed that the the configuration options for networks (config.vm.network) in Vagrant don’t match up cleanly to the network Adapter types in VirtualBox – as an example, when defining a “private_network” in a Vagrantfile Vagrant will actually provision your virtual appliance with a “Host-only Adapter.” The remainder of this blog post will describe the mapping of network configuration in a Vagrantfile to the adapters in VirtualBox in more detail.

forwarded_port Network Configuration

“forwarded_port” maps to a “NAT” type adapter
note that Vagrant always installs a NAT type adapter as the first network interface (typically en0 or eth0) when using VirtualBox. This is not well documented on the Internet, but can be found in Mitchell Hashimoto’s Vagrant: Up and Running.

private_network Network

“private_network” maps to a “Host-only Adapter”
if you provide an IP address (example: config.vm.network “private_network”, ip: “192.168.2.2”) which does not exist on a VirtualBox “Host-only Network” (such as vboxnet0) Vagrant will instruct VirtualBox to create a network for your virtual appliance.

public_network Network:

“public_network” maps to a “Bridged Adapter”
note that if you are using a public_network without specifying an adapter to bridge to you will be asked “what interface should the network bridge to” and be given a list of physical interfaces provided buy the Host OS.

Screenshot resulting from Testing

I attached a screenshot that resulted from the Vagrantfile that I used to create this mapping. Essentially the screenshot shows an instance that contains one of each type of adapter:

Vagrant-VirtualBox-Create_all_network_types

Vagrantfile used in Testing

To perform the mapping test I created a Vagrantfile that attaches each type of network interface. This Vagrantfile is available in my Snippets GitHub repository at https://github.com/colinbjohnson/snippets/blob/master/vagrant/create_all_network_types/Vagrantfile.

Questions or Comments?

If you have any questions please feel free to comment.

Allowing Access to Vagrant Instances from the Internet

Allowing Access to Vagrant Instances from the Internet

aka: “how do I use Vagrant with ngrok”

I’ve been working with a client whose developers use both Vagrant and ngrok very heavily. Here is what the developers like about both of them:

Vagrant:

  1. provides ability to spin up/down local development instances very quickly and consistently.
  2. the vagrant instances also run configuration management (in this case, Puppet) ensuring that the configuration is production-like

Ngrok:

  1. tunnels ports from the public internet (typically ports 80 and 443) to a given port on your own host, allowing connectivity to development instances from other hosts and devices. An example would be connecting an iOS or Android device to a vagrant instance.

Problem: accessibility, efficiency and consistency

  1. The developers were running into problems connecting hosts (typically iPhones and Android devices) to their local vagrant development instances
  2. Although easy to use, ngrok configuration was performed manually and usage wasn’t consistent developer to developer

Solution: vagrant with foodshow plugin

I solved both of these problems using a the vagrant plugin vagrant-foodshow – this vagrant plugin modifies vagrant port forwarding so that the host port also becomes the destination port of an ngrok tunnel. As all of the developers share one vagrant file, this also had the benefit of automating ngrok configuration and ensuring consistency of ngrok use. A screenshot of the results are below:

vagrant-foodshow-ngrok - Running in Chrome
Connectivity to a vagrant virtual machine from the public internet.
vagrant-foodshow-ngrok - Bootup
vagrant bootup with foodshow plugin configured.

 

Solution: the code

The Vagrantfile used to accomplish this below, and has also been checked into my snippets GitHub repository at https://github.com/colinbjohnson/snippets:

vagrantfile_api_version = "2"
if Vagrant.has_plugin?('vagrant-foodshow')
 ngrok_enabled = true
else
 ngrok_enabled = false
end
Vagrant.configure(vagrantfile_api_version) do |config|
config.vm.network "private_network", ip: "192.168.2.2"

if ngrok_enabled == true
 print "Ngrok enabled.\n"
 config.foodshow.enabled = true
 # using config.foodshow.subdomain will host your vagrant instance at the
 # given subdomain - for instance, you could host at my_subdomain.ngrok.com
 # instead of 94f3bdd.ngrok.com
 # config.foodshow.subdomain = 'my_subdomain'
 # config.foodshow.authtoken is required if using config.foodshow.subdomain
 # add your authtoken below
 # config.foodshow.authtoken = 'my_authtoken'
 config.vm.network :forwarded_port, guest: 80, host: 8080, ngrok_proto: "http+https"
 else
 $stderr.print "Ngrok not enabled.\n"
 end
config.vm.box = "ubuntu/precise64"
end

Two final notes:

  1. The above configuration exposes your vagrant instance to the public instance. If this is a concern, ngrok supports authentication to protect the destination service.
  2. My hope is to find time to create a private network shared amongst only the host OS, guest OS and the additional host, if it is an iOS or Android device.