vagrant/website/source/docs/triggers/usage.html.md

134 lines
3.3 KiB
Markdown
Raw Normal View History

2018-03-29 16:08:25 +00:00
---
layout: "docs"
page_title: "Vagrant Triggers Usage"
sidebar_current: "triggers-usage"
description: |-
Various Vagrant Triggers examples
2018-03-29 16:08:25 +00:00
---
# Basic Usage
2018-03-29 16:08:25 +00:00
Below are some very simple examples of how to use Vagrant Triggers.
## Examples
2018-04-05 21:19:35 +00:00
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:
```ruby
Vagrant.configure("2") do |config|
config.trigger.before :up do |trigger|
trigger.name = "Hello world"
trigger.info = "I am running before vagrant up!!"
end
config.trigger.after :up do |trigger|
2018-04-05 21:19:35 +00:00
trigger.name = "Hello world"
trigger.info = "I am running after vagrant up!!"
end
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
end
end
```
These will run before and after each defined guest in the Vagrantfile.
Running a remote script to save a database on your host before __destroy__ing a
guest:
```ruby
Vagrant.configure("2") do |config|
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 > /vagrant/outfile"}
end
end
end
```
Now that the trigger is defined, running the __destroy__ command will fire off
the defined trigger before Vagrant destroys the machine.
```shell
$ vagrant destroy ubuntu
```
An example of defining three triggers that start and stop tinyproxy on your host
machine using homebrew:
```shell
#/bin/bash
# start-tinyproxy.sh
brew services start tinyproxy
```
```shell
#/bin/bash
# stop-tinyproxy.sh
brew services stop tinyproxy
```
```ruby
Vagrant.configure("2") do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.before :up do |trigger|
trigger.info = "Starting tinyproxy..."
trigger.run = {path: "start-tinyproxy.sh"}
end
ubuntu.trigger.after :destroy, :halt do |trigger|
trigger.info = "Stopping tinyproxy..."
trigger.run = {path: "stop-tinyproxy.sh"}
end
end
end
```
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.
```ruby
Vagrant.configure("2") do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.after :up do |trigger|
trigger.info = "More information with ruby magic"
trigger.ruby do |env,machine|
puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`
end
end
end
end
```
If you are defining your triggers using the hash syntax, you must use the `Proc`
type for defining a ruby trigger.
```ruby
Vagrant.configure("2") do |config|
config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = "ubuntu"
ubuntu.trigger.after :up,
info: "More information with ruby magic",
ruby: proc{|env,machine| puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`}
end
end
```