簡體   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