Bruno Finger
Bruno Finger

Reputation: 2573

Have .vagrant folder one directory up

Vagrant will create a directory called .vagrant in the same path where the Vagrantfile is located, where it stores information and boxes states.

I would like to have this directory stored in another location, the best option being a directory up (../.vagrant), defined in the Vagrantfile.

Is there any way to do that?

Upvotes: 2

Views: 842

Answers (2)

Frederic Henri
Frederic Henri

Reputation: 53713

You can do that using the VAGRANT_DOTFILE_PATH environmnent variable

VAGRANT_DOTFILE_PATH can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to .vagrant. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set VAGRANT_DOTFILE_PATH to .vagrant-laptop and .vagrant-desktop on the respective machines. (Remember to update your .gitignore!)

Looking at https://github.com/mitchellh/vagrant/issues/3705#issuecomment-218569456 you can set this variable from your Vagrantfile - for example

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANT_DOTFILE_PATH = "#{ENV['HOME']}/project/vagrant/.vagrant";    
currpath = ENV['VAGRANT_DOTFILE_PATH'];
if(currpath.nil?)
    currpath = '.vagrant';
end
puts currpath #debugging
if(currpath != VAGRANT_DOTFILE_PATH)
    ENV['VAGRANT_DOTFILE_PATH'] = VAGRANT_DOTFILE_PATH
    ret = system "vagrant",*ARGV
    FileUtils.rm_r(currpath)
    if(ret)
      exit
    else
      abort "Finished"
    end
end

Vagrant.configure(2) do |config|
  ...
end

I believe on windows, the HOME variable is called HOMEPATH.

Upvotes: 3

nelsonda
nelsonda

Reputation: 1188

You can do this by setting environment variable for Vagrant, specifically VAGRANT_DOTFILE_PATH.

So in this case:

cd /path/to/Vagrantfile
export VAGRANT_DOTFILE_PATH=../.vagrant
vagrant up

Upvotes: 1

Related Questions