简体   繁体   中英

Conditional port forwarding in vagrantfile

I'm trying to create a multi vm setup using vagrant where only the server's exposed port needs to be forwarded to the specified host machine port. Client ports need not be exposed. But when I try to do this using the attached Vagrantfile, it for some reason is evaluating my if condition for filtering the clients out, true for clients also. Can someone point out what I may be doing wrong here?

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

Output:

$ 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.

Now you are effectively setting the vm.network value for all machines if one of them is named some_server .

You should place the vm.network setting inside the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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