Origin Vagrantfile location

36 views
Skip to first unread message

Dalibor Karlović

unread,
Apr 25, 2015, 11:48:57 AM4/25/15
to vagra...@googlegroups.com
Hello,

I have a custom box which contains a embedded Vagrantfile (let's call it VF1). This file contains a lot of box-specific config / functionality and the target is I should be (and am) able to generate the project Vagrantfile (VF2) just with:

Vagrant.configure(2) do |config|
  config.vm.box = "my/box"
end


and still get all the functionality (Puppet integration, VirtualBox tweaks, YAML config support, etc). The problem is that I'd like to read files relative to VF2 from VF1. The way I see it, VF2 gets loaded before VF1 and thus, Vagrant should be able to figure out where VF2 is before loading loading VF1. So, how do I access this information?

Thanks in advance.

Alvaro Miranda Aguilera

unread,
Apr 25, 2015, 1:15:00 PM4/25/15
to vagra...@googlegroups.com
Hello Dalibor,

Are 2 things to take into consideration.

This is the overall simple answer, so apologies in advance if all this
is known and doesn't answer your question.

One is, Vagrant will load all the Vagrantfiles and follow this rule:

https://docs.vagrantup.com/v2/vagrantfile/index.html

<QUOTE>
Vagrant actually loads a series of Vagrantfiles, merging the settings
as it goes. This allows Vagrantfiles of varying level of specificity
to override prior settings. Vagrantfiles are loaded in the order shown
below. Note that if a Vagrantfile is not found at any step, Vagrant
continues with the next step.

Vagrantfile packaged with the box that is to be used for a given machine.
Vagrantfile in your Vagrant home directory (defaults to ~/.vagrant.d).
This lets you specify some defaults for your system user.
Vagrantfile from the project directory. This is the Vagrantfile that
you'll be modifying most of the time.
Multi-machine overrides if any.
Provider-specific overrides, if any.

At each level, settings set will be merged with previous values. What
this exactly means depends on the setting. For most settings, this
means that the newer setting overrides the older one.

</QUOTE>

So by default it should read and merge all the Vagrantfiles.

If your base box include other files, you can reference them like this:

include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)
config.ssh.private_key_path = File.expand_path("../vagrant_rsa", __FILE__)

This came from this thread as example:
http://comments.gmane.org/gmane.comp.tools.vagrant/4813

Alvaro.
> --
> You received this message because you are subscribed to the Google Groups
> "Vagrant" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to vagrant-up+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dalibor Karlović

unread,
Apr 25, 2015, 2:06:25 PM4/25/15
to vagra...@googlegroups.com
Hello Alvaro, thank you very much for your answer.

I'll start by linking the GH repo where you can see what I'm trying to do: https://github.com/dkarlovi/vagrant-playground

Now, to me more concrete:
  1. my custom box is called dkarlovi/centos-6.6
  2. it gets embedded with a custom Vagrantfile, https://github.com/dkarlovi/vagrant-playground/blob/master/box/_Vagrantfile
  3. when installed, that Vagrantfile ends up in /home/dkarlovi/.vagrant.d/boxes/dkarlovi-VAGRANTSLASH-centos-6.6/1.0.0/virtualbox/include/_Vagrantfile
  4. I create a new Vagrant project in /home/dkarlovi/Vagrant/project1, vagrant init dkarlovi/centos-6.6 --minimal
  5. when running, I can see that the /home/dkarlovi/Vagrant/project1/Vagrantfile gets evaluated before /home/dkarlovi/.vagrant.d/boxes/dkarlovi-VAGRANTSLASH-centos-6.6/1.0.0/virtualbox/include/_Vagrantfile so I should, in theory, be able to figure out where's the Vagrantfile that started it all
  6. I'm currently simulating this, see line 11 but this does not work if I run vagrant from say /home/dkarlovi/Vagrant/project1/src

I'm doing all of this to be able to check for existence of certain files (Puppet related) and turn provisioning on/off so I'd be able to have a full Puppet stack (if files present) or nothing (if not present), my box becomes more versatile and usable (for my usages).

I've been going through Vagrant source and found that Vagrant::Config::Loader is the place where the path is available, but I don't see where and how I should fetch it.

Alvaro Miranda Aguilera

unread,
Apr 25, 2015, 7:14:49 PM4/25/15
to vagra...@googlegroups.com
What you can use is something like this:

puts File.join(File.expand_path(File.dirname(__FILE__)), "Vagrantfile")

puts File.dirname(__FILE__)

Dalibor Karlović

unread,
Apr 26, 2015, 3:26:20 AM4/26/15
to vagra...@googlegroups.com
__FILE__ in /home/dkarlovi/.vagrant.d/boxes/dkarlovi-VAGRANTSLASH-centos-6.6/1.0.0/virtualbox/include/_Vagrantfile is equal to that path. What I need is a way to get "/home/dkarlovi/Vagrant/project1/Vagrantfile", the path of the project Vagrantfile, but from inside the box Vagrantfile. It seems to me Vagrant should have an API for this, there's no way around it.

I'll open a new issue on GH. Thank you.

You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/85PwXic-jB0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.

dragon788

unread,
Apr 27, 2015, 11:41:36 AM4/27/15
to vagra...@googlegroups.com
As Alvaro outlined, the inheritance ordering is very static. It will always load the farthest/highest Vagrantfile first, and then the closer/more specific Vagrantfiles later.

If you define a variable at the ~/.vagrant.d/ level it will persist into the project's Vagrantfile load as well.

A simple way to watch this is to define the VAGRANTFILE_API_VERSION in a "higher" Vagrantfile, and see the warnings at the VM/project level when it has already been defined.
Reply all
Reply to author
Forward
0 new messages