Discussion:
[vagrant-up] Incrementing a port number for forwarding
rhys.james.campbell via Vagrant
2018-11-22 15:31:03 UTC
Permalink
Hello All,

I am trying to forward a port to a couple of servers in a Vagrant file but
the variable is not being incremented for some reason...

All I'm doing is this...

if host.start_with?("testnagios")
config.vm.network "forwarded_port", guest: 80, host:
nagios_forward_port
nagios_forward_port += 1

The first instance works but I get the following error when the second
instance fires up...

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 8888 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. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.


I set the nagios_forward_port right after the Vagrant.configure("2") do
|config| line. It is not referenced anywhere else in the Vagrantfile other
than the lines given above. Is there something about scope I'm missing here?

Cheers,

Rhys
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/deadadb0-ef6e-4d8f-8683-e5a63b9cd98c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alvaro Miranda Aguilera
2018-11-22 19:03:46 UTC
Permalink
Hello

If you share the full Vagrantfile I can have a look

but this works:

*==> default: Forwarding ports...*

default: 80 (guest) => 8080 (host) (adapter 1)

default: 80 (guest) => 8081 (host) (adapter 1)

default: 22 (guest) => 2203 (host) (adapter 1)

*==> default: Booting VM...*




Vagrant.configure("2") do |config|

config.vm.box = "alvaro/xenial64"


nagios_forward_port=8080

puts nagios_forward_port

config.vm.network "forwarded_port", guest: 80, host: nagios_forward_port


nagios_forward_port += 1

puts nagios_forward_port

config.vm.network "forwarded_port", guest: 80, host: nagios_forward_port


end


Alvaro.

On Thu, Nov 22, 2018 at 4:31 PM rhys.james.campbell via Vagrant <
Post by rhys.james.campbell via Vagrant
Hello All,
I am trying to forward a port to a couple of servers in a Vagrant file but
the variable is not being incremented for some reason...
All I'm doing is this...
if host.start_with?("testnagios")
nagios_forward_port
nagios_forward_port += 1
The first instance works but I get the following error when the second
instance fires up...
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 8888 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
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. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
I set the nagios_forward_port right after the Vagrant.configure("2") do
|config| line. It is not referenced anywhere else in the Vagrantfile other
than the lines given above. Is there something about scope I'm missing here?
Cheers,
Rhys
--
This mailing list is governed under the HashiCorp Community Guidelines -
https://www.hashicorp.com/community-guidelines.html. Behavior in
violation of those guidelines may result in your removal from this mailing
list.
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/vagrant-up/deadadb0-ef6e-4d8f-8683-e5a63b9cd98c%40googlegroups.com
<https://groups.google.com/d/msgid/vagrant-up/deadadb0-ef6e-4d8f-8683-e5a63b9cd98c%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Alvaro
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/CAHqq0eyVg-jZueYHM3DxC3RGa7dD5ku_VLr-6qu-HdgWKULDhg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
rhys.james.campbell via Vagrant
2018-11-23 13:57:26 UTC
Permalink
My full Vagrant file is as below. I've since "fixed" this by removing the
increment and adding auto_correct: true to the port mapping. That works but
I don;t see what I'm doing wrong the old way. Cheers, Rhys

Vagrant.configure("2") do |config|


nagios_forward_port=8888
nrpe_hosts = [ "nrpeyum01",
"nrpepip02",
"nrpescript03",
"nrpeyumpip04",
"nrpeyumscript05",
"nrpepipscript06",
"nrpeall07"
]
nagios_hosts = [ "testnagios01",
"testnagios02" ]


all_hosts = nrpe_hosts + nagios_hosts


all_hosts.each do |host|
config.vm.define "#{host}" do |nrpe|
nrpe.vm.box = "bento/centos-7.5"
nrpe.vm.provider :virtualbox do |vb|
vb.customize [
"modifyvm", :id,
"--name", "#{host}",
"--memory", "1024"
]
vb.cpus = 1
end
config.vm.hostname = "#{host}"
if host.start_with?("testnagios")
config.vm.network "forwarded_port", guest: 80, host:
nagios_forward_port
nagios_forward_port += 1
end
if host == all_hosts[-1]
config.vm.provision :ansible do |ansible|
ansible.groups = {
"nrpe" => nrpe_hosts,
"nagios" => nagios_hosts,
}
ansible.limit = "nrpe"
ansible.playbook = "nrpe.yml"
end
config.vm.provision :ansible do |ansible|
ansible.groups = {
"nrpe" => nrpe_hosts,
"nagios" => nagios_hosts,
}
ansible.limit = "nagios"
ansible.playbook = "nagios.yml"
end
end
end
end
end
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/d8bcdbf5-8f4b-48a3-8f72-75a97ec6140f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Alvaro Miranda Aguilera
2018-11-24 22:46:29 UTC
Permalink
not sure what you want to do

