Vagrant runs wrong provisioning file when booting multiple machines - vagrant

I have the following vagrantfile, which specifies 2 machines - the frontend and the backend box.
Vagrant.configure(2) do |config|
config.vm.box = "frontend"
config.vm.network "private_network", ip: "192.168.0.5"
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "4096"
end
config.vm.communicator = "winrm"
config.vm.provision "shell", path: "Provision.ps1"
config.vm.define "db" do |db|
db.vm.box = "backend"
db.vm.network "private_network", ip: "192.168.0.10"
db.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "4096"
end
db.vm.communicator = "winrm"
db.vm.provision "shell", path: "ProvisionRemote.ps1"
end
end
When I type vagrant up, according to the Multi-Machine documentation it should first boot the front end box and run Provision.ps1 and then boot the backend box and run ProvisionRemote.ps1 on it (outside in).
However, instead what happens is that the backend box boots first, and then it attempts to run Provision.ps1 (which is for the front end box) on it.
Bringing machine 'db' up with 'virtualbox' provider...
==> db: Importing base box 'backend'...
==> db: Matching MAC address for NAT networking...
==> db: Checking if box 'backend' is up to date...
==> db: Setting the name of the VM: RemoteBox_db_1459513634410_78500
==> db: Clearing any previously set network interfaces...
==> db: Preparing network interfaces based on configuration...
db: Adapter 1: nat
db: Adapter 2: hostonly
db: Adapter 3: hostonly
==> db: Forwarding ports...
db: 5985 => 55985 (adapter 1)
db: 5986 => 55986 (adapter 1)
==> db: Running 'pre-boot' VM customizations...
==> db: Booting VM...
==> db: Waiting for machine to boot. This may take a few minutes...
db: WinRM address: 127.0.0.1:55985
db: WinRM username: vagrant
db: WinRM transport: plaintext
==> db: Machine booted and ready!
==> db: Checking for guest additions in VM...
db: The guest additions on this VM do not match the installed version of
db: VirtualBox! In most cases this is fine, but in rare cases it can
db: prevent things such as shared folders from working properly. If you see
db: shared folder errors, please make sure the guest additions within the
db: virtual machine match the version of VirtualBox you have installed on
db: your host and reload your VM.
db:
db: Guest Additions Version: 4.3.28
db: VirtualBox Version: 5.0
==> db: Configuring and enabling network interfaces...
==> db: Mounting shared folders...
db: /vagrant => E:/_workingSource/project/env/
==> db: Running provisioner: shell...
db: Running: Provision.ps1 as c:\tmp\vagrant-shell.ps1
Why is it doing this? What am I doing wrong?

you should highlight that you have 2 machines
here you just define one machine (config and you did override some parameters as defined in the backend block but this is really the same machine definition) so vagrant is booting the one machine you define and tries to run all provisioner
The following works and defines 2 machines
Vagrant.configure(2) do |config|
config.vm.communicator = "winrm"
config.vm.define "front" do |front|
front.vm.box = "frontend"
front.vm.network "private_network", ip: "192.168.0.5"
front.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "4096"
end
front.vm.provision "shell", path: "Provision.ps1"
end
config.vm.define "db" do |db|
db.vm.box = "backend"
db.vm.network "private_network", ip: "192.168.0.10"
db.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "4096"
end
db.vm.provision "shell", path: "ProvisionRemote.ps1"
end
end
the config* parameter applys for the 2 machines (like config.vm.communicator) so all parameter that are common should be applied against the config. variable (note: I did not try to put the virtual box provider under config but it should work as long as it is the same) , and if you need machine specific parameter you must define within the specific block (like the IP defined as front.vm.network "private_network", ip: "192.168.0.5")

this approach fires provisioners in correct order ...
config.vm.provision "docker" do |d|
# installs docker
end
config.vm.provision :shell do |sh|
sh.privileged = false
sh.inline = $provision
end
config.vm.provision :shell do |sh|
sh.privileged = false
sh.path = "generateWebserverInstallEnvironment.sh"
end

Related

Vagrantbox with Hyper-V not starting

