使用Vagrant触发器在主机上执行bash脚本



我使用Vagrant来启动我的测试环境。可悲的是,在旋转我的流浪汉盒子之前,我必须检索信息(密码)。到目前为止,我使用Vagrant触发器来执行此操作,并且有多个run "do something"命令。

[:up, :provision].each do |cmd|
    config.trigger.before cmd, stdout: true do
      run "rm -rf #{cookbooks_path}"
      run "mkdir -p #{cookbooks_path}"
      run "touch fileX"
      run "touch fileY"
      run "touch fileZ"
    end
end

如何将所有命令移动到一个批处理文件中,然后我只 包括?

应该

[:up, :provision].each do |cmd|
    config.trigger.before cmd, stdout: true do
      include_script "Vagrant_trigger_before.sh"
    end
end

感谢您的帮助!

您可以使用

run指令直接运行脚本

[:up, :provision].each do |cmd|
    config.trigger.before cmd, stdout: true do
      run "Vagrant_trigger_before.sh"
    end
end

触发器插件合并到流浪主线后,语法似乎更改为

config.trigger.before :up, :provision do |trigger|
  trigger.run = {inline: "Vagrant_trigger_before.sh"}
end

参考: https://www.vagrantup.com/docs/triggers/configuration#inline

除了这是一个相当古老的线程之外,还有一些有用的提示给其他最终在这里的人:

  1. 正如 @frederic-henri 和 @lin-n 指出的那样,您可以使用trigger.run和当前的触发器语法在特定 Vagrant 命令之前或之后在主机上执行脚本。
  2. trigger.run 接受脚本的参数

总结(未测试):

config.trigger.before :up, :provision do |trigger|
  trigger.run do |run|
    run.args = cookbooks_path
    run.path = <Script>
  end
end

最新更新