Skip to content

Instantly share code, notes, and snippets.

@bcantoni
Created September 15, 2014 22:20
Show Gist options
  • Save bcantoni/f9ca55cd4393ec4929a2 to your computer and use it in GitHub Desktop.
Save bcantoni/f9ca55cd4393ec4929a2 to your computer and use it in GitHub Desktop.
Example of passing a host environment variable through to a guest, using Vagrant script provisioner
# -*- mode: ruby -*-
# vi: set ft=ruby :
# test setting a guest environment variable based on a host environment variable
# if FOO_BAR is set locally, create command to add it to .profile in the guest
env_var_cmd = ""
if ENV['FOO_BAR']
value = ENV['FOO_BAR']
env_var_cmd = <<CMD
echo "export FOO_BAR=#{value}" | tee -a /home/vagrant/.profile
CMD
end
script = <<SCRIPT
#{env_var_cmd}
SCRIPT
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "hashicorp/precise64"
config.vm.hostname = "envtest"
config.vm.provision :shell, :inline => script
config.vm.provider "virtualbox" do |vb|
# vb.gui = true
vb.name = "envtest"
vb.customize ["modifyvm", :id, "--memory", "1000"]
end
end
@millerjp
Copy link

Very handy Gist!

@emmanuellyautomated
Copy link

👏 🎅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment