Working with Triggers
1. Defining Before Triggers
| Trigger Type | Description | Usage |
|---|---|---|
| trigger.before | Executes before a specific Vagrant action | config.trigger.before :up, :provision do |trigger| |
Example: Defining a Before Trigger
config.trigger.before :up, :provision do |trigger|
trigger.name = "Check dependencies"
trigger.run = { path: "check-deps.sh" }
end
Note: Before triggers run before the specified action and can be used to validate prerequisites or prepare the environment.
2. Defining After Triggers
After triggers in Vagrant execute after a specific action has completed. They are defined using the trigger block within the Vagrantfile.
| Property | Description | Values |
|---|---|---|
| trigger.after | Specifies the action after which the trigger should run | :up, :provision, :reload, :destroy |
| trigger.name | Unique identifier for the trigger | String |
| trigger.execute | Command or script to execute | String or block |
Example: After Provision Trigger
vagrant.trigger.after :provision do
puts "Provisioning completed!"
end
Note: Triggers are executed in the context of the Vagrant environment and can access Vagrant's API.
3. Running Commands
| Command | Description | Example |
|---|---|---|
| vagrant trigger run | Executes triggers defined in the Vagrantfile | vagrant trigger run |
| vagrant trigger run --name <trigger_name> | Runs a specific trigger by name | vagrant trigger run --name provision |
Example: Trigger Configuration
Vagrant.configure("2") do |config|
config.trigger.after :up, "echo 'Provisioning complete'"
end
4. Running Inline Scripts
| Property | Description | Example |
|---|---|---|
| trigger | Defines a trigger that runs an inline script | trigger :inline, "echo 'Running inline script'" |
| on | Specifies the event that activates the trigger | on :create, :update |
| inline | Specifies the inline script to execute | inline "echo 'Hello from Vagrant'" |
Example: Running a Script on VM Creation
config.trigger.after :up do
run "echo 'VM is up and running'"
end
Example: Inline Script with Variable Substitution
config.trigger.before :provision do
run "echo 'Provisioning started at: $(date)'"
end
5. Setting Trigger Conditions
| Condition Type | Description | Example |
|---|---|---|
| on | Specifies the event that triggers the action | "on" : "provision" |
| if | Boolean expression to evaluate before running the trigger | "if" : "machine.state == 'running'" |
| unless | Opposite of if, runs only if condition is false |
"unless" : "machine.state == 'not_created'" |
Example: Trigger on Provision
Vagrant.configure("2") do |config|
config.trigger.after :provision, "echo 'Provisioning complete'"
end
Example: Conditional Trigger
Vagrant.configure("2") do |config|
config.trigger.after :up, "echo 'Machine is up'" do
if "machine.state == 'running'"
end
end
6. Using Trigger Names
| Trigger Name | Description | Usage |
|---|---|---|
| vagrant.up | Triggered when 'vagrant up' is executed | vagrant trigger --target vagrant.up --action "echo 'Provisioning started'" |
| vagrant.halt | Triggered when 'vagrant halt' is executed | vagrant trigger --target vagrant.halt --action "echo 'Machine halted'" |
| vagrant.destroy | Triggered when 'vagrant destroy' is executed | vagrant trigger --target vagrant.destroy --action "echo 'Machine destroyed'" |
| vagrant.provision | Triggered when 'vagrant provision' is executed | vagrant trigger --target vagrant.provision --action "echo 'Provisioning complete'" |
| vagrant.reload | Triggered when 'vagrant reload' is executed | vagrant trigger --target vagrant.reload --action "echo 'Machine reloaded'" |
Example: Using Trigger Names in Vagrantfile
trigger :vagrant.up do |
on_success "echo 'Vagrant up triggered'"
end
trigger :vagrant.halt do |
on_success "echo 'Vagrant halt triggered'"
end
7. Configuring Ignore Errors
| Option | Values | Description |
|---|---|---|
| ignore_errors | true | false |
When set to true, Vagrant will continue executing subsequent triggers even if a previous trigger fails. Defaults to false. |
Example: Setting ignore_errors to true
Vagrant.configure("2") do |config|
config.trigger.after :up, :ignore_errors => true do
run "echo 'This will run even if previous trigger failed'"
end
end
Note: Setting
ignore_errors to true allows for more resilient automation but should be used carefully as it can mask issues that need attention.8. Setting Abort on Error
| Option | Values | Description |
|---|---|---|
| trigger.abort_on_error | true | false |
If set to true, the trigger will abort the Vagrant operation if any error occurs during execution. Defaults to false. |
Example: Configuring Abort on Error
Vagrant.configure("2") do |config|
config.trigger.after :up, abort_on_error: true do
run "echo 'Provisioning completed successfully'"
end
end
9. Using Info Messages
Info messages in Vagrant are used to display informational content to users during the provisioning process. These messages do not halt execution and are useful for providing context or status updates.
| Option | Values | Description |
|---|---|---|
| trigger.info | string |
Displays an informational message to the user during provisioning. |
Example: Displaying an Info Message
Vagrant.configure("2") do |config|
config.trigger.after :up, :info => "Provisioning complete!" do
run :local => "echo 'Vagrant up completed.'"
end
end
10. Executing Ruby Code
| Property | Description | Example |
|---|---|---|
| trigger | Defines a trigger block in Vagrantfile | |
| trigger.exec | Executes a shell command or Ruby code | trigger.exec "ruby -e 'puts ENV[\\"VAGRANT_ENV\\"]'\"" |
| trigger.exec Ruby | Executes inline Ruby code | trigger.exec "ruby -e \\"puts 'Hello from Ruby'\\"" |
Example: Executing Ruby Code in Trigger
vagrant.trigger.after :up do |trigger|
trigger.exec "ruby -e 'puts ENV[\\"VAGRANT_ENV\\"]'\""
end
Note: Ruby code must be properly escaped when passed as a string to
trigger.exec.