I'm trying to setup a vagrantbox with Hyper-V on my local windows-10 mashine. My Workstation is running behind a proxy, but I configured a local cntlm proxy to get around these limitations. Proxy settings work fine since I was able to install a vagrant plugin and downloan a box image.
But now my guest linux does not start up and I am running out of ideas.
My vagrant file
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04" # ubuntu 18.04 image with support for virtual box and Hyper-V
config.vm.hostname = "skywalker"
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
config.vm.provider "hyperv" do |hv|
hv.memory = "2048"
end
config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" # prevent tty errors
# install the vagrant plugin "vagrant-cachier" to cache downloaded artifacts
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box
end
# vagrant behing local cntlm proxy if plugin exists (= provinzial win10 workstation)
if !Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.http = "http://localhost:3128/"
config.proxy.https = "http://localhost:3128/"
config.proxy.no_proxy = "localhost, 127.0.0.1"
end
# --------------------------------------------------------------------------
# provision virtual mashine (basic setup) and install applications in VM
#config.vm.provision "shell", path: "scripts/install-ansible.sh"
config.vm.provision "shell", path: "scripts/install-ncdu.sh"
config.vm.provision "shell", path: "scripts/install-git.sh"
config.vm.provision "shell", path: "scripts/install-openjdk-11.sh"
config.vm.provision "shell", path: "scripts/install-maven.sh"
config.vm.provision "shell", path: "scripts/install-node-npm.sh"
config.vm.provision "shell", path: "scripts/install-docker.sh"
config.vm.provision "shell", path: "scripts/install-docker-compose.sh"
# npm webserver
config.vm.provision "shell", path: "apps/install-npm-apps.sh"
config.vm.network "forwarded_port", guest: 8000, host: 8000
# artifactory setup (start artifactory after vm startup)
config.vm.network "forwarded_port", guest: 8081, host: 8081 # artifactory from docker
config.vm.network "forwarded_port", guest: 8082, host: 8082 # artifactory from docker
# See README.md for Artifactory in Docker
end
Starting the box with this settings results in
C:\home\work\workspace\vagrant-boxes\skywalker (master -> origin)
λ vagrant up
Bringing machine 'default' up with 'hyperv' provider...
==> default: Verifying Hyper-V is enabled...
==> default: Verifying Hyper-V is accessible...
==> default: Importing a Hyper-V instance
default: Creating and registering the VM...
default: Successfully imported VM
default: Configuring the VM...
==> default: Starting the machine...
==> default: Waiting for the machine to report its IP address...
default: Timeout: 120 seconds
default: IP: fe80::215:5dff:fe02:8b01
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: fe80::215:5dff:fe02:8b01:22
default: SSH username: vagrant
default: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.
If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.
If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.
On a different mashine with VirtualBox as VM provider this Vagrantfile works fine. Sadly I cannot use any VM provider other than Hyper-V on my windows mashine due to limitations which are out of my control ...
I set up Hyper-V using Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All.
Any ideas? Thanks in advance and best regards. Sebastian
I went through this exercise last week and saw similar problems. I made lot of progress following these steps and the vm's get spun up at a reasonable time under windows 10 hyper-v. This is what i did.
Followed this blog and tried creating a vm successfully first.
https://win32.io/posts/Vagrant-Install-HyperV
Then i created a simple vagrantfile and got it to work successfully. Here is mine ,
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.hostname = 'utility-server-1'
config.vm.network 'public_network', bridge: 'Internet'
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "hyperv" do |vb|
vb.memory = "4024"
vb.cpus = 2
vb.vmname = 'utility-server-1'
end
end
Once this works, add more complex steps from your vagrantfile, like shell scripts and forwarded ports.

After PC restarted the vagrant machine fails to starts. It shows port collision

