繁体   English   中英

vagrantfile中的条件端口转发

[英]Conditional port forwarding in vagrantfile

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

Vagrantfile:

# -*- 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.

现在,如果其中一台计算机名为some_server则可以有效地为所有计算机设置vm.network值。

您应该将vm.network设置放入vm.define -loop内:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM