Use Vagrant trigger to execute bash script on host
Asked Answered
H

3

6

I use Vagrant to start my testing environment. Sadly I have to retrieve information (passwords) before spinning up my Vagrant box. So far I use Vagrant-Triggers to do this and have multiple run "do something" commands.

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?

SHOULD

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

Thank you for your help!

Halvorsen answered 5/1, 2017 at 10:30 Comment(3)
why cant you run the script as run "Vagrant_trigger_before.sh"Woo
Does this method still allow the variable substitution the 'IS' shows?Pogey
Sadly, do not know.Halvorsen
W
5

You can run your script directly using the run instructions

[:up, :provision].each do |cmd|
    config.trigger.before cmd, stdout: true do
      run "Vagrant_trigger_before.sh"
    end
end
Woo answered 5/1, 2017 at 16:22 Comment(0)
D
5

After trigger plugin was merged to vagrant mainline, the syntax seems to be changed to

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

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

Dopp answered 3/8, 2020 at 4:47 Comment(0)
S
0

Besides this being a quite old thread, some useful hints for others who end up here:

  1. As @frederic-henri and @lin-n pointed out, you can use trigger.run and current trigger syntax to execute a script on the host before or after specific Vagrant commands
  2. trigger.run accepts arguments for the script

Summing up (not tested):

config.trigger.before :up, :provision do |trigger|
  trigger.run do |run|
    run.args = cookbooks_path
    run.path = <Script>
  end
end
Swan answered 28/9, 2021 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.