I can't seem to set a variable from a trigger which would be used by a provisioner. Here's a really ugly hack that seems to work.
Created
August 22, 2019 17:24
-
-
Save jamiejackson/4ec92bef2e148d58d207b15f2c92b066 to your computer and use it in GitHub Desktop.
Hack: Set a variable from a trigger in Vagrant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- mode: ruby -*- | |
| # vi: set ft=ruby : | |
| require 'pp' | |
| Vagrant.configure("2") do |config| | |
| config.vm.box = "ubuntu/bionic64" | |
| vagrant_dir=File.dirname(__FILE__) | |
| host_temp_dir = "#{vagrant_dir}/temp" | |
| puts "create temp directory, where var files will be stored" | |
| Dir.mkdir(host_temp_dir) unless File.exists?(host_temp_dir) | |
| puts "create a placeholder var file" | |
| File.open("#{host_temp_dir}/local_dump_path_name.var.txt", "w") { | |
| |file| file.puts "default value" | |
| } | |
| config.trigger.before [:up, :provision] do |trigger| | |
| trigger.ruby do |env, machine| | |
| puts "executing trigger and rewriting value of var file" | |
| File.open("#{host_temp_dir}/local_dump_path_name.var.txt", "w") { | |
| |file| file.puts "value set in trigger" | |
| } | |
| end | |
| end | |
| # doesn't work. gets default value | |
| config.vm.provision "read_var_from_trigger_on_host", | |
| type: "shell", | |
| privileged: false, | |
| inline: "echo '#{File.read("#{host_temp_dir}/local_dump_path_name.var.txt")}'" | |
| # works. gets "value set in trigger" | |
| config.vm.provision "read_var_from_trigger_on_vm", | |
| type: "shell", | |
| privileged: false, | |
| inline: "echo \"$(head -n 1 /vagrant/temp/local_dump_path_name.var.txt)\"" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ vagrant up --provision | |
| create temp directory, where var files will be stored | |
| create a placeholder var file | |
| Bringing machine 'default' up with 'virtualbox' provider... | |
| ==> default: Running action triggers before up ... | |
| ==> default: Running trigger... | |
| executing trigger and rewriting value of var file | |
| ==> default: Checking if box 'ubuntu/bionic64' version '20190705.0.0' is up to date... | |
| ==> default: Running provisioner: read_var_from_trigger_on_host (shell)... | |
| default: Running: inline script | |
| default: default value | |
| ==> default: Running provisioner: read_var_from_trigger_on_vm (shell)... | |
| default: Running: inline script | |
| default: value set in trigger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment