Kris White
Kris White

Reputation: 679

Adding VM /etc/host entries that point to host machine with Vagrant and Puphpet

I know how to use vagrant-hostsupdater to add entries into the host's /etc/hosts file that point to the VM, but I'm actually trying to find a dynamic way to go the OTHER direction. On my machine, I have MySQL installed with a large db. I don't want to put this inside the VM, I need the VM to be able to access it.

I can easily set it up manually. After vagrant up, I can ssh into the VM and edit the /etc/hosts there and make an entry like hostmachine.local and point to my IP address at the time. However, as I move from home to work my host machine will change so I constantly have to update that entry.

Is there a way within an .erb file or somehow to make a vagrant up take the IP of the host machine and make such an entry in a VM hosts file?

Upvotes: 4

Views: 6998

Answers (3)

Indrek Ots
Indrek Ots

Reputation: 3911

Another possible solution is to use the vagrant-hosts plugin. Host IP can be found the same way BrianC showed in his answer.

Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'socket'

def my_first_private_ipv4
  Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
end
host_ip = my_first_private_ipv4.ip_address()

Vagrant.configure(2) do |config|
    config.vm.define "web", primary: true do |a|
        a.vm.box = "ubuntu/trusty64"
        a.vm.hostname = "web.local"

        a.vm.provider "virtualbox" do |vb|
          vb.memory = 2048
          vb.cpus = 1
        end

        a.vm.provision :hosts do |provisioner|
            provisioner.add_host host_ip, ['host.machine']
        end
    end
end

Provisioner will add a row to the VM's /etc/hosts file, mapping host machine's IP address to host.machine. Running provisioner multiple times will not result in duplicate lines in /etc/hosts.

Upvotes: 1

Kris White
Kris White

Reputation: 679

I actually found a simpler solution given my situation, running the VM on a Mac and the fact that my local.db.yml file is not part of the source code. Instead of using a name/IP I was actually just able to go to Mac's System Preferences and find my local network name for my computer, i.e. Kris-White-Mac.local

This resolves both inside and outside of the VM, so by putting that name instead of localhost or 127.0.0.1, it works even when my IP changes.

Upvotes: 0

BrianC
BrianC

Reputation: 10721

Here's one way to do it. Since the Vagrantfile is a Ruby script, we can use a bit of logic to find the local hostname and IP address. Then we use those in a simple provisioning script which adds them to the guest /etc/hosts file.

Example Vargrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# test setting the host IP address into the guest /etc/hosts

# determine host IP address - may need some other magic here
# (ref: http://stackoverflow.com/questions/5029427/ruby-get-local-ip-nix)
require 'socket'
def my_first_private_ipv4
  Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
end
ip = my_first_private_ipv4.ip_address()

# determine host name - may need some other magic here
hostname = `hostname`

script = <<SCRIPT
echo "#{ip} #{hostname}" | tee -a /etc/hosts
SCRIPT

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.hostname = "iptest"
  config.vm.provision :shell, :inline => script
  config.vm.provider "virtualbox" do |vb|
  #   vb.gui = true
     vb.name = "iptest"
     vb.customize ["modifyvm", :id, "--memory", "1000"]
  end
end

Note: the echo | tee -a command adding to /etc/hosts will keep appending if you provision multiple times (without destroying the VM). You might need a better solution there if you run into that.

Upvotes: 2

Related Questions