Adding VM / etc / host entries pointing to Vagrant and Puphpet host machine

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

I can easily configure it manually. After vagrants, I can ssh into a virtual machine and edit / etc / hosts there and make an entry like hostmachine.local and point to my IP at that time. However, when I go from home to work, my host machine changes, so I need to update this entry constantly.

Is there a way in the .erb file or somehow to get the vagrant to go up to the host machine's IP and make an entry like that in the VM hosts file?

+3


source to share


3 answers


Here's one way to do it. Since it Vagrantfile

is a Ruby script, we can use a bit of logic to find the local hostname and IP address. We then use them in a simple script provisioning that adds them to the guest file /etc/hosts

.

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 command echo | tee -a

adding to /etc/hosts

will continue adding if you execute multiple times (without destroying the virtual machine). You may need a better solution if you come across this.

+2


source


Actually I found an easier solution given my situation, running VM on Mac and the fact that my local.db.yml file is not part of the source code. Instead of using the name / IP, I could actually go into Mac OS preferences and find the name of my local network for my computer, i.e. Kris-White-Mac.local



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

0


source


Another possible solution is to use the vagrant-hosts plugin . Host IP can be found in the same way as Brian 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

      

The Provisioner will add a line to the VM file /etc/hosts

, mapping the host's IP address to host.machine

. Running the assistant multiple times will not result in duplicate lines in /etc/hosts

.

0


source







All Articles