如何在vagrant base(baseline)框中包含和引用自定义ssh密钥?(的virtualbox)

Dan*_*nie 4 ssh virtualbox vagrant vagrantfile

在vagrant文​​档中,我没有找到关于如何在使用"vagrant package"时从同一基线框中包含的Vagrantfile引用包含文件的提示.有人可以帮忙吗?

细节:

从零开始为vagrant创建新的基线框时,您可以自由使用标准的vagrant不安全ssh密钥或创建自定义新密钥.我做了最后一件事.当我使用我的Vagrantfile时,这个新的基线框可以正常使用我的自定义键:

config.ssh.private_key_path = "custom_key_file"
Run Code Online (Sandbox Code Playgroud)

现在我决定将我的基线框分发给我的团队成员.那没问题.只需输入:

vagrant package --output custom.box
Run Code Online (Sandbox Code Playgroud)

所有其他团队成员都将"custom_key_file"复制到项目根目录,并使用此内容创建"Vagrantfile"(使用版本控制系统完成):

# -*- 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
Run Code Online (Sandbox Code Playgroud)

完成后,每个团队成员输入以下内容以快速简便地获取基于custom.box的虚拟机:

vagrant box add custombox custom.box
vagrant up
Run Code Online (Sandbox Code Playgroud)

工作良好.

现在我想在分发之前稍微调整我的基线框.我想要包含"custom_key_file"和"Vagrantfile.pkg",内容如下:

# -*- 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
Run Code Online (Sandbox Code Playgroud)

要创建调整的基线框,请输入:

vagrant package --output custom2v.box --vagrantfile Vagrantfile.pkg --include custom_key_file
Run Code Online (Sandbox Code Playgroud)

当我提取custom2v.box时,我可以看到有这棵树:

C:.
?   box-disk1.vmdk
?   box.ovf
?   Vagrantfile
?
????include
        custom_key_file
        _Vagrantfile
Run Code Online (Sandbox Code Playgroud)

并且"include/_Vagrantfile"具有我的Vagrantfile.pkg的内容.我可以按如下方式添加该框:

vagrant box add custombox2v custom2v.box
Run Code Online (Sandbox Code Playgroud)

对于一个新项目,现在很容易为流浪者启用它.只需添加"Vagrantfile",如下所示:

# -*- 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
Run Code Online (Sandbox Code Playgroud)

但是,当我做一个:

vagrant up
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

[...]
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
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

m1k*_*eil 5

原因是Vagrant的加载顺序和其配置的合并.

你想要发生的是Vagrant使用盒子档案中的私钥.

当你运行"up"时真正发生的是Vagrant在你的"加载和合并"路径上将你的配置与其他几个配置合并.

所以在最后你有一个大配置的方式:

config.ssh.private_key_path = "custom_key_file"

因此,Vagrant将custom_key_file在您的Vagrantfile所在的文件夹中查找,这就是您收到错误的原因.

检查此答案此问题,以获取有关如何配置Vagrant以查找相对于box的Vagrantfile的密钥的信息.