Basic Usage
Below are some very simple examples of how to use Vagrant Triggers.
Examples
The following is a basic example of two global triggers. One that runs before
the :up
command and one that runs after the :up
command:
These will run before and after each defined guest in the Vagrantfile.
Running a remote script to save a database on your host before destroying a guest:
Now that the trigger is defined, running the destroy command will fire off the defined trigger before Vagrant destroys the machine.
An example of defining three triggers that start and stop tinyproxy on your host machine using homebrew:
Running vagrant up
would fire the before trigger to start tinyproxy, where as
running either vagrant destroy
or vagrant halt
would stop tinyproxy.
Ruby Option
Triggers can also be defined to run Ruby, rather than bash or PowerShell. An
example of this might be using a Ruby option to get more information from the VBoxManage
tool. In this case, we are printing the ostype
defined for thte guest after
it has been brought up.
If you are defining your triggers using the hash syntax, you must use the Proc
type for defining a ruby trigger.
Typed Triggers
Below are some basic examples of using :type
triggers. They cover commands, hooks,
and actions.
It is important to note that while command
triggers will be a fairly common use case,
both action
and hook
triggers are more complicated and are a more advanced use case.
Commands
The most common use case for typed triggers are with command
. These kinds of
triggers allow you to run something before or after a subcommand in Vagrant.
Because they are specifically for subcommands, they do not work with any guest
operations like run_remote
or if you define the trigger as a guest trigger.
Hooks
Below is an example of a Vagrant trigger that runs before and after each defined provisioner:
Notice how this trigger runs before each provisioner defined for the guest:
Actions
With action typed triggers, you can fire off triggers before or after certain
Action classes. A simple example of this might be warning the user when Vagrant
invokes the GracefulHalt
action.