Skip to content

Instantly share code, notes, and snippets.

@karlwilbur
Last active July 20, 2019 21:32
Show Gist options
  • Save karlwilbur/33f889cffd7052ddcd2f5bf55164ff59 to your computer and use it in GitHub Desktop.
Save karlwilbur/33f889cffd7052ddcd2f5bf55164ff59 to your computer and use it in GitHub Desktop.
Vagrant - Set Guest ENV vars
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANT_API_VERSION='2'
$set_environment_variables = <<SCRIPT
cat > '/etc/profile.d/envvars.sh' <<EOV
# AWS environment variables.
export SOME_VAR=things
export OTHER_VAR='other stuff'
EOV
SCRIPT
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm..box 'ubuntu/bionic64'
# This method utilizes the variable file constructed in the `SCRIPT` above
# which would be under source control as it is contained within this `Vagrantfile`
config.vm.define 'my-host-name' do |machine|
machine.vm.provision "shell", inline: $set_environment_variables, run: "always"
end
# This method utilizes a variable file which alreedy exists in the
# codebase (although possibly not under source control). The `/vagrant` directory
# on the guest machine maps to `.` on the host machine.
config.vm.define 'my-host-name' do |machine|
machine.vm.provision "shell",
inline: "echo 'source /vagrant/config/vagrant/bootstrap-env.sh' > /etc/profile.d/envvars.sh",
run: "always"
end
end
@karlwilbur
Copy link
Author

karlwilbur commented Jul 20, 2019

This takes advantage of the /etc/profile mechanism which runs any script in /etc/profile.d/; this applies to all users on the system including root.

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