UPDATE: After host PC reboot vagrant up shows port collision on any port
My vagrant machine fails to start.
Usually it was working normally, but today after I restarted my PC the vagrant machine no more starts.
I did not change the Vagrantfile.
It says about ports collision.
If I remove the 6379 setting for redis, then it says for port 1080 and so on.
Like all ports have the collision.
If I try to run the macine via VMware - then it starts OK, but I cannont connect to it via ssh, because it doesn't have the settings that are defined in Vagrantfile...
Here is the log:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: There was a problem while downloading the metadata for your box
==> default: to check for updates. This is not an error, since it is usually due
==> default: to temporary network problems. This is just a warning. The problem
==> default: encountered was:
==> default:
==> default: The requested URL returned error: 404
==> default:
==> default: If you want to check for box updates, verify your network connectio n
==> default: is valid and try again.
==> default: Fixed port collision for 6379 => 6379. Now on port 2250.
Vagrant found a port collision for the specified port and virtual machine.
While this port was marked to be auto-corrected, the ports in the
auto-correction range are all also used.
VM: default
Forwarded port: 1080 => 1080
Here is my Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
forward_port = ->(guest, host = guest) do
config.vm.network :forwarded_port,
guest: guest,
host: host,
auto_correct: true
end
config.vm.synced_folder "sites/", "/var/www"
config.vm.synced_folder ".", "/vagrant", disabled: true
forward_port[6379] # redis
forward_port[1080] # mailcatcher
forward_port[3306] # mysql
forward_port[80, 8080] # nginx/apache
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "manifests"
puppet.manifest_file = "default.pp"
end
config.vm.network :private_network, ip: "33.33.33.10"
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end

Vagrant 'pre-boot' VM customization error

What is the cause of this particular error?
I have the latest version of VirtualBox (5.2.22-126460) and Vagrant (2.2.2).
Those are running on a freshly installed Windows 10 operating system.
Whenever I try to 'vagrant up' my machine I get this following output:
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default: https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default: https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 1080 (guest) => 1080 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
A customization command failed:
["modifyvm", :id, "--cableconnected0", "on"]
The following error was experienced:
#<Vagrant::Errors::VBoxManageError: There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.
Command: ["modifyvm", "22fda01f-9b13-43f9-bf0d-4deb81c688c8", "--cableconnected0", "on"]
Stderr: VBoxManage.exe: error: Invalid NIC number 0
>
Please fix this customization and try again.
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "local.1400degrees.com"
config.vm.hostname = "local.website.com"
config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777,fmode=777"]
config.vm.network "forwarded_port", guest: 1080, host: 1080
config.vm.network "private_network", ip: "192.168.33.13"
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.ssh.username = 'vagrant'
config.ssh.password = 'vagrant'
config.ssh.insert_key = false
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--cableconnected1", "on"]
end
end
Per this github issue a solution can be to
1.first of all destroy the virtual box with vagrant destroy
2.navigate to C:\Users\YOUR_USERNAME
3.delete the .vagrant.d folder
4.go into VirtualBox VMs folder and delete everything inside it.
5.start it again with vagrant up
You can get more in depth logs by using vagrant up --debug or if you would like to save it to a file vagrant up --debug &> vagrant.log. Also, a great thing about this is you can always tear it down and start over :)
I believe the real reason this is happening is due to this: https://github.com/hashicorp/vagrant/issues/10481
The fix was already merged to the vagrant master branch, however it will probably be included in version 2.2.5 or later. Latest vagrant version as of writing this up is 2.2.4 and the bug is still present.
The fix that was done:
In plugins/providers/virtualbox/action/set_default_nic_type.rb
They changed the code from E1000_SUSCEPTIBLE = Gem::Requirement.new("<= 5.2.22").freeze to E1000_SUSCEPTIBLE = Gem::Requirement.new("< 5.2.22").freeze

Vagrant Up (private_network) Error : default: Warning: Connection aborted. Retrying

I have the following Vagranfile. When I vagrant up, I'm getting the following error. However, if I switch to use a config.vm.network "public_network", everything works fine.
But I still want to use config.vm.network "private_network" so I can assign fixed IP address for development.
[Error]
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection aborted. Retrying...
default: Warning: Connection aborted. Retrying...
[Vagrantfile]
Vagrant.configure("2") do |config|
config.vm.box = "pef"
config.vm.network "private_network", ip: "192.168.33.12"
#config.vm.network "public_network"
config.vm.synced_folder "C:/Users/user1/myprj", "/home/vagrant"
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "1024"
# For host-only adapter
vb.customize ["modifyvm", :id, "--uartmode1", "disconnected" ]
vb.customize ["modifyvm", :id, "--nictype1", "Am79C973"]
vb.customize ["modifyvm", :id, "--nictype2", "Am79C973"]
end
end
I had this issue which is confusing. First thing you need to know, is the problem causing timeout.
If you're using VirtualBox, you can add this to your VagrantFile :
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
vb.gui = true
That will launch VirtualBox window and help you understand the potential error you have during the booting process of your VM.
In my case, I tried several things and ended up raising Vagrant time out to 600 :
config.vm.boot_timeout = 600
You can also read to this feed on StackOverflow , it describes the same issue :
Vagrant up timeout

