检查 Vagrant 配置是否已完成 [英] Check if Vagrant provisioning has been done
问题描述
我正在使用 Vagrant 部署虚拟机进行开发.其中一项要求是 vagrant provision
创建一个新用户(在我编写的配置脚本中完成),然后 vagrant ssh
作为该用户连接到框.
I am using Vagrant to deploy VMs for development. One of the requirements is that vagrant provision
creates a new user (done in a provisioning script I wrote) and then vagrant ssh
connects to the box as that user.
我不知道如何判断盒子是否已配置.
I cannot figure out how to tell if the box has been provisioned or not.
我看到 Vagrant 配置 code 设置 env[:provision_enabled]
如果这次运行应该进行配置,所以我想我可以做这样的事情:
I see that the Vagrant provisioning code sets env[:provision_enabled]
if this run is supposed to be doing provisioning, so I thought I would be able to do something like this:
if env[:provision_enabled]
config.ssh.username = "#{data['ssh']['provision_username']}"
else
config.ssh.username = "#{data['ssh']['username']}"
end
这个想法是,用于配置的 SSH 连接将使用一个连接,而用于其他所有内容的 SSH 连接将使用另一个.
The idea is that SSH connections for provisioning would use one connection and SSH connections for everything else would use the other.
但是,env[:provision_enabled]
在 Vagrantfile
中似乎无法访问.
However, env[:provision_enabled]
does not appear to be accessible in the Vagrantfile
.
有没有办法做到这一点?
Is there a way to do this?
推荐答案
这似乎是由 Vagrant 数据目录 (.vagrant/
) 中的 action_provision
文件决定的.它通常与您的 Vagrantfile
位于同一文件夹中.
This seems to be determined by the action_provision
file in the Vagrant data dir (.vagrant/
). It's usually located in the same folder as your Vagrantfile
.
因此,一个粗略的解决方法是根据文件是否存在在您的 Vagrantfile 中设置 ssh 用户名.不过,我无法对此进行测试,但是如果您只是重命名或删除 action_provision
文件并转到 vagrant reload
它应该再次提供.
So a crude workaround would be to set the ssh username in your Vagrantfile depending on if the file exists or not. I haven't been able to test this though, but if you just rename or remove the action_provision
file and go vagrant reload
it should provision again.
这篇关于检查 Vagrant 配置是否已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!