【问题标题】:Adding VM /etc/host entries that point to host machine with Vagrant and Puphpet使用 Vagrant 和 Puphpet 添加指向主机的 VM /etc/host 条目
【发布时间】:2015-11-26 21:45:54
【问题描述】:

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

我可以轻松地手动设置它。在 vagrant up 之后,我可以 ssh 进入虚拟机并在那里编辑 /etc/hosts 并创建一个类似 hostmachine.local 的条目并指向我当时的 IP 地址。但是,当我从家搬到工作地点时,我的主机会发生变化,因此我必须不断更新该条目。

有没有办法在 .erb 文件中或以某种方式让 vagrant up 获取主机的 IP 并在 VM 主机文件中创建这样的条目?

【问题讨论】:

    标签: vagrant puphpet


    【解决方案1】:

    这是一种方法。由于Vagrantfile 是一个Ruby 脚本,我们可以使用一些逻辑来查找本地主机名和IP 地址。然后我们在一个简单的配置脚本中使用它们,将它们添加到来宾/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
    

    注意:添加到/etc/hostsecho | tee -a 命令将在您配置多次时继续追加(不破坏虚拟机)。如果遇到这种情况,您可能需要更好的解决方案。

    【讨论】:

      【解决方案2】:

      另一种可能的解决方案是使用vagrant-hosts plugin。可以像 BrianC 在他的回答中显示的那样找到主机 IP。

      流浪文件:

      # -*- 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 地址映射到 host.machine。多次运行provisioner不会导致/etc/hosts中出现重复的行。

      【讨论】:

      • vagrant hosts 插件有一个新功能,让你做a.vm.provision :hosts, :sync_hosts =&gt; true
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 2014-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        相关资源
        最近更新 更多