Set a variable in a trigger to be used by a provisioner.

27 views
Skip to first unread message

Jamie Jackson

unread,
Aug 19, 2019, 4:55:28 PM8/19/19
to vagra...@googlegroups.com
How would I set a variable in a trigger if I wanted it to be available in a provisioner?

Here's a quick (failing) test case:

# -*- mode: ruby -*-

# vi: set ft=ruby :


Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/bionic64"

  

  config.trigger.before [:up, :provision] do |trigger|

    trigger.ruby do |env, machine|

      var_derived_in_ruby_trigger = "hi"

    end

  end

    

  config.vm.provision "read_var_from_trigger",

    type: "shell",

    privileged: false,

    inline: "echo '[#{var_derived_in_ruby_trigger}']"

  

end


Output:


$ vagrant up

Vagrant failed to initialize at a very early stage:

There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
an invalid or undefined variable.

Path: /Users/jjackson/eclipse-workspace/simplevagrant/Vagrantfile
Line number: 20
Message: undefined local variable or method `var_derived_in_ruby_trigger

Thanks,
Jamie

Brian Cain

unread,
Aug 19, 2019, 7:02:40 PM8/19/19
to vagra...@googlegroups.com
On Mon, Aug 19, 2019 at 1:55 PM Jamie Jackson <jamie...@gmail.com> wrote:
How would I set a variable in a trigger if I wanted it to be available in a provisioner?

Hi there -

Variables defined inside the ruby trigger are limited to that triggers scope, and aren't made
available outside the Ruby block. There are probably some work arounds if you need some value
that the Ruby trigger has if a different provisioner needs it, but a new Ruby variables' scope is still
limited to that Ruby block.

Thanks!
 
--
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/CA%2BonWPdK4sZddU_AeiL3F1jWk2%2Bhfgu%2BDY0JawZnnRvj8dzQoA%40mail.gmail.com.


--
Brian Cain

Jamie Jackson

unread,
Aug 21, 2019, 4:17:44 PM8/21/19
to vagra...@googlegroups.com
Thanks, Brian,

My migrations from the vagrant-triggers plugin to the native triggers implementation have been pretty rough, but thanks for the help so far.

I'm open to (ugly) workarounds since I have to stop spending cycles on this. Can anybody think of creative workarounds for doing this, whether it's gaining access to some global scope from the trigger or something else?

Thanks,
Jamie

Jamie Jackson

unread,
Aug 22, 2019, 9:23:13 AM8/22/19
to vagra...@googlegroups.com
For instance, I'm not above grabbing "env" or "machine" from:

trigger.ruby do |env, machine|

... and running instance_variable_set("@my_global_var", "my value") on one of them, but I don't know if that would work or how I'd refer to the variable outside of the trigger.

Thanks,
Jamie



Jamie Jackson

unread,
Aug 22, 2019, 1:26:59 PM8/22/19
to vagra...@googlegroups.com
I think I have the basis for a solution. It's hideous, and I would prefer better, but I think it might allow me to move on with my life. I write the variable to a file in the trigger and read it back in the provisioner.

Reply all
Reply to author
Forward
0 new messages