vagrant Vagrantfile mount point ignored - vagrant

I have a Vagrantfile as below running under libvirt. When the box boots, the project directory is mounted under "/vagrant" not "/path/to/source". It works fine on another machine under VirtualBox.
Any ideas? Symlinking /vagrant to the actual place I want the mount is hacky.
vagrant up --debug shows that it isn't even attempting it. The port forwarding works OK though.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "mybox"
file = File.open("#{Dir.home}/.mybox_key", "rb")
key = file.read
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.synced_folder "./", "/path/to/source/", type: "nfs"
end

If you're running an older version of Vagrant that doesn't support this option (e.g. 1.3.5), it may fail silently instead of trying to setup the NFS share. Ensure you're running at least Vagrant 1.5.1.

Related

vagrant ssh fail when using synced_folder

I want to mount a local directory relative to my vagrant file inside the vm. However, when I do that,
vagrant ssh
No longer works -- private key fails. I am not sure why ssh suddenly fails.
help? (if it matters: I am trying to mount the directory my Java artifacts compile to).
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
config.vm.box = "ubuntu/trusty64"
# Configurate the virtual machine to use 2GB of RAM
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.cpus = 1
end
config.vm.network :forwarded_port, guest: 8080, host: 8080
config.vm.synced_folder "target/", "/home/vagrant"
end
As you try to sync directly on /home/vagrant in the VM, you need to make sure your target directory contains a .ssh folder with the authorized_keys
When you vagrant up the VM, vagrant will ssh and create this directory for you but when going through the sync_folder part, it will replace all the content from /home/vagrant with your host target/ so loosing what it did create before.
If you really really want to sync on /home/vagrant what you could do is run first without the sync, copy all files that have been created (.ssh/, .bash ...) into your target directory and then you should be able to rerun with the sync on /home/vagrant. (Note: I did not try that and honestly would not recommend to sync on /home/vagrant directly as if you install other soft from provisioning or other, you might run into issues later)

Connecting my vagrant and another VM in VirtualBox

For my web development, I use a vagrant install to run a local instance of the website with the following config:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "scotch/box"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network :forwarded_port, host: 4567, guest: 80
config.vm.hostname = "some-website"
config.vm.synced_folder ".", "/var/www/public", :mount_options => ["dmode=777", "fmode=666"]
config.vm.provision :shell, path: "bootstrap.sh", :args => "some-website", privileged: false
end
This website is accessible in the browser on the specified IP.
However, I also have VM's for cross browser testing and these VM's don't have access to the IP address of the Vagrant box.
I need them to connect, because I want to do cross browser testing on that locally running vagrant box.
A couple of StackOverflow searches gave me the option of changing the private_network to a public_network, but then I can't access the IP anymore.
Also adding virtualbox__intnet: true doesn't work.
Would anyone be able to come up with a solution for this? It would be great if I could access the IP from the IE9 VM, directly into the Vagrant VM.
So, to clarify, currently:
Laptop can access Vagrant (laptop -> VM)
IE9 VM can't access Vagrant (VM -!> VM)
What I want:
Laptop can access Vagrant (laptop -> VM)
IE9 VM can access Vagrant (VM -> VM)
As Frédéric pointed out: the private_network is actually accessible from another VM on the same host.
My problem seemed to be WordPress itself and not related to Vagrant or the VM setup.

Vagrant not listening to my port and using default port

I'm using Vagrant for multiple projects now with a per-project-installation, meaning I have multiple boxes. One for each project (or for production enviroment)
When I want to access my first Vagrantbox I access it via http://sitea.com:8080. My second Vagrantbox should listen to 1337 (I tried multiple ports here) but when I call http://siteb.com:1337 I'm getting an error in all browsers (ERR_CONNECTION_REFUSED in Chrome).
However, when I'm calling my second site like http://siteb.com it works without any problems. It seems that my second Vagrantbox just doesn't listen to the ports and uses the default port. But why is that?
First box (Which works with port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.50.4"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Second box (Which works without port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 1337
config.vm.network "private_network", ip: "192.168.50.5"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Both have the same vhost configuration, and my hosts file looks like this
# Vagrant
192.168.50.4 sitea.com
192.168.50.5 siteb.com
When using port forwarding with Vagrant you'll usually access localhost since the forwarded port is available at your host machine. Like this:
http://localhost:8080 -> should deliver the website siteA.com
http://localhost:1337 -> should deliver the website siteB.com
If you are able to access http://siteB[:80] it means that the DNS entry of siteB points to 192.168.50.5, which is the private network ip of that box.
Check:
getent hosts | grep siteB\.com
I bet it will give you 192.168.50.5.

Vagrant 'permission denied' on Windows

I am having trouble accessing files through Vagrant on Windows. I have been using it on OS X for quite some time and have my Vagrantfile setup correctly which works every time.
I have sent my colleague the same Vagrant file, he is on Windows and receives 'Permission Denied' when trying to access files through the browser.
Just to be clear, the permission errors are returned by the server when accessing 'dev.local' in the browser and not from Vagrant itself... it will be a configuration error on Windows or within the VM.
The VM is CentOS 6.5
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "chef/centos-6.5"
config.vm.network "private_network", ip: "192.168.33.21"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.provision :shell, :path => "install.sh"
config.vm.hostname = "dev.local"
config.vm.synced_folder ".", "/home", id: "vagrant", :nfs => false, :mount_options => ["dmode=777","fmode=777"]
config.ssh.insert_key = false
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
end
Can any Windows Vagrant users shed any light on this?
It was VBGuestAdditions out of date. The permission error was caused by not being able to sync to my local folder (which contained an index.php) so it was using the servers /home folder which didn't contain anything and since viewing directory structure is disabled it returned permission errors.
I had 4.X.X installed, and VirtualBox is on 5.X.X Here is the fix:
run command: vagrant plugin install vagrant-vbguest
the run vagrant up which may still throw an error as the plugin fails to copy a file.
vagrant ssh to get into the box and run the following command:
sudo ln -s /opt/VBoxGuestAdditions-5.X.X/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions
Replace 5.X.X with the version of your VirtualBox.
logout and run vagrant reload
do a happy dance

Vagrant unidirectional synced folder

I have been using synced folders in Vagrant for a while, but the behaviour is not what I expected. It seems both changes in the host machines and changes in the guest machine are synced. However, I want syncing to be in only one direction: from the host to the guest. Part of the reason I want to create a Vagrant machine is that, if my code accidentally does something destructive, like deleting itself, I want that behaviour to be contained in a safe environment.
For reference, this is the Vagrantfile I am using:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise32"
config.vm.provision :shell, :path => "bootstrap.sh"
config.vm.network :forwarded_port, host: 8080, guest: 80
config.vm.synced_folder "test", "/test", :nfs => true
config.vm.network "private_network", type: "dhcp"
end
where bootstrap.h just executes apt-get install nfs-common portmap.
What is the easiest way I can configure unidirectional syncing in Vagrant?
Starting from Vagrant 1.5 you can use rsync as sync folder method:
Vagrant.configure("2") do |config|
config.vm.synced_folder "test", "/test", type: "rsync",
end
This is unidirectional.

Resources