How to exclude (ignore) certain folders in vagrant rsync? - vagrant

I wish to run npm install in my vagrant virtual box.
But whenever I ran the npm install command, my rsync will execute. Since my host computer does not have node_modules installed, it simply remove the folder completely for me.
What do I need to do so that my vagrant rsync will ignore the node_modules folder?
I cannot have node_modules being rsynced into the guest machine because my host and guest are two different systems. So far my vagrantfile looks like this.
Vagrant.configure("2") do |config|
config.vm.box = "laravel/homestead"
config.vm.hostname = "localhost"
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.synced_folder "./", "/home/vagrant/app"
config.vm.provider "virtualbox" do |v|
v.name = "web2"
v.memory = "4096"
v.cpus = 2
v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/SHARE_NAME", "1"]
end
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync_auto: true, rsync_exclude: ['node_modules*']
config.vm.provision :shell, path: "provision/setup.sh"
end
And I execute vagrant rsync via
vagrant rsync-auto

rsync__exclude as well as rsync__auto takes 2 _
You need to rewrite your line as
config.vm.synced_folder "./", "/home/vagrant/app", type: "rsync", rsync__auto: true, rsync__exclude: ['./node_modules*']

Step 3 on http://perrymitchell.net/article/npm-symlinks-through-vagrant-windows/ worked better for me in the end...
From the page:
The steps are simple:
Delete the node_modules directory if it exists.
Create a directory called, say "node_modules_projectname" in the VM's home directory (~) (Some articles and posts recommend making the
directory in /tmp, but obviously this is cleared on reboot, so it may
not be an optimal experience for this type of thing).
Link a local node_modules dir from within the project's directory
ln -s ~/node_modules_projectname /path/to/your-project/node_modules
Install the packages in the project directory:
npm install

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 causes contents to be erased

I'm trying to add add an additional synced folder besides the default /vagrant.
I've read the documentation which indicates that this should be as simple as:
config.vm.synced_folder "/path/to/somewhere/on/host", "/path/to/guest_directory", create: true
When I add this to my Vagrantfile and then reprovision it, the guest_directory folder shows up in the expected place on the host, but it's empty. And the folder in the guest OS is also empty... which breaks my site because those are the website files. Interestingly, if I remove that from the Vagrantfile and reprovision again, the files reappear in the guest OS where they were originally.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
# NEITHER OF THESE WORK...
# config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/home/vagrant/web__public_web", create: true
# config.vm.synced_folder "./web__public_web", "/home/vagrant/web__public_web", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
You can see that I've tried a two different type: options and got the same wrong result with both. I also tried placing a symlink to the project files inside /vagrant/ and that didn't work either. (The broken symlink showed up in the host os directory instead of the files I want to access)
Here are a couple more details about my setup, just in case they're relevant:
In the guest OS the project files live in /home/vagrant/web__public_web but the document root is /srv/www/... which contains a symlink pointing to the former path.
VirtualBox: Version 5.2.18 r124319 (Qt5.6.3)
Vagrant: v2.1.0
OSX: 10.13.6
What am I doing wrong?
Each mount requires a unique id, if the box is already built you'll need to rebuild it.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.99"
config.vm.synced_folder "/Users/ESL/Sites/project/web__public_web", "/path/where/mounted/on/vm", id: "unique_id_1", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.synced_folder "/home/vagrant/web__public_web", "/path/where/mounted/on/vm2", id: "unique_id_2", type: "rsync", rsync__args: ["--verbose", "--archive", "--delete", "-z"], create: true
config.vm.provider "virtualbox" do |vb|
vb.memory = "2046"
end
end
I answered my own question...
It turns out the sync was working all along. But I misunderstood the directionality of the sync. I was expecting the files in the guest OS to show up in the host directory, but it's the other way around. The empty folder on the host directory was getting synced to the guest and wiping out all the files.
After I manually copied all of the files from the guest to the host directory, the same configurations worked as expected.
Bottom line: if you want to sync an existing guest folder with files in it, you have to manually move it into place on the host OS first.

Vagrant - VM can not follow host-symlink within virtualbox synced_folder

I set up a vagrant machine to maintain an older project of mine. I use VirtualBox as provider and created a synced_folder to publish the project to the vm.
The amount of assets is kind of huge (~40GByte), so i dont want to keep that on my ssd. So i moved those assets to an hdd and created a symlink from my project-folder pointing to the hdd. All of this happens on the host system.
My problem is, that when i ssh into my vagrant machine and inspect the assets-folder i can see a broken symlink. I want my vm to follow this path.
The folder structure (the part, that matters) looks like:
root/
|--src/
|--public/
|--|--assets -> /media/hdd
As you can see /root/public/assets points to my hdd mount.
My synced_folder is set up like
Vagrant.configure("2") do |config|
# skipped lines
# skipped lines
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
# skipped lines
# skipped lines
end
Now i dont know how to configure the synced_folder to follow the path on the host machine.
I thought about having another line like
config.vm.synced_folder "/media/hdd", "/vagrant/public/assets", type: "virtualbox"
but i am not the only one using this setup, so i would like to solve it with some kind of option, like:
config.vm.synced_folder ".", "/vagrant", type: "virtualbox", options: ["optionIDontKnowAbout", "1"]
What i tried so far:
config.vm.provider "virtualbox" do |vb|
# should allow symlinks in synced folders
vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
end
But that did not help.
Thanks in advance
For the moment i use the following workaround. It does not answer my initial quesion, so i wont delete it:
if ENV['USER'].downcase == 'yourusername'
config.vm.synced_folder "/media/hdd", "/media/hdd", type: "virtualbox"
end

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.

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)

Resources