Reputation: 517
Is there any way to explicity specify the path of a Vagrantfile? My company wants to do something like this: For testing on a confluence machine, type a command like vagrant spinup confluence
, and then point that to a Vagrantfile in a different directory that contains the confluence environment, and then brings up all of these machines.
However, it doesn't look like there is any way to explicitly state what Vagrantfile to use, and I'm somewhat (very) new at ruby, so I'm having a hard time writing my own plugin for it. Does anyone have recommendations on what to do? Or has anyone done something similar to this?
Upvotes: 35
Views: 21092
Reputation: 1020
Use VAGRANT_CWD variable
VAGRANT_CWD is the directory where vagrant looks for Vagrantfile
Quote from official docs :
VAGRANT_CWD can be set to change the working directory of Vagrant. By default, Vagrant uses the current directory you are in. The working directory is important because it is where Vagrant looks for the Vagrantfile.
Upvotes: 1
Reputation: 1611
Further to Andrew Lorente's answer, you can also use the VAGRANT_VAGRANTFILE
environment variable to specify the filename of the Vagrantfile
. This has the advantage over VAGRANT_CWD
of not changing the current working directory which can be useful when relying on relative paths.
For example, the following will run vagrant up
on Vagrantfile.other
:
VAGRANT_VAGRANTFILE=Vagrantfile.other vagrant up
Upvotes: 75
Reputation: 431
Putting shell commands in parenthesis creates a sub-shell, so from Git for Windows (or bash on Linux/OS X), you can do:
(cd test/ruby; vagrant up)
Upvotes: 2
Reputation: 1784
While other answerers are correct that this particular case didn't need separate Vagrantfiles, I think there are legitimate uses for specifying a Vagrantfile path--reading vagrant ssh-config
information in a script, for example.
Happily, you can do it by setting the VAGRANT_CWD
environment variable:
VAGRANT_CWD=~/some/path/ vagrant ssh-config
This doesn't appear to be documented anywhere, but you can see it in the source.
Upvotes: 22
Reputation: 109
You can have as many different Vagrantfile
s as you want/need. Just create a new directory for your project (which can then consist of just one or multiple VMs), then cd
into that directory and run vagrant init
to make Vagrant create a new Vagrantfile
which then can be customized to your needs. To start your VM using that new Vagrantfile
just run vagrant up
from inside the directory that contains it.
Upvotes: 2
Reputation: 10890
There is no need to have a separate Vagrantfile, you can just define multiple VM's in the same file. See the documentation here: http://docs.vagrantup.com/v2/multi-machine/index.html
If you are just using one VM in your 'normal' environment and one VM for your 'confluence' environment then it is simply a case of just defining each VM and vagrant up
-ing the specific VM.
If you have multiple machines that make up each of your environments then you have two options, you can use regular expressions and make sure you name and type the commands correctly or you can put a bit of logic into your Vagrantfile to make it easier for people.
For example with a little bit of a hack in your Vagrantfile you can do the following:
Vagrant.configure('2') do |config|
if ARGV[1] == 'confluence'
ARGV.delete_at(1)
confluence = true
else
confluence = false
end
config.vm.provider :virtualbox do |virtualbox, override|
#virtualbox.gui = true
virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
virtualbox.customize ["modifyvm", :id, "--memory", 512]
override.vm.box = 'Ubuntu 12.10 x64 Server'
override.vm.box_url = 'http://goo.gl/wxdwM'
end
if confluence == false
config.vm.define :normal1 do |normal1|
normal1.vm.hostname = 'normal1'
normal1.vm.network :private_network, ip: "192.168.1.1"
end
config.vm.define :normal2 do |normal2|
normal2.vm.hostname = 'normal2'
normal2.vm.network :private_network, ip: "192.168.1.2"
end
end
if confluence == true
config.vm.define :confluence1 do |confluence1|
confluence1.vm.hostname = 'confluence1'
confluence1.vm.network :private_network, ip: "192.168.1.3"
end
config.vm.define :confluence2 do |confluence2|
confluence2.vm.hostname = 'confluence2'
confluence2.vm.network :private_network, ip: "192.168.1.4"
end
end
end
Now vagrant up
brings up your normal vm's and vagrant up confluence
brings up your confluence vm's!
Upvotes: 27