Vagrant synced/shared folder - vagrant

In my Vagrantfile (Vagrant 1.7.4) I have the following:
config.vm.synced_folder ".", "/home/vagrant/sync", type: "rsync", :mount_options => ["dmode=777", "fmode=666"], rsync__exclude: [".git/"]
When I issue vagrant up, it outputs the following:
==> default: Rsyncing folder: /project/ => /home/vagrant/sync
==> default: - Exclude: [".vagrant", ".git/",]
==> default: Mounting shared folders...
default: /vagrant => /project
I don't understand why there is a shared and a synced folder. I'm using virtualbox so i understand these are the same but I just want a shared folder - why are there 2?

So first sync folder is the Vagrant term for Virtual Box shared folder, so you're correct to say the 2 are the same. Now coming to your question,
By default, Vagrant will share your project directory (the directory
with the Vagrantfile) to /vagrant.
This is defined as config.vm.synced_folder ".", "/vagrant"; so this one always comes, if you define or not new sync folder.
If you want not to have the default sync folder, you can add the following to your Vagrant file
config.vm.synced_folder ".", "/vagrant", disabled: true

Related

Sync folders after provision script has ran?

Is it possible to have vagrant sync a folder after the provision script is ran?
I need to sync a guest folder, with the host folder, but the host folder must be empty for the provision script to work. The user and group also need to exist. Non of which can exist until the provision script has ran. My provision script creates the user, and group.
The synced folder is a Magento theme. Magento first needs to install before I can sync the directories as Magento will not install into a non empty directory.
Here is my Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "bento/ubuntu-18.04"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provision "shell", path: "provision.sh"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.synced_folder "app/design/frontend",
"/var/www/mage/app/design/frontend",
create: false,
owner: "mage",
group: "www-data"
config.vm.provider "virtualbox" do |vb|
vb.memory = "3000"
end
end
I've taken a look at triggers, but I can figure out how to use config.vm.synced_folder within a trigger.
Any advice?
I've found a solution.
In the Vagrantfile set config.vm.synced_folder to be false like so (also works for nfs):
config.vm.synced_folder "app/design/frontend",
"/var/www/mage/app/design/frontend",
type: "rsync",
disabled: false,
owner: "mage",
group: "www-data"
Now run vagrant up. Once the server provisioning is complete change disabled to true like so:
config.vm.synced_folder "app/design/frontend",
"/var/www/mage/app/design/frontend",
type: "rsync",
disabled: true,
owner: "mage",
group: "www-data"
Now run vagrant reload and the folder will be synced.
The docs say
disabled (boolean) - If true, this synced folder will be disabled and
will not be setup. This can be used to disable a previously defined
synced folder or to conditionally disable a definition based on some
external factor.
Which makes me believe this may be one of the intended ways to solve my problem.
There is a feature request to mount folders post-provision which seems to be closed and not resolved so I don't think there's any other way to this: https://github.com/hashicorp/vagrant/issues/936

Vagrant synced_folder /vagrant shared_folder missing

Hi I'm trying to provision my box using vagrant and ansible but I always encounter /vagrant/ansible/playbook.yml is missing. It's looking for a /vagrant directory. When I ssh inside my box the /vagrant does not exist. I'm not sure what happen but what I did was to sync all my local copy into a specific folder location inside my box. Below is a sample implementation I did
Vagrant.configure("2") do |config|
config.vm.define "ans", primary: true do |ans|
ans.vm.box = "ubuntu/xenial64"
ans.vm.network "forwarded_port", guest: 80, host: 8080
ans.vm.network "private_network", ip: "10.8.9.20", auto_network: true
ans.vm.synced_folder ".", "/var/www/"
ans.vm.hostname = "my_project"
ans.vm.provision "pre-build", type: "shell", :path => "ansible/bootstrap.sh"
ans.vm.provision "ansible_local" do |ansible|
ansible.playbook = "ansible/playbook.yml"
end
end
end
I'm getting an error in the ansible.playbook = "ansible/playbook.yml" it seems this line is looking under /vagrant folder which does not exist. What will be the workaround or fix for this? My playbook exist in this location /var/www/ansible/playbook.yml inside the box
By default, vagrant automatically add a shared folder from your current directory with /vagrant folder on the VM.
Because you have added
ans.vm.synced_folder ".", "/var/www/"
the current folder shared folder has been replaced from /vagrant to /var/www (you can easily verify that by removing the line from your vagrantfile)
do you really need to sync the whole current folder with /var/www ideally you would have your project files in a subdirectory that you will share in /var/www like
ans.vm.synced_folder "html/", "/var/www/"
and push all your html/csss/js files under the html folder (feel free to rename project or something else) and you'll not need to sync all the vagrant scripts and provisioning script with your web server folder.

