» 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:

  1. Vagrant.configure("2") do |config|
  2. config.trigger.before :up do |trigger|
  3. trigger.name = "Hello world"
  4. trigger.info = "I am running before vagrant up!!"
  5. end
  6. config.trigger.after :up do |trigger|
  7. trigger.name = "Hello world"
  8. trigger.info = "I am running after vagrant up!!"
  9. end
  10. config.vm.define "ubuntu" do |ubuntu|
  11. ubuntu.vm.box = "ubuntu"
  12. end
  13. 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 destroying aguest:

  1. Vagrant.configure("2") do |config|
  2. config.vm.define "ubuntu" do |ubuntu|
  3. ubuntu.vm.box = "ubuntu"
  4. ubuntu.trigger.before :destroy do |trigger|
  5. trigger.warn = "Dumping database to /vagrant/outfile"
  6. trigger.run_remote = {inline: "pg_dump dbname > /vagrant/outfile"}
  7. end
  8. end
  9. end

Now that the trigger is defined, running the destroy command will fire offthe defined trigger before Vagrant destroys the machine.

  1. $ vagrant destroy ubuntu

An example of defining three triggers that start and stop tinyproxy on your hostmachine using homebrew:

  1. #/bin/bash
  2. # start-tinyproxy.sh
  3. brew services start tinyproxy
  1. #/bin/bash
  2. # stop-tinyproxy.sh
  3. brew services stop tinyproxy
  1. Vagrant.configure("2") do |config|
  2. config.vm.define "ubuntu" do |ubuntu|
  3. ubuntu.vm.box = "ubuntu"
  4. ubuntu.trigger.before :up do |trigger|
  5. trigger.info = "Starting tinyproxy..."
  6. trigger.run = {path: "start-tinyproxy.sh"}
  7. end
  8. ubuntu.trigger.after :destroy, :halt do |trigger|
  9. trigger.info = "Stopping tinyproxy..."
  10. trigger.run = {path: "stop-tinyproxy.sh"}
  11. end
  12. end
  13. end

Running vagrant up would fire the before trigger to start tinyproxy, where asrunning either vagrant destroy or vagrant halt would stop tinyproxy.

» Ruby Option

Triggers can also be defined to run Ruby, rather than bash or powershell. Anexample of this might be using a Ruby option to get more information from the VBoxManagetool. In this case, we are printing the ostype defined for thte guest afterit has been brought up.

  1. Vagrant.configure("2") do |config|
  2. config.vm.define "ubuntu" do |ubuntu|
  3. ubuntu.vm.box = "ubuntu"
  4. ubuntu.trigger.after :up do |trigger|
  5. trigger.info = "More information with ruby magic"
  6. trigger.ruby do |env,machine|
  7. puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`
  8. end
  9. end
  10. end
  11. end

If you are defining your triggers using the hash syntax, you must use the Proctype for defining a ruby trigger.

  1. Vagrant.configure("2") do |config|
  2. config.vm.define "ubuntu" do |ubuntu|
  3. ubuntu.vm.box = "ubuntu"
  4. ubuntu.trigger.after :up,
  5. info: "More information with ruby magic",
  6. ruby: proc{|env,machine| puts `VBoxManage showvminfo #{machine.id} --machinereadable | grep ostype`}
  7. end
  8. end

» 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 oftriggers allow you to run something before or after a subcommand in Vagrant.

  1. config.trigger.after :status, type: :command do |t|
  2. t.info = "Showing status of all VMs!"
  3. end

Because they are specifically for subcommands, they do not work with any guestoperations 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 definedprovisioner:

  1. config.trigger.before :provisioner_run, type: :hook do |t|
  2. t.info = "Before the provision!"
  3. end
  4. config.vm.provision "file", source: "scripts/script.sh", destination: "/test/script.sh"
  5. config.vm.provision "shell", inline: <<-SHELL
  6. echo "Provision the guest!"
  7. SHELL

Notice how this trigger runs before each provisioner defined for the guest:

  1. ==> guest: Running provisioner: Sandbox (file)...
  2. ==> vagrant: Running hook triggers before provisioner_run ...
  3. ==> vagrant: Running trigger...
  4. ==> vagrant: Before the provision!
  5. guest: /home/hashicorp/vagrant-sandbox/scripts/script.sh => /home/vagrant/test/script.sh
  6. ==> guest: Running provisioner: shell...
  7. ==> vagrant: Running hook triggers before provisioner_run ...
  8. ==> vagrant: Running trigger...
  9. ==> vagrant: Before the provision!
  10. guest: Running: inline script
  11. guest: Provision the guest!

» Actions

With action typed triggers, you can fire off triggers before or after certainAction classes. A simple example of this might be warning the user when Vagrantinvokes the GracefulHalt action.

  1. config.trigger.before :"Vagrant::Action::Builtin::GracefulHalt", type: :action do |t|
  2. t.warn = "Vagrant is halting your guest..."
  3. end