I would like to set up VirtualBox via Vagrant in a way that resembles my cloud provider setup as close as possible. This means:
VM is reachable via some public IP (e.g. 192.168.0.2)
A given public SSH key is set up in /root/.ssh/authorized_keys
Bonus: The default user is root (but that's probably another question)
My Vagrantfile is:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.define "master" do | w |
w.vm.hostname = "master"
w.vm.network "public_network", ip: "192.168.0.2"
w.vm.network "private_network", ip: "10.0.0.2"
w.vm.provider "virtualbox" do |vb|
vb.memory = "4096"
vb.cpus = 2
vb.name = "master"
end
end
config.vm.provision "file", source: "~/.ssh/bob.pub", destination: "~/.ssh/authorized_keys"
end
When Vagrant sets the box up it does some port forwarding for SSH for some reason:
==> master: Forwarding ports...
master: 22 (guest) => 2222 (host) (adapter 1)
==> master: Running 'pre-boot' VM customizations...
==> master: Booting VM...
==> master: Waiting for machine to boot. This may take a few minutes...
master: SSH address: 127.0.0.1:2222
master: SSH username: vagrant
master: SSH auth method: private key
So if you want to SSH into the box then you need:
ssh -p 2222 vagrant#127.0.0.1
But I would like to be able to have the setup without the port forwarding on the regular port and public IP, i.e. ssh vagrant#192.168.0.2
Is this somehow possible? The network settings in VirtualBox seem to be alright, but ssh vagrant#192.168.0.2 times out. Not sure how this port forwarding makes even sense.
Thanks for any suggestion!
Try using this.
Clearly define port and host for each VM via config.ssh
config.ssh.host = "ip"
config.ssh.port = "port"
Related
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
Having a problem getting my localhost to work properly.
Running git bash I've successfully vagrant init "hashicorp/precise32"
vagrant up
Perhaps I'm putting my local host IP in the wrong place: 127.0.0.1
Confirmed my VM is running and here is my vagrant file:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
Vagrant.configure(2) do |config|
config.vm.box = "hashicorp/precise32"
# config.vm.boot_timeout = "300"
# Setting this above never worked for me when i had a config.vm.boot_timeout
config.vm.network :forwarded_port, guest: 80, host: 8082
# config.vm.network :public_network
config.vm.network "private_network", ip: "127.0.0.1"
config.vm.synced_folder ".", "/vagrant", type: "nfs"
config.vm.provider :virtualbox do |vb|
vb.gui = true
# I turned this vb.gui = true on when i was having a problem with config.vm.boot_timeout
vb.customize ["modifyvm", :id, "--memory", "4096"]
vb.cpus = 4
end
end
I've added some additional options but I think my ip or ports are wrong. Any help would be greatly appreciated. Thank you.
edit ************************
Host file looks like so:
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handle within DNS itself.
127.0.0.1 localhost
::1 localhost
After I vagrant reload:
$ vagrant reload
==> default: [vagrant-hostsupdater] Removing hosts
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'hashicorp/precise32' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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: 80 (guest) => 8082 (host) (adapter 1)
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: Machine booted and ready!
[default] GuestAdditions 5.1.8 running --- OK.
==> default: Checking for guest additions in VM...
==> default: [vagrant-hostsupdater] Checking for host entries
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => C:/Users/Timothy/Documents/Magento
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
Why is this so confusing right now, I've never had so much trouble setting up a local dev. environment.
you cannot use a private_network of 127.0.0.1 from your host thats your loopback adapter so it will never be able to reach your VM.
You should use on of the 3 following ranges (see https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces)
10.0.0.0 - 10.255.255.255
172.16.0.0 - 172.31.255.255
192.168.0.0 - 192.168.255.255
vagrant will already create a NAT adapter on 10.0.2.15 to communicate over ssh.
I suggest to take an IP in the 192.168.x.x range and you will be able to work correctly with your VM
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
I have the following inside my Vagrant file.
config.vm.network "private_network", ip: "192.168.33.12"
When I "vagrant up", vagrant is assigning 127.0.0.1. I'm not sure what's causing this. Below is the full trace.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> 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: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2200
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => C:/vm2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
what you see here
default: SSH address: 127.0.0.1:2200
is not your VM IP address - this is just for vagrant to ssh into the box using an available port (in your case 2200). vagrant configures on your VM the eth0 interface to communicate with the host
Login to the VM and run ifconfig you will see you have (at least) 2 interfaces available with eth0 on 10.0.2.15 and eth1 on 192.168.33.12
As a shorthand way of doing this, a oneliner you can run from the host to see assigned IPs on the guest:
$ vagrant ssh -c "ifconfig | grep 'inet addr'"
Which will give you an output like this:
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet addr:192.168.200.200 Bcast:192.168.200.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0
Here are the steps that i choose while setting up the static ip of the Ubuntu machine:
ls /etc/netplan/ //go to this directory to find the config file
Note: the YAML file is very strict about the indentations e.g
dhcp4:true //will give wrong indentation error
dhcp4: true //right way to give
Now edit the config file:-
sudo nano /etc/netplan/01-netcfg.yaml
Change Default settings from :-
network:
version: 2
renderer: networkd
eth0:
dhcp4: true
dhcp6: false
optional: true
nameservers:
addresses: [8.8.8.8,8.8.4.4]
To
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses: [172.28.32.11/16] //here choose your desired IP
gateway4: 172.28.32.1 //provide the gateway address,
nameservers:
addresses: [8.8.8.8,8.8.4.4]
Now do :
sudo netplan apply // to apply the network settings
You can then check that the ip address remains same during any course between vagrant halt/up
Here is what i did for the virtual box set machine of vagrant:
Add this to to vagrant file :-
config.vm.network "private_network", ip: "any IP address that you choose"
Then do the Vagrant Reload and you should be good.
In case the VM stucks for Vagrant Up command in retrying the connect or ssh .. do this:-
Run the below command from the cmder console:-
vagrant plugin install vagrant-vbguest
After this enable Vagrant GUI by adding the following to the vagrant file:-
config.vm.provider :virtualbox do |vb|
vb.gui = true
end
Now do the vagrant up
the vagrant gui window will open and Keep Pressing "S" to skip and enter the login screen
Login with the username/password which is root/vagrant by default
and run the following command:-
sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
Now come out of the vagrant gui window shell and
remove the line added earlier for enabling the vagrant gui
from the cmder console do "vagrant up" again
Done!
I'm trying to do some Vagrant/Ansible stuff, but running into problems from the start. Here's my Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.6.66"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
config.vm.provision "ansible" do |ansible|
ansible.playbook = "site.yml"
end
end
site.yml is simply
---
- name: Bring up server with MySQL, Nginx, and PHP-FPM
hosts: all
remote_user: root
roles:
- common
and common/tasks/main.yml is
---
- name: Update apt
apt: update_cache=yes
When doing vagrant up, the output is
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: ansible-provision_default_1412793587231_72507
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> 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: 22 => 2200 (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:2200
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Checking for host entries
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/bram/Projects/Brammm/ansible-provision
==> default: Running provisioner: ansible...
PLAY [Bring up server with MySQL, Nginx, and PHP-FPM] *************************
GATHERING FACTS ***************************************************************
fatal: [default] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue
TASK: [common | Update apt] ***************************************************
FATAL: no hosts matched or all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit #/Users/bram/site.retry
default : ok=0 changed=0 unreachable=1 failed=0
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
If I look at .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory, I see the following:
default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200
I would expect the IP there to be the same as set in the private_network? I've been staring at this for over an hour, did I do something wrong? I have a feeling the IP isn't being set properly or something. I can ping 192.168.6.66.
The problem here that your site.yml overrides the remote user ansible will use to root. But, you do not provide the private key for it, nor password.
So the way, I fixed it was to set ansible_ssh_user to "vagrant", because it is default known user, and ansible will behave the same as vagrant ssh if remote_user is not overriden. And set sudo true, because "vagrant" user is sudoer, but not su.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.6.66"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
config.vm.provision "ansible" do |ansible|
ansible.playbook = "site.yml"
ansible.extra_vars = { ansible_ssh_user: 'vagrant' }
ansible.sudo = true
#ansible.verbose = 'vvvv'
end
end
Please refer to the "WHY DOES THE ANSIBLE PROVISIONER CONNECT AS THE WRONG USER?" section Ansible Provisioning of Vagrant Documentation
Vagrant will create it's own ansible inventory file (which you've seen) with the defaults of 127.0.0.1 and 2200.
You need to specify the ansible inventory file using ansible.inventory_path
A very simple inventory file for use with Vagrant might look like:
default ansible_ssh_host=192.168.111.222 Where the above IP address is
one set in your Vagrantfile:
config.vm.network :private_network, ip: "192.168.111.222"
From https://docs.vagrantup.com/v2/provisioning/ansible.html
I had Ansible on Vagrant 1.7 trying to connect as root, probably because of some separate deployment repo group vars defaults. Possible fixes:
set ansible.force_remote_user = true or ansible_ssh_user extra/host/group vars appropriately
or move to Vagrant 1.8, which forces remote user by default, as per the changelog
I was having a similar problem, where it wouldn't ssh into the server for whatever reason for provisioning.
vagrant ssh worked fine, but vagrant provision would fail to ssh, due to a timeout.
I finally connected the dots and realized that this was actually because of the VPN client that my employer has us use. Cisco AnyConnect VPN client does some kind of buggery and makes it so that I have to restart my entire workstation to be able to run it.
Luckily, I don't have to be connected to a VPN for many things, so this only happens sporadically for me.