Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Vagrantfile path explicity, if not plugin

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?

like image 491
addicted2unix Avatar asked Jun 25 '13 22:06

addicted2unix


4 Answers

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

Notes

  • This used to appear to be an undocumented feature which was only visible in the source code.
  • In later releases this is now documented in the Hashicorp docs here: Environmental Variables.
like image 196
Mark Hobson Avatar answered Nov 07 '22 13:11

Mark Hobson


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!

like image 27
Matt Cooper Avatar answered Nov 07 '22 15:11

Matt Cooper


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.

like image 21
Erin Call Avatar answered Nov 07 '22 13:11

Erin Call


You can have as many different Vagrantfiles 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.

like image 2
avhilchen Avatar answered Nov 07 '22 14:11

avhilchen