Can't connect to guest node set up with nat and hostonly vagrant and virtualbox

I have been trying to replace manual vitualbox setup with vagrant. I usually create a guest node with 2 network adapters a NAT for the guest(ubuntu) to use the host internet for all apt-get operations, and a hostonly for my host to connect to the guest. I has been working pretty well.
Below is my vagrant file
Vagrant.configure("1") do |config|
config.vm.boot_mode = :gui
end
Vagrant.configure("2") do |config|
config.vm.define "web" do |web|
web.vm.box = "ubuntu/trusty64"
web.vm.network "forwarded_port", guest:22, host:2222, id:"ssh", disabled:true
web.vm.network "private_network", ip:"192.168.56.103", :adapter =>2
end
end
Below is the output:
Bringing machine 'web' up with 'virtualbox' provider...
==> web: Importing base box 'ubuntu/trusty64'...
==> web: Matching MAC address for NAT networking...
==> web: Checking if box 'ubuntu/trusty64' is up to date...
==> web: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> web: have version '14.04'. The latest is version '20150430.0.0'. Run
==> web: `vagrant box update` to update.
==> web: Setting the name of the VM: ansible_web_1431424632866_64634
==> web: Clearing any previously set forwarded ports...
==> web: Clearing any previously set network interfaces...
==> web: Preparing network interfaces based on configuration...
web: Adapter 1: nat
web: Adapter 2: hostonly
==> web: Forwarding ports...
==> web: Booting VM...
==> web: Waiting for machine to boot. This may take a few minutes...
web: SSH address: 127.0.0.1:22
web: SSH username: vagrant
web: SSH auth method: private key
web: Warning: Authentication failure. Retrying...
web: Warning: Authentication failure. Retrying...
web: Warning: Authentication failure. Retrying...
Now on vagrant it almost works and I can't figure out few things:
Question 1 Does vagrant need port forwarding to connect to the guest?
Question 2 When vagrant says : ==> web: Preparing network interfaces based on configuration...
web: Adapter 1: nat
web: Adapter 2: hostonly I figured the eth1 should be up but that's not the case. ifconfig -a shows the interface eth1 without any IP any sudo ifup eth1 shows Ignoring unknown interface eth1=eth1. What happened here and how can I solve this?
Thanks for helping
Never mind, I fixed it. This relies heavily on the version of vagrant used. This works with any of the version which support auto correct feature of the forwarding port.
After removing the line:
web.vm.network "forwarded_port", guest:22, host:2222, id:"ssh", disabled:true
It started working like a charm and I could add other nodes .Kindly see below the config that worked for sake of sharing
Vagrant.configure("1") do |config|
config.vm.boot_mode = :gui
end
Vagrant.configure("2") do |config|
auto_config= false
config.vm.define "web" do |web|
web.vm.box = "ubuntu/trusty64"
web.vm.network "private_network", ip:"192.168.56.103", :adapter =>2
end
(1..3).each do |i|
config.vm.define "mongo-#{i}" do |mongo|
mongo.vm.box = "ubuntu/trusty64"
mongo.vm.network "private_network" , ip:"192.168.56.11#{i}", :adapter =>2
end
end
(1..2).each do |i|
config.vm.define "tomcat-#{i}" do |tomcat|
tomcat.vm.box = "ubuntu/trusty64"
tomcat.vm.network "private_network" , ip:"192.168.56.12#{i}", :adapter =>2
end
end
(1..2).each do |i|
config.vm.define "mysql-#{i}" do |mysql|
mysql.vm.box = "ubuntu/trusty64"
mysql.vm.network "private_network" , ip:"192.168.56.13#{i}", :adapter =>2
end
end
end

Resources