summaryrefslogtreecommitdiff
path: root/devdocs/vagrant/triggers%2Findex.html
blob: 9ec41dd22db5a4cea47194a98dd6db7894c7418c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<h1 id="vagrant-triggers">  Vagrant Triggers </h1> <p>As of version 2.1.0, Vagrant is capable of executing machine triggers <em>before</em> or <em>after</em> Vagrant commands.</p> <p>Each trigger is expected to be given a command key for when it should be fired during the Vagrant command lifecycle. These could be defined as a single key or an array which acts like a <em>whitelist</em> for the defined trigger.</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby"># single command trigger
config.trigger.after :up do |trigger|
...
end

# multiple commands for this trigger
config.trigger.before [:up, :destroy, :halt, :package] do |trigger|
...
end

# or defined as a splat list
config.trigger.before :up, :destroy, :halt, :package do |trigger|
...
end
</pre></div>
<p>Alternatively, the key <code>:all</code> could be given which would run the trigger before or after every Vagrant command. If there is a command you don't want the trigger to run on, you can ignore that command with the <code>ignore</code> option.</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby"># single command trigger
config.trigger.before :all do |trigger|
  trigger.info = "Running a before trigger!"
  trigger.ignore = [:destroy, :halt]
end
</pre></div>
<p><strong>Note:</strong> <em>If a trigger is defined on a command that does not exist, a warning will be displayed.</em></p> <p>Triggers can be defined as a block or hash in a Vagrantfile. The example below will result in the same trigger:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">config.trigger.after :up do |trigger|
  trigger.name = "Finished Message"
  trigger.info = "Machine is up!"
end

config.trigger.after :up,
  name: "Finished Message",
  info: "Machine is up!"
</pre></div>
<p>Triggers can also be defined within the scope of guests in a Vagrantfile. These triggers will only run on the configured guest. An example of a guest only trigger:</p> <div class="highlight"><pre class="highlight ruby" data-language="ruby">config.vm.define "ubuntu" do |ubuntu|
  ubuntu.vm.box = "ubuntu"
  ubuntu.trigger.before :destroy do |trigger|
    trigger.warn = "Dumping database to /vagrant/outfile"
    trigger.run_remote = {inline: "pg_dump dbname &gt; /vagrant/outfile"}
  end
end
</pre></div>
<p>Global and machine-scoped triggers will execute in the order that they are defined within a Vagrantfile. Take for example an abstracted Vagrantfile:</p> <div class="highlight"><pre class="highlight plaintext">Vagrantfile
  global trigger 1
  global trigger 2
  machine defined
    machine trigger 3
  global trigger 4
end
</pre></div>
<p>In this generic case, the triggers would fire in the order: 1 -&gt; 2 -&gt; 3 -&gt; 4</p> <p>For more information about what options are available for triggers, see the <a href="configuration">configuration section</a>.</p><div class="_attribution">
  <p class="_attribution-p">
    &copy; 2010&ndash;2018 Mitchell Hashimoto<br>Licensed under the MPL 2.0 License.<br>
    <a href="https://www.vagrantup.com/docs/triggers/" class="_attribution-link">https://www.vagrantup.com/docs/triggers/</a>
  </p>
</div>