How to include a file in vagrant vbox

Lets say I'm running an instance of a box. How to include a file that physically resides in the same directory as box and vagrantfile? I suppose I need to state that in the vagrantfile. How should that include look?
My vagrantfile (excluding the comments) is:
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant_data"
Running the box I get:
(...)
default: SSH auth method: private key
default: Warning: Authentication failure. Retrying...
default: Warning: Authentication failure. Retrying...
I can 'vagrant ssh' into it, but theres no /vagrant_data folder available
From vagrant doc
By default, Vagrant shares your project directory (remember, that is the one with the Vagrantfile) to the /vagrant directory in your
guest machine.
Note that when you vagrant ssh into your machine, you're in /home/vagrant. /home/vagrant is a different directory from the synced
/vagrant directory.
so if you have a file that physically resides in the same directory as vagrantfile you will be able to access in the VM from the /vagrant directory.

Cannot mount vagrant synced folder with nfs

I managed to setting up my Symfony2 project inside a ubuntu vagrant box. But it takes around 20 seconds to load the website through it's webserver. After some research, i came up with using nfs for the sync folder. Here're my setting from Vagrantfile:
config.vm.network "private_network", ip: "192.168.56.101"
config.vm.synced_folder ".", "/vagrant", :nfs => true, :mount_options => ["dmode=777","fmode=777"]
After starting de vagrant box i get the following error
==> default: Mounting NFS shared folders...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mount -o 'dmode=777,fmode=777' 192.168.56.1:'/Users/marcschenk/Projects/teleboy.ch' /vagrant
Stdout from the command:
Stderr from the command:
stdin: is not a tty
mount.nfs: an incorrect mount option was specified
The VM seems to work, but the synced folder's obviously empty. What did i wrong?
My setup:
Vagrant 1.6.5 & Virtualbox 4.3.18
Host: OS X 10.10
Guest: Ubuntu 12.04
Found the solution for the problem here https://github.com/mitchellh/vagrant/issues/2546
The correct syntax for vagrant version 1.3 to 1.6 is:
config.vm.synced_folder ".", "/vagrant", :nfs => { :mount_options => ["dmode=777","fmode=777"] }

config.vm.share_folder setting not found

I am getting this error with this line in my Vagrantfile during vagrant up, until I comment it out.
The setting is documented here:
http://docs-v1.vagrantup.com/v1/docs/config/vm/share_folder.html
Not sure why the following documented paramenter causes an error
config.vm.share_folder "puppetdir", "/etc/puppet", "/vagrant/mypuppetdir"
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:
vm:
* The following settings don't exist: share_folder
The latest virtualbox and latest vagrant. everything else works fine.
On Vagrant 1.1+ you should use config.vm.synced_folder, the docs you are looking at are for older versions. Please refer to the updated documentation for more info: http://docs.vagrantup.com/v2/synced-folders/basic_usage.html
Shared Folders has been renamed to Synded Folder since 1.1.
In your Vagrantfile you should be using the following
config.vm.synced_folder "../data", "/vagrant_data"
# by default enabled, uncomment to disable
# config.vm.synced_folder ".", "/vagrant", disabled: true
NOTE: By default, Vagrant will share your project directory (the directory where Vagrantfile resides) to /vagrant.
config.vm.synced_folder ".", "/vagrant", disabled: true
More flexible example
vagrant_data = File.expand_path("../vagrant_data", __FILE__)
Dir::mkdir(vagrant_data) unless FileTest::directory?(vagrant_data)
config.vm.synced_folder "vagrant_data", "/vagrant_data"
Take a look at this for more information => shared folders VS synced folders
Extending from https://github.com/mitchellh/vagrant/issues/936#issuecomment-7179034
if you need to mount a volume as a user that doesn't exist when the vm boots you can get there like so:
# Vagrantfile line
config.vm.synced_folder "host_folder", "/svr/fake_mount_folder", id: "whatever_name"
# ...
config.vm.provision "shell", inline: <<-SHELL
# ...
# In my case a package installed a user with UID 110, GID 116
mount -t vboxsf -o uid=110,gid=116 whatever_name /media/actual_mounted_folder
# ...
SHELL

Resources