Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass environment variables to vagrant shell provisioner

It looks like passing environment variables when calling vagrant up is simple if you're using a Ruby provisioner:

VAR=123 vagrant up 

In the Vagrantfile:

ENV['VAR'] 

How do I do this with the :shell provisioner? Simply doing this does not seem to work:

$VAR 
like image 799
Abdullah Jibaly Avatar asked Oct 29 '13 01:10

Abdullah Jibaly


People also ask

What is the Provisioner that used in Vagrant by default?

By default, VirtualBox is the default provider for Vagrant.

What is Vagrant Linux sh?

Vagrant is an open-source tool that allows you to create, configure, and manage boxes of virtual machines through an easy to use command interface. Essentially, it is a layer of software installed between a virtualization tool (such as VirtualBox, Docker, Hyper-V) and a VM.

What is Shell provisioning?

The shell Packer provisioner provisions machines built by Packer using shell scripts. Shell provisioning is the easiest way to get software installed and configured on a machine. Building Windows images? You probably want to use the PowerShell or Windows Shell provisioners.


1 Answers

Since Vagrant 1.8.0 you can forget the ugly hacks from the other answers here. Just use the env option for the shell provisioner (docs).

Use it like this in your Vagrantfile:

config.vm.provision "shell", path: "provisionscript.sh", env: {"MYVAR" => "value"} 

This will set the environment for the provisioning script only. If you need a persistent environment variable set for all processes in the VM, this is out of scope for Vagrant provisioning and look here: Shell environment variables in vagrant files are only passed on first up.

like image 108
gertvdijk Avatar answered Sep 23 '22 13:09

gertvdijk