Skip to content

Instantly share code, notes, and snippets.

@Max-AR
Created October 23, 2016 09:06
Show Gist options
  • Save Max-AR/3c70cd3213a633218bcf2579c2ab10d1 to your computer and use it in GitHub Desktop.
Save Max-AR/3c70cd3213a633218bcf2579c2ab10d1 to your computer and use it in GitHub Desktop.
# -*- mode: ruby -*-
# vi: set ft=ruby :
#You can modify these two values as you see fit
vb_memory = '4096'
vb_cpus = '4'
hostname = File.basename(Dir.getwd) + ".lan"
$logger = Log4r::Logger.new('vagrantfile')
def read_ip_address(machine)
command = "ip a | grep 'inet' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $2 }' | cut -f1 -d\"/\""
result = ""
$logger.info "Processing #{ machine.name } ... "
begin
# sudo is needed for ifconfig
machine.communicate.sudo(command) do |type, data|
result << data if type == :stdout
end
$logger.info "Processing #{ machine.name } ... success"
rescue
result = "# NOT-UP"
$logger.info "Processing #{ machine.name } ... not running"
end
# the second inet is more accurate
result.chomp.split("\n").last
end
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = 'centos/7'
config.vm.provision :shell, :path => 'VagrantBootstrap.sh'
config.vm.synced_folder ".", "/home/vagrant/sync", :mount_options => ["dmode=777,fmode=777"]
config.vm.provision :hostmanager
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
# ...
if Vagrant.has_plugin?("HostManager")
# ...
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
read_ip_address(vm)
end
end
config.vm.define hostname do |node|
node.vm.hostname = hostname
node.vm.network :private_network, type: 'dhcp'
end
config.vm.provider "virtualbox" do |vb|
vb.memory = vb_memory
vb.cpus = vb_cpus
vb.name = hostname
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment