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

6
我使用Vagrant启动我的测试环境。不幸的是,我必须在启动我的Vagrant box之前检索信息(密码)。到目前为止,我使用Vagrant-Triggers来完成这个任务,并拥有多个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

谢谢你的帮助!

2
为什么你不能像这样运行脚本:run "Vagrant_trigger_before.sh" - Frederic Henri
这个方法是否仍然允许变量替换 'IS' 显示? - Jesse Adelman
很遗憾,不知道。 - lony
3个回答

5
您可以直接使用run指令运行您的脚本。
[:up, :provision].each do |cmd|
    config.trigger.before cmd, stdout: true do
      run "Vagrant_trigger_before.sh"
    end
end

4

在触发器插件被合并到Vagrant主干之后,语法似乎已经发生了变化:

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

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

这个链接提供了有关如何内联定义Vagrant触发器配置的信息。通过内联定义,可以直接在Vagrantfile中编写触发器,而不需要单独的配置文件。通过使用内联定义,可以更轻松地管理Vagrant触发器。

0

除了这是一个相当古老的帖子外,以下是一些对于其他人有用的提示:

  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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接