» Configuration

Vagrant Triggers has a few options to define trigger behavior.

» Execution Order

The trigger config block takes two different operations that determine when a triggershould fire:

  • before
  • afterThese define how the trigger behaves and when it should fire off duringthe Vagrant life cycle. A simple example of a before operation could look like:
  1. config.trigger.before :up do |t|
  2. t.info = "Bringing up your Vagrant guest machine!"
  3. end

Triggers can also be used with commands, actions, or hooks.By default triggers will be defined to run before or after a Vagrant guest. For moredetailed examples of how to use triggers, check out the usage section.

» Trigger Options

The trigger class takes various options.

  • action (symbol, array) - Expected to be a single symbol value, an array of symbols, or a splat of symbols. The first argument that comes after either before or after when defining a new trigger. Can be any valid Vagrant command. It also accepts a special value :all which will make the trigger fire for every action. An action can be ignored with the ignore setting if desired. These are the valid action commands for triggers:

  • ignore (symbol, array) - Symbol or array of symbols corresponding to the action that a trigger should not fire on.

  • info (string) - A message that will be printed at the beginning of a trigger.

  • name (string) - The name of the trigger. If set, the name will be displayed when firing the trigger.

  • on_error (symbol) - Defines how the trigger should behave if it encounters an error. By default this will be :halt, but can be configured to ignore failures and continue on with :continue.

  • only_on (string, regex, array) - Limit the trigger to these guests. Values can be a string or regex that matches a guest name.

  • ruby (block) - A block of Ruby code to be executed on the host. The block accepts two arguments that can be used with your Ruby code: env and machine. These options correspond to the Vagrant environment used (note: these are not your shell's environment variables), and the Vagrant guest machine that the trigger is firing on. This option can only be a Proc type, which must be explicitly called out when using the hash syntax for a trigger.

  1. ubuntu.trigger.after :up do |trigger|
  2. trigger.info = "More information"
  3. trigger.ruby do |env,machine|
  4. greetings = "hello there #{machine.id}!"
  5. puts greetings
  6. end
  7. end
  • run_remote (hash) - A collection of settings to run a inline or remote script with on the guest. These settings correspond to the shell provisioner.

  • run (hash) - A collection of settings to run a inline or remote script on the host. These settings correspond to the shell provisioner. However, at the moment the only settings run takes advantage of are:

  • warn (string) - A warning message that will be printed at the beginning of a trigger.

  • exit_codes (integer, array) - A set of acceptable exit codes to continue on. Defaults to 0 if option is absent. For now only valid with the run option.

  • abort (integer,boolean) - An option that will exit the running Vagrant process once the trigger fires. If set to true, Vagrant will use exit code 1. Otherwise, an integer can be provided and Vagrant will it as its exit code when aborting.

» Trigger Types

Optionally, it is possible to define a trigger that executes around Vagrant commands,hooks, and actions.

Warning! This feature is still experimental and may break orchange in between releases. Use at your own risk.

This feature currently reqiures the experimental flag to be used. To explicitly enable this feature, you can set the experimental flag to:

  1. VAGRANT_EXPERIMENTAL="typed_triggers"

Please note that VAGRANT_EXPERIMENTAL is an environment variable. For moreinformation about this flag visit the Experimental docs pagefor more info. Without this flag enabled, triggers with the :type optionwill be ignored.

A trigger can be one of three types:

  • type (symbol) - Optional
    • :action - Action triggers run before or after a Vagrant action
    • :command - Command triggers run before or after a Vagrant command
    • :hook - Action hook triggers run before or after a Vagrant hookThese types determine when and where a defined trigger will execute.
  1. config.trigger.after :destroy, type: :command do |t|
  2. t.warn = "Destroy command completed"
  3. end

» Quick Note

Triggers without the type option will run before or after a Vagrant guest.

Older Vagrant versions will unfortunately not be able to properly parse the new:type option. If you are worried about older clients failing to parse your Vagrantfile,you can guard the new trigger based on the version of Vagrant:

  1. if Vagrant.version?(">= 2.3.0")
  2. config.trigger.before :status, type: :command do |t|
  3. t.info = "before action!!!!!!!"
  4. end
  5. end

» Commands

Command typed triggers can be defined for any valid Vagrant command. They will alwaysrun before or after the command.

The difference between this and the default behavior is that these triggers arenot attached to any specific guest, and will always run before or after the givencommand. A simple example might be running a trigger before the up command to givea simple message to the user:

  1. config.trigger.before :up, type: :command do |t|
  2. t.info = "Before command!"
  3. end

For a more detailed example, please check out the examplespage for more.

» Hooks

Advanced topic! This is an advanced topic for use only ifyou want to execute triggers around Vagrant hooks. If you are just gettingstarted with Vagrant and triggers, you may safely skip this section.

Hook typed triggers can be defined for any valid Vagrant action hook that is defined.

A simple example would be running a trigger on a given hook called action_hook_name.

  1. config.trigger.after :action_hook_name, type: :hook do |t|
  2. t.info = "After action hook!"
  3. end

For a more detailed example, please check out the examplespage for more.

» Actions

Advanced topic! This is an advanced topic for use only ifyou want to execute triggers around Vagrant actions. If you are just gettingstarted with Vagrant and triggers, you may safely skip this section.

Action typed triggers can be defined for any valid Vagrant action class. Actionsin this case refer to the Vagrant class #Action, which is used internally toVagrant and in every Vagrant plugin.

  1. config.trigger.before :"Action::Class::Name", type: :action do |t|
  2. t.info = "Before action class!
  3. end

For a more detailed example, please check out the examplespage for more.