Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I configure Vagrant to setup a synced folder only if it exists on the host?

Tags:

vagrant

For our developers ~/Development/framework_repo is normally available on the host, but for our designers it wouldn't be. Is it possible to conditionally configure a synced folder depending on it's availability on the host?

like image 488
Kit Sunde Avatar asked Jan 12 '15 11:01

Kit Sunde


People also ask

What is NFS in Vagrant?

NFS is a faster way of sharing your project's files between host and guests that sync the files instantly. To use NFS with Vagrant, nfs-utils package needs to be installed and nfs-server needs to be running. If it is not, run: $ sudo dnf install nfs-utils && sudo systemctl enable nfs-server.

How do I transfer files from local to Vagrant?

Using SCP. Another way to copy files and folders from host to guest in Vagrant is to use to SCP. Firstly, make sure you have scp installed on your host. If you don't have it, you can install the openssh-clients package if you're using Linux or install Cygwin if you're using Windows.


1 Answers

Since the Vagrantfile is a Ruby script, you could check for the presence of the directory with File.directory(), and enable the shared folder only if needed.

For example:

Vagrant.configure("2") do |config|
  if File.directory?(File.expand_path("~/Development/framework_repo"))
    config.vm.synced_folder "~/Development/framework_repo", "/guest/path"
  end
end
like image 123
BrianC Avatar answered Oct 01 '22 03:10

BrianC