vagrantfile 中的条件端口转发

Conditional port forwarding in vagrantfile

我正在尝试使用 vagrant 创建一个多虚拟机设置,其中只需要将服务器的公开端口转发到指定的主机端口。不需要公开客户端端口。但是,当我尝试使用附加的 Vagrantfile 执行此操作时,出于某种原因,它正在评估我的 if 条件以过滤掉客户端,这对客户端也是如此。有人可以指出我在这里可能做错了什么吗?

流浪文件:

# -*- mode: ruby -*-

# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = '2'
BASEBOX = 'centos-6.7'
BOX_MEMORY = '256'

# Declare the cluster config in a hash
HOST_CONFIG = { 
  'some_server' => '192.168.205.10', 
  'some_client' => '192.168.205.11'
}

# Create the vms
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = BASEBOX

  HOST_CONFIG.each do |hostname, hostip|
    config.vm.network "forwarded_port", guest: 80, host: 8080 if hostname == "some_server"
    config.vm.define hostname do |hname|
      hname.vm.provider 'virtualbox' do |v|
        v.name = hostname
        v.customize [ 'modifyvm', :id, '--cpus', '1' ]
        v.customize [ 'modifyvm', :id, '--memory', BOX_MEMORY ]
      end

      hname.vm.network 'private_network', ip: hostip
      hname.vm.provision :hosts do |provisioner|
        provisioner.autoconfigure = true
        provisioner.sync_hosts = true
      end

      hname.vm.provision 'ansible' do |ansible|
        ansible.playbook = 'bootstrap.yml'
      end
    end
  end
end

输出:

$ vagrant up
Bringing machine 'server' up with 'virtualbox' provider...
Bringing machine 'client' up with 'virtualbox' provider...
==> server: Importing base box 'centos-6.7'...
==> server: Matching MAC address for NAT networking...
==> server: Setting the name of the VM: server
==> server: Clearing any previously set network interfaces...
==> server: Preparing network interfaces based on configuration...
    server: Adapter 1: nat
    server: Adapter 2: hostonly
==> server: Forwarding ports...
    server: 80 (guest) => 8080 (host) (adapter 1)
    server: 22 (guest) => 2222 (host) (adapter 1)
==> server: Running 'pre-boot' VM customizations...
==> server: Booting VM...
==> server: Waiting for machine to boot. This may take a few minutes...
    server: SSH address: 127.0.0.1:2222
    server: SSH username: vagrant
    server: SSH auth method: private key
    server: Warning: Remote connection disconnect. Retrying...
    server: Warning: Remote connection disconnect. Retrying...
==> server: Machine booted and ready!
==> server: Checking for guest additions in VM...
==> server: Configuring and enabling network interfaces...
==> server: Mounting shared folders...
    server: /vagrant => /Users/ANJUWAA/Projects/Nagios
==> server: Running provisioner: hosts...
==> client: Importing base box 'centos-6.7'...
==> client: Matching MAC address for NAT networking...
==> client: Setting the name of the VM: client
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8080 is already in use
on the host machine.

To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

  config.vm.network :forwarded_port, guest: 80, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.

现在您实际上是在为所有机器设置 vm.network 值,如果其中一台机器名为 some_server

您应该将 vm.network 设置放在 vm.define 循环中:

HOST_CONFIG.each do |hostname, hostip|
    config.vm.define hostname do |hname|
      hname.vm.network "forwarded_port", guest: 80, host: 8080 if hostname == "some_server"
      hname.vm.provider 'virtualbox' do |v|
        v.name = hostname
        v.customize [ 'modifyvm', :id, '--cpus', '1' ]
        v.customize [ 'modifyvm', :id, '--memory', BOX_MEMORY ]
      end