Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create linux environment variable using vagrant provisioner

Tags:

vagrant

I've got vagrant running Ubuntu for development purposes. I've used a shell script provisioner to download/install my dependencies and create some aliases, but I've hit a wall in terms of using the provisioner to create environment variables (which are used for several flags within my project). Originally I had something like:

export MY_VAR='value' 

Into my provisioner script, but then found out that you can't add environment variables from inside a shell script by running it normally. Fair enough, so I tried instead changing my line of the Vagrantfile to:

config.vm.provision "shell", inline: “source setup.sh" 

Which didn't solve the problem. Environment variables still weren't there. I tried adding the exports directly as an inline:

config.vm.provision "shell", inline: “export MY_VAR='value'" 

No luck. Still no global environment when I ssh'ed in. Is there a way to use the shell script to set a bash environment variable, or is it time to throw in the towel on shell provisioners and learn chef?

like image 570
MBrizzle Avatar asked Jul 11 '14 23:07

MBrizzle


People also ask

Can be used as Provisioner with vagrant?

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.

What is the Provisioner that used in vagrant by default?

Every Vagrant provisioner accepts a few base options. The only required option is what type a provisioner is: name (string) - The name of the provisioner.


2 Answers

You should have the provisioning script add a line to your .profile:

echo "export VAR=value" >> ~/.profile 

On login, the .profile script will be read by bash and the variable will be set.

like image 156
rje Avatar answered Sep 17 '22 05:09

rje


Seeing as the accepted answer does indeed add an export VAR=value to your .profile (or .bashrc) file each time you run vagrant provision, here's how I've added environment variables quickly

source ~/.profile && [ -z "$VAR" ] && echo "export VAR=value" >> ~/.profile 

Breakdown:

  • source ~/.profile: load the current .profile file
  • [ -z "$VAR"]: check whether or not VAR is set, if not:
  • echo "export VAR=value" >> ~/.profile: add the export line to .profile

Putting it all together:

I normally use puphpet for my vagrant boxes, so I tend to stick to the directory structure it uses, which means putting my provisioning script in puphpet/shell/* (relative to the Vagrantfile file). In that file, you can add as many environment variables as you like:

#!/usr/bin/env bash #Replace .profile with .bashrc if required source ~/.profile if [ -z "$VAR" ]; then # only checks if VAR is set, regardless of its value     echo "export VAR=value" >> ~/.profile fi #other env variables and profile stuff here 

If you want to set the environment variables to a specific value, even if they're set, you can replace [ -z "$VAR" ] with this (As Maks3w suggested):

if [ -z "$VAR" ] || [ "$VAR" != "value" ]; then     #same as before fi 

Then simply add this to your Vagrantfile:

config.vm.provision "shell", path: "puphpet/shell/your-script.sh" 

That ought to do the trick...

like image 31
Elias Van Ootegem Avatar answered Sep 20 '22 05:09

Elias Van Ootegem