Skip to content

Instantly share code, notes, and snippets.

@DonRichards
Last active December 29, 2022 13:33
Show Gist options
  • Save DonRichards/8daab90879ec47d7b622ff0702e545d4 to your computer and use it in GitHub Desktop.
Save DonRichards/8daab90879ec47d7b622ff0702e545d4 to your computer and use it in GitHub Desktop.
Conditional Vagrant environments

Conditional Vagrant environments

2 options to start vagrant

command line with pass-through variable or alias

Run while passing variable to vagrantfile

$ ENV='local' vagrant up 

Add an alias

$ alias vagrant='ENV='\''local'\'' vagrant' 

Vagrantfile

if ENV['ENV'] != 'local' 
        config.vm.provision :shell, path: "./scripts/stage_env.sh", :args => shared_dir, :privileged => true
    else
        config.vm.provision :shell, path: "./scripts/development_env.sh", :args => shared_dir, :privileged => true
    end
@agray
Copy link

agray commented Nov 5, 2021

alias option flat out does not work. at least not on bash

@DonRichards
Copy link
Author

@agray Did you try the ENV='local' vagrant up command originally suggested by Vagrant?

@DonRichards
Copy link
Author

A couple of alternatives I found were (I didn't test these) if you'd prefer to drop it into the vagrantfile.

config.vm.provision "shell", inline: "echo " + ENV['ABC']
config.vm.provision :shell, path: "scripts/bootstrap.sh", env: {"MYVAR" => ENV['ABC']}

But I think I like the suggestion here the most "Updating .bashrc and environment variables during Vagrant provisioning" for adding more than 1 environment variable to vagrant.

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