but as is, seems it will only do the test nagios server

On Fri, Nov 23, 2018 at 2:57 PM rhys.james.campbell via Vagrant <
Post by rhys.james.campbell via Vagrant
My full Vagrant file is as below. I've since "fixed" this by removing the
increment and adding auto_correct: true to the port mapping. That works but
I don;t see what I'm doing wrong the old way. Cheers, Rhys
Vagrant.configure("2") do |config|
nagios_forward_port=8888
nrpe_hosts = [ "nrpeyum01",
"nrpepip02",
"nrpescript03",
"nrpeyumpip04",
"nrpeyumscript05",
"nrpepipscript06",
"nrpeall07"
]
nagios_hosts = [ "testnagios01",
"testnagios02" ]
all_hosts = nrpe_hosts + nagios_hosts
all_hosts.each do |host|
config.vm.define "#{host}" do |nrpe|
nrpe.vm.box = "bento/centos-7.5"
nrpe.vm.provider :virtualbox do |vb|
vb.customize [
"modifyvm", :id,
"--name", "#{host}",
"--memory", "1024"
]
vb.cpus = 1
end
config.vm.hostname = "#{host}"
if host.start_with?("testnagios")
nagios_forward_port
nagios_forward_port += 1
end
if host == all_hosts[-1]
config.vm.provision :ansible do |ansible|
ansible.groups = {
"nrpe" => nrpe_hosts,
"nagios" => nagios_hosts,
}
ansible.limit = "nrpe"
ansible.playbook = "nrpe.yml"
end
config.vm.provision :ansible do |ansible|
ansible.groups = {
"nrpe" => nrpe_hosts,
"nagios" => nagios_hosts,
}
ansible.limit = "nagios"
ansible.playbook = "nagios.yml"
end
end
end
end
end
--
This mailing list is governed under the HashiCorp Community Guidelines -
https://www.hashicorp.com/community-guidelines.html. Behavior in
violation of those guidelines may result in your removal from this mailing
list.
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an
To view this discussion on the web visit
https://groups.google.com/d/msgid/vagrant-up/d8bcdbf5-8f4b-48a3-8f72-75a97ec6140f%40googlegroups.com
<https://groups.google.com/d/msgid/vagrant-up/d8bcdbf5-8f4b-48a3-8f72-75a97ec6140f%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
Alvaro
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/CAHqq0ezm9WFc-rv4jRquaKaEKupcr804W-%3DiSNRAgLGrQkaQ9A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
rhys.james.campbell via Vagrant
2018-11-26 10:07:36 UTC
Permalink
There are two Nagios servers. The first one should be allocated the port 8888
in the forwarding. The second one should get 8889 because I increment it
right after the first one. So I basically want the port assigned as
follows...

testnagios01 - 8888
testnagios02 - 8889


This does not happen and the Vagrantfile errors with the following...

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 8888 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. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.

So basically the increment isn't happening.
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/7401d515-17a4-4463-9a93-ce7ccf57c094%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
rhys.james.campbell via Vagrant
2018-11-26 11:03:30 UTC
Permalink
OK. This is strange. This...

if host.eql?("testnagios01")
config.vm.network "forwarded_port", guest: 80, host: 8888
end
if host.eql?("testnagios02")
config.vm.network "forwarded_port", guest: 80, host: 8889
end

==> testnagios01: Checking for guest additions in VM...
==> testnagios01: Setting hostname...
==> testnagios01: Mounting shared folders...
testnagios01: /vagrant => ..../vagrant
==> testnagios02: Importing base box 'bento/centos-7.5'...
==> testnagios02: Matching MAC address for NAT networking...
==> testnagios02: Checking if box 'bento/centos-7.5' is up to date...
vagrant_testnagios02_1543229171053_3544
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 8888 is already in use
on the host machine.
To fix this, modify your current project's Vagrantfile to use another
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. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
If I append auto_correct: true to the mappings it work but I get a
different end result...

Rhyss-MacBook-Pro:vagrant rhyscampbell$ vagrant port testnagios01
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.
22 (guest) => 2206 (host)
80 (guest) => 8888 (host)
Rhyss-MacBook-Pro:vagrant rhyscampbell$ vagrant port testnagios02
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if the
provider supports automatic port collision detection and resolution.
22 (guest) => 2208 (host)
80 (guest) => 2207 (host)
80 (guest) => 8889 (host)
Note the second host has two mappings to port 80. Which is really weird.
One is the result of the auto_correct so it's being applied twice to the
second host.
--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.

GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/9e47e759-a094-4cc5-a8b4-8ec9ccc506c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...