如果已经配置了Vagrant VM,请避免重新配置

Dav*_*ter 17 vagrant

我正在尝试获取一个shell配置程序,以避免重新配置VM实例(如果它之前已经这样做过).

考虑以下Vagrantfile:

Vagrant::Config.run do |config|
  config.vm.define :minimal do |config|
    # Base image
    config.vm.box = "lucid32"
    config.vm.box_url = "http://files.vagrantup.com/lucid32.box"

    config.vm.provision :shell, :inline => "mkdir /tmp/foobar"
  end
end
Run Code Online (Sandbox Code Playgroud)

如果您运行vagrant up minimal,它将创建该框并最初进行配置.如果你然后运行vagrant provision minimal它将尝试重新配置该框但会失败(因为/ tmp/foobar目录已经存在).

有没有办法让Vagrant记住它过去是否配置了一台机器并避免以后重置它?

更多上下文:如果我运行vagrant up minimal,重新启动我的主机,然后vagrant up minimal再次运行,它将尝试重新配置该框并失败.这种情况经常发生,因为VirtualBox经常在我的主机上引起内核恐慌.

Lee*_*een 10

这可能不是您想要的答案,但如果您将其更改mkdir为a mkdir -p,它将起作用;)

但是,严肃地说,我认为Vagrant期望配置者是幂等的(也就是说,如果第二次运行,它将不采取任何行动).

实现真正的幂等性可能很棘手,取决于您在配置脚本中实际执行的操作,但这mkdir -p是一个良好的开端.您还可以在系统上创建一个标志文件,并首先检查该标志文件是否存在; 如果它存在,只是exit 0.

  • 创建幂等性的简单而愚蠢的方法是在配置结束时"触摸〜/ .VM_PROVISIONED"(假设一切都成功)并通过`[-e~/.VM_PROVISIONED]`检查它的存在性. (5认同)

All*_*ate 6

如果您正在使用bash配置脚本,则可能性不会是幂等的.

这是一个如何避免配置两次的低级示例:

PROVISIONED="/some-app-dir/PROVISIONED";

if [[ -f $PROVISIONED ]]; then
  echo "Skipping provisioning";
  exit;
else
  echo "Provisioning";
fi

#...do provisioning things

touch $PROVISIONED;
Run Code Online (Sandbox Code Playgroud)


Lee*_*Gee 5

你看过这个吗?

vagrant up --no-provision

$ vagrant up --help
Usage: vagrant up [vm-name] [options] [-h]

    --[no-]provision             Enable or disable provisioning
    --provision-with x,y,z       Enable only certain provisioners, by type.
    --[no-]parallel              Enable or disable parallelism if provider supports it.
    --provider provider          Back the machine with a specific provider.
-h, --help                       Print this help
Run Code Online (Sandbox Code Playgroud)