Specify Vagrantfile path explicity, if not plugin
Asked Answered
T

6

35

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?

Thuggee answered 25/6, 2013 at 22:26 Comment(0)
F
27

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!

Factious answered 26/6, 2013 at 5:49 Comment(1)
Thanks so much I had no idea you could embed ruby in the Vagrantfile :D Silly of me seeing how it's ruby syntax. Thanks so much! This was exactly the answer I needed. I'm probably going to have an array of all the possible environments then check ARGV[1] against each one then bring up the desired one. This is so much simpler than what I had in mind.Thuggee
B
75

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

Bruxelles answered 25/3, 2015 at 12:39 Comment(0)
F
27

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!

Factious answered 26/6, 2013 at 5:49 Comment(1)
Thanks so much I had no idea you could embed ruby in the Vagrantfile :D Silly of me seeing how it's ruby syntax. Thanks so much! This was exactly the answer I needed. I'm probably going to have an array of all the possible environments then check ARGV[1] against each one then bring up the desired one. This is so much simpler than what I had in mind.Thuggee
R
22

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.

Roethke answered 11/3, 2014 at 21:14 Comment(0)
P
2

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.

Pebrook answered 15/7, 2013 at 12:33 Comment(1)
Thanks for your answer, but I've been busy mutilating what used to look like a Vagrantfile into a full-fledged ruby application to do everything that I want. Feel free to check out github.com/jedmunds/vagrant to see what I've been up to! It's incredibly the amount you can extend vagrant.Thuggee
D
2

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)

Donatelli answered 14/3, 2017 at 20:52 Comment(0)
S
1

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.

Sulfaguanidine answered 6/1, 2022 at 12:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.