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

Use Vagrant trigger to execute bash script on host

我使用 Vagrant 启动我的测试环境。遗憾的是,我必须在启动我的 Vagrant 盒子之前检索信息(密码)。到目前为止,我使用 Vagrant-Triggers 来执行此操作并有多个 run "do something" 命令。

IS

[: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

How can I move all my commands to one batch file which I then only include?

应该

[: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

trigger插件合并到vagrant mainline后,语法好像改成了

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 和当前 trigger syntax 在特定 Vagrant 命令之前或之后在主机上执行脚本
  2. trigger.run 接受脚本参数

总结(未测试):

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