How-to specify a trigger to run before a specific provisioner script

558 views
Skip to first unread message

Steve Freeman

unread,
Feb 11, 2020, 11:10:33 AM2/11/20
to Vagrant
I have a shell provisioners specified with names, like so:

config.vm.provision "disk", type: "shell", path: "provision.ps1"

How do I get a trigger to execute specific code only before this particular provisioner?

Brian Cain

unread,
Feb 11, 2020, 3:21:53 PM2/11/20
to vagra...@googlegroups.com
Hey Steve - 

What kind of trigger are you writing? If it's just a trigger that runs on the guest, the easiest thing to do
would be to use a regular provisioner instead.
 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/416eb13e-c41c-40cc-97f2-cfadd64eb1f1%40googlegroups.com.


--
Brian Cain

Steve Freeman

unread,
Feb 11, 2020, 3:28:58 PM2/11/20
to Vagrant
I'm trying to write out a configuration file based on the state of execution at that point in the overall provisioning process.  If I don't use a trigger, then the code will execute the code to write the file with different parameters while the Vagrantfile is being evaluated. The resulting file is the last one generated. The file isn't just a handful of properties to be written or I'd pass the values as arguments and write the file locally in the provisioning script.

Here is what I'd like to be able to do:

            config.trigger.before [:up,:provision], :provisioner_name => "install" do |trigger|
                trigger.info = "Generate install configuration"
                trigger.ruby do |env, machine|
                    puts "generating configuration for #{json['version'][0]}"
                    generate_configuration( json, baseOS, node_name )
                end
            end


On Tuesday, February 11, 2020 at 3:21:53 PM UTC-5, Brian Cain wrote:
Hey Steve - 

On Tue, Feb 11, 2020 at 8:10 AM Steve Freeman <free...@gmail.com> wrote:
I have a shell provisioners specified with names, like so:

config.vm.provision "disk", type: "shell", path: "provision.ps1"

How do I get a trigger to execute specific code only before this particular provisioner?

What kind of trigger are you writing? If it's just a trigger that runs on the guest, the easiest thing to do
would be to use a regular provisioner instead.
 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagra...@googlegroups.com.


--
Brian Cain

Steve Freeman

unread,
Feb 11, 2020, 3:29:46 PM2/11/20
to Vagrant
One other point, the trigger runs on the host, not the guest.  The resulting file is to be used on the guest.

.

unread,
Feb 11, 2020, 3:39:56 PM2/11/20
to vagra...@googlegroups.com
Vagrant has the idea of events and triggers, however, they are very limited in what they can do. They typically only fire when a Vagrant specific command fires - https://www.vagrantup.com/docs/triggers/ You can create your own (I believe that the host_updater plugin takes advantage of this)

remote.trigger.after :up do |trigger|
        trigger.only_on = box_info[box_info.length - 1][:host]
        trigger.ruby do |env,machine|
          puts ""
        end
end

If you are looking to target a specific machine - you can target that machine on the Ruby/Vagrantfile side like

if default_os != 'bento/ubuntu-14.04' then
      puts "Updating Underlying OS"
      config.vm.provision "shell", inline: $your_script
end



--
Dan Morgan

Brian Cain

unread,
Feb 11, 2020, 3:48:23 PM2/11/20
to vagra...@googlegroups.com
On Tue, Feb 11, 2020 at 12:29 PM Steve Freeman <freem...@gmail.com> wrote:
I'm trying to write out a configuration file based on the state of execution at that point in the overall provisioning process.  If I don't use a trigger, then the code will execute the code to write the file with different parameters while the Vagrantfile is being evaluated. The resulting file is the last one generated. The file isn't just a handful of properties to be written or I'd pass the values as arguments and write the file locally in the provisioning script.

Here is what I'd like to be able to do:

            config.trigger.before [:up,:provision], :provisioner_name => "install" do |trigger|
                trigger.info = "Generate install configuration"
                trigger.ruby do |env, machine|
                    puts "generating configuration for #{json['version'][0]}"
                    generate_configuration( json, baseOS, node_name )
                end
            end

You can write a trigger that runs before a given Vagrant hook, i.e. before the provision step:


Please note the additional type option that defines this trigger as a hook.

config.trigger.before :provisioner_run, type: :hook do |t|
  t.info = "Before the provision!"
  t.ruby do |env,machine|
    puts "ruby stuff!"
    generate_stuff()
  end
end
 
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/446d83dc-2dbf-4bc8-8fcc-0a5fc2f711ed%40googlegroups.com.


--
Brian Cain
Message has been deleted

Brian Cain

unread,
Feb 14, 2020, 2:55:16 PM2/14/20
to vagra...@googlegroups.com


On Fri, Feb 14, 2020 at 10:53 AM Steve Freeman <freem...@gmail.com> wrote:
Okay, but how do I obtain the provisioner's name so I can conditionally execute the code?

That would run before all provisioners. You can't run a trigger before a specific provisioner at the moment.
You can run provisioners before or after other provisioners, but unfortunately those only run on the guest, and aren't what you want based on the method
you're using to generate configuration.
 

On Tuesday, February 11, 2020 at 3:48:23 PM UTC-5, Brian Cain wrote:


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages