5

In vagrant documentation i did not found a hint on how to reference an included file from a included Vagrantfile within the same baseline box when using "vagrant package". Can anyone help?

Details:

When creating a new baseline box from scratch for vagrant, you are free to use the standard vagrant insecure ssh key or to create a custom new key. I did the last thing. And this new baseline box works fine with my custom key, when i use my Vagrantfile with this additional line:

config.ssh.private_key_path = "custom_key_file"

Now i decided to distribute my baseline box to my team members. Thats no problem. Just enter:

vagrant package --output custom.box

All other team members do copy the "custom_key_file" to the project root dir and create a "Vagrantfile" with this content (done using a version controll system):

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "custombox"
    config.ssh.private_key_path = "custom_key_file"
end

When done, each team member enter the following to get a virtual machine based on custom.box fast and easy:

vagrant box add custombox custom.box
vagrant up

Works fine.

Now i want to tune my baseline box a little bit before distributing. I want to include the "custom_key_file" and a "Vagrantfile.pkg" that reads as follows:

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

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "custombox"
    config.ssh.private_key_path = "custom_key_file"
end

To create the tuned baseline box i enter:

vagrant package --output custom2v.box --vagrantfile Vagrantfile.pkg --include custom_key_file

When i extract the custom2v.box i can see there is this tree:

C:.
│   box-disk1.vmdk
│   box.ovf
│   Vagrantfile
│
└───include
        custom_key_file
        _Vagrantfile

And "include/_Vagrantfile" has the content of my Vagrantfile.pkg. I can add that box as follows:

vagrant box add custombox2v custom2v.box

to a new project it is now very easy to enable it for vagrant. Just add a "Vagrantfile" read as follows:

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

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
    config.vm.box = "custombox2v"
end

But when i do a:

vagrant up

i get the following error message:

[...]
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

SSH:
* `private_key_path` file must exist: custom_key_file

Can anyone help?

DanielaWaranie
  • 1,405
  • 1
  • 17
  • 22

1 Answers1

6

The reason is Vagrant's load order and merging of its configs.

What you want to happen is Vagrant to use the private key located inside the box archive.

What really happens when you run "up" is Vagrant merges your config with few others configs on its "load & merge" path.

So in the end of the way you have one big config with the setting:

config.ssh.private_key_path = "custom_key_file"

So Vagrant will look for custom_key_file in the same folder as your Vagrantfile and that's why you get your error.

Check this answer and this issue for information how to config Vagrant to look for the key relatively to box's Vagrantfile.

Community
  • 1
  • 1
m1keil
  • 4,515
  • 22
  • 26
  • I expected that vagrant remembers all pathes where vagrant finds a Vagrantfile while merging the config. Further i expected that vagrant iterates over all these pathes to lookup for referenced files. I failed. It seams that vagrant is not working that way. Sadly it seams that there is no environment variable or ruby variable to be used to make the config portable (no absolute path). Thanks for the link to the issue. I will use the ruby statements to "implement" that missing feature and accept your answer. – DanielaWaranie Aug 21 '14 at 06:22