添加VM/etc/host条目,这些条目指向带有Vagrant和Puppet的主机

添加VM/etc/host条目,这些条目指向带有Vagrant和Puppet的主机,vagrant,puphpet,Vagrant,Puphpet,我知道如何使用vagrant hostsupdater将指向VM的条目添加到主机的/etc/hosts文件中,但实际上我正试图找到一种动态方法来实现另一个方向。在我的机器上,我安装了带有大型数据库的MySQL。我不想把它放在虚拟机中,我需要虚拟机能够访问它 我可以很容易地手动设置它。在漫游之后,我可以用ssh连接到VM中,编辑那里的/etc/hosts,并创建一个类似hostmachine.local的条目,同时指向我的IP地址。然而,当我从家里搬去工作时,我的主机将发生变化,所以我必须不断更新

我知道如何使用vagrant hostsupdater将指向VM的条目添加到主机的/etc/hosts文件中,但实际上我正试图找到一种动态方法来实现另一个方向。在我的机器上,我安装了带有大型数据库的MySQL。我不想把它放在虚拟机中,我需要虚拟机能够访问它

我可以很容易地手动设置它。在漫游之后,我可以用ssh连接到VM中,编辑那里的/etc/hosts,并创建一个类似hostmachine.local的条目,同时指向我的IP地址。然而,当我从家里搬去工作时,我的主机将发生变化,所以我必须不断更新该条目


在.erb文件中是否有方法,或者以某种方式使流浪者获取主机的IP并在VM主机文件中创建这样的条目

这里有一种方法。由于
Vagrantfile
是一个Ruby脚本,我们可以使用一些逻辑来查找本地主机名和IP地址。然后我们在一个简单的配置脚本中使用它们,该脚本将它们添加到guest
/etc/hosts
文件中

示例
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
#-*-模式:ruby-*-
#vi:set ft=ruby:
#测试将主机IP地址设置为guest/etc/hosts
#确定主机IP地址-此处可能需要一些其他魔法
#(参考:http://stackoverflow.com/questions/5029427/ruby-get-local-ip-nix)
需要“插座”
定义my_first_private_ipv4
Socket.ip_address_list.detect{| intf | intf.ipv4_private?}
结束
ip=my_first_private_ipv4.ip_address()
#确定主机名-此处可能需要一些其他魔法
hostname=`hostname`

script=考虑到我的情况,我实际上找到了一个更简单的解决方案,在Mac上运行VM,并且我的local.db.yml文件不是源代码的一部分。实际上,我没有使用名称/IP,而是可以转到Mac的系统首选项,找到我的计算机的本地网络名称,即Kris-White-Mac.local


这解决了VM内部和外部的问题,因此,通过使用该名称而不是localhost或127.0.0.1,即使我的IP发生了变化,它也能正常工作。

另一种可能的解决方案是使用。主机IP的查找方法与BrianC在回答中显示的相同

流浪汉档案:

# -*- 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将在VM的
/etc/hosts
文件中添加一行,将主机的IP地址映射到
主机.machine
。多次运行provisioner不会导致在
/etc/hosts

中出现重复的行。vagrant hosts插件有一个新功能,让您只需执行
a.vm.provision:hosts,:sync_hosts=>true