Skip to content

Instantly share code, notes, and snippets.

@ekho
Created October 23, 2017 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekho/adeb972661ab5304c6ecb5cfd262b17a to your computer and use it in GitHub Desktop.
Save ekho/adeb972661ab5304c6ecb5cfd262b17a to your computer and use it in GitHub Desktop.
vagrant hostmanager versus public_interface
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "public_network", use_dhcp_assigned_default_route: true, bridge: "en0: Wi-Fi (AirPort)"
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.manage_guest = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
# custom ip resolver for public network with dhcp
config.hostmanager.ip_resolver = proc do |vm|
ip = nil
if vm.state.id == :running
if vm.id
property_value = `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.strip
ip = property_value.split()[1] unless property_value == 'No value set!'
end
if ip.nil?
ifconfig_value = ''
vm.communicate.execute("ifconfig eth1; exit 0") do |type, data|
ifconfig_value << data if type == :stdout
end
ip = (ip = /^\s*inet .*?(\d+\.\d+\.\d+\.\d+)\s+/.match(ifconfig_value)) && ip[1]
end
end
ip
end
config.vm.provision :hostmanager
config.vm.define :vm1 do |machine|
machine.vm.hostname = 'vm1'
machine.hostmanager.aliases = %w(vm1.local)
end
config.vm.define :vm2 do |machine|
machine.vm.hostname = 'vm2'
machine.hostmanager.aliases = %w(vm2.local)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment