2018-03-29 16:08:25 +00:00
---
layout: "docs"
page_title: "Vagrant Triggers Configuration"
sidebar_current: "triggers-configuration"
description: |-
2018-04-23 16:14:31 +00:00
Documentation of various configuration options for Vagrant Triggers
2018-03-29 16:08:25 +00:00
---
2018-03-30 22:37:43 +00:00
# Configuration
2018-03-29 16:08:25 +00:00
2018-04-23 16:14:31 +00:00
Vagrant Triggers has a few options to define trigger behavior.
2018-04-04 22:00:26 +00:00
2019-01-22 23:05:21 +00:00
## Execution Order
The trigger config block takes two different operations that determine when a trigger
should fire:
* `before`
* `after`
2019-01-24 22:31:54 +00:00
These define _how_ the trigger behaves and when it should fire off during
2019-01-22 23:05:21 +00:00
the Vagrant life cycle. A simple example of a _before_ operation could look like:
```ruby
config.trigger.before :up do |t|
t.info = "Bringing up your Vagrant guest machine!"
end
```
2019-02-07 21:34:12 +00:00
Triggers can also be used with [_commands_ ](#commands ), [_actions_ ](#actions ), or [_hooks_ ](#hooks ).
By default triggers will be defined to run before or after a Vagrant guest. For more
2019-01-24 22:31:54 +00:00
detailed examples of how to use triggers, check out the [usage section ](/docs/triggers/usage.html ).
2019-01-22 23:05:21 +00:00
## Trigger Options
2018-03-30 22:37:43 +00:00
The trigger class takes various options.
2018-04-24 18:07:37 +00:00
* `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:
- `destroy`
- `halt`
- `provision`
- `reload`
- `resume`
2018-06-13 16:51:45 +00:00
- `suspend`
2018-04-24 18:07:37 +00:00
- `up`
2018-03-30 22:37:43 +00:00
* `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.
2018-04-04 22:00:26 +00:00
* `name` (string) - The name of the trigger. If set, the name will be displayed when firing the trigger.
2018-03-30 22:37:43 +00:00
* `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` .
2018-05-06 19:25:46 +00:00
* `only_on` (string, regex, array) - Limit the trigger to these guests. Values can be a string or regex that matches a guest name.
2018-03-30 22:37:43 +00:00
2018-10-08 16:47:03 +00:00
* `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.
2018-10-04 23:24:28 +00:00
```ruby
ubuntu.trigger.after :up do |trigger|
trigger.info = "More information"
trigger.ruby do |env,machine|
greetings = "hello there #{machine.id}!"
puts greetings
end
end
```
2018-05-06 19:23:20 +00:00
* `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 ](/docs/provisioning/shell.html ).
2018-03-30 22:37:43 +00:00
2018-05-06 19:23:20 +00:00
* `run` (hash) - A collection of settings to run a inline or remote script on the host. These settings correspond to the [shell provisioner ](/docs/provisioning/shell.html ). However, at the moment the only settings `run` takes advantage of are:
2018-04-05 16:08:09 +00:00
+ `args`
+ `inline`
+ `path`
2018-03-30 22:37:43 +00:00
* `warn` (string) - A warning message that will be printed at the beginning of a trigger.
2018-07-09 22:19:13 +00:00
* `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.
2018-09-22 00:08:26 +00:00
* `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.
2019-01-22 23:05:21 +00:00
## Trigger Types
2019-02-07 21:35:47 +00:00
Optionally, it is possible to define a trigger that executes around Vagrant commands,
2019-01-24 22:31:54 +00:00
hooks, and actions.
2019-01-22 23:05:21 +00:00
< div class = "alert alert-warning" >
< strong > Warning!< / strong > This feature is still experimental and may break or
change in between releases. Use at your own risk.
2019-01-24 22:31:54 +00:00
This feature currently reqiures the experimental flag to be used. To explicitly enable this feature, you can set the experimental flag to:
2019-01-22 23:05:21 +00:00
```
VAGRANT_EXPERIMENTAL="typed_triggers"
```
2019-01-24 22:31:54 +00:00
Please note that `VAGRANT_EXPERIMENTAL` is an environment variable. For more
information about this flag visit the [Experimental docs page ](/docs/experimental/ )
for more info. Without this flag enabled, triggers with the `:type` option
will be ignored.
2019-01-22 23:05:21 +00:00
< / div >
2019-02-07 00:29:40 +00:00
A trigger can be one of three types:
2019-01-22 23:05:21 +00:00
* `type` (symbol) - Optional
- `:action` - Action triggers run before or after a Vagrant action
2019-02-07 21:35:47 +00:00
- `:command` - Command triggers run before or after a Vagrant command
2019-02-07 00:29:40 +00:00
- `:hook` - Action hook triggers run before or after a Vagrant hook
2019-01-22 23:05:21 +00:00
These types determine when and where a defined trigger will execute.
```ruby
config.trigger.after :destroy, type: :command do |t|
t.warn = "Destroy command completed"
end
```
2019-01-24 22:31:54 +00:00
#### Quick Note
Triggers _without_ the type option will run before or after a Vagrant guest.
2019-01-22 23:05:21 +00:00
2019-06-13 14:37:55 +00:00
Older Vagrant versions will unfortunately not be able to properly parse the new
2019-02-07 00:29:40 +00:00
`:type` option. If you are worried about older clients failing to parse your Vagrantfile,
2019-01-24 22:31:54 +00:00
you can guard the new trigger based on the version of Vagrant:
```ruby
if Vagrant.version?(">= 2.3.0")
config.trigger.before :status, type: :command do |t|
t.info = "before action!!!!!!!"
end
end
```
2019-01-23 21:23:39 +00:00
2019-01-22 23:05:21 +00:00
### Commands
2019-02-07 21:35:47 +00:00
Command typed triggers can be defined for any valid Vagrant command. They will always
run before or after the command.
2019-01-22 23:05:21 +00:00
2019-02-07 00:29:40 +00:00
The difference between this and the default behavior is that these triggers are
not attached to any specific guest, and will always run before or after the given
command. A simple example might be running a trigger before the up command to give
a simple message to the user:
2019-01-22 23:05:21 +00:00
```ruby
2019-02-07 00:29:40 +00:00
config.trigger.before :up, type: :command do |t|
t.info = "Before command!"
2019-01-22 23:05:21 +00:00
end
```
2019-02-07 00:29:40 +00:00
For a more detailed example, please check out the [examples ](/docs/triggers/usage.html#commands )
page for more.
2019-01-22 23:05:21 +00:00
2019-01-23 21:23:39 +00:00
### Hooks
< div class = "alert alert-warning" >
< strong > Advanced topic!< / strong > This is an advanced topic for use only if
you want to execute triggers around Vagrant hooks. If you are just getting
started with Vagrant and triggers, you may safely skip this section.
< / div >
2019-02-07 00:29:40 +00:00
Hook typed triggers can be defined for any valid Vagrant action hook that is defined.
2019-01-24 22:31:54 +00:00
2019-02-07 00:29:40 +00:00
A simple example would be running a trigger on a given hook called `action_hook_name` .
2019-01-24 22:31:54 +00:00
```ruby
2019-02-07 00:29:40 +00:00
config.trigger.after :action_hook_name, type: :hook do |t|
t.info = "After action hook!"
2019-01-24 22:31:54 +00:00
end
```
2019-02-07 00:29:40 +00:00
For a more detailed example, please check out the [examples ](/docs/triggers/usage.html#hooks )
page for more.
2019-01-22 23:05:21 +00:00
2019-02-07 00:29:40 +00:00
### Actions
2019-01-24 22:31:54 +00:00
2019-01-22 23:05:21 +00:00
< div class = "alert alert-warning" >
< strong > Advanced topic!< / strong > This is an advanced topic for use only if
you want to execute triggers around Vagrant actions. If you are just getting
started with Vagrant and triggers, you may safely skip this section.
< / div >
2019-02-07 00:29:40 +00:00
Action typed triggers can be defined for any valid Vagrant action class. Actions
in this case refer to the Vagrant class `#Action` , which is used internally to
Vagrant and in every Vagrant plugin.
```ruby
config.trigger.before :"Action::Class::Name", type: :action do |t|
t.info = "Before action class!
end
```
For a more detailed example, please check out the [examples ](/docs/triggers/usage.html#actions )
page for more.