Setting environment variables for multiple providers

206 views
Skip to first unread message

Scott Mebberson

unread,
Sep 1, 2014, 5:12:08 AM9/1/14
to vagra...@googlegroups.com
Hi all,

I'm new to Vagrant, but I'm really like it so far. I've transition too projects and it's working great locally.

I have a project that I'm working on, I have two environments:

- development
- production

Development is on my local machine (now in Vagrant), and production is on dotcloud (which I'll be moving away from eventually).

I want to introduce a beta environment, so that I can development and release to beta testers a new version of my product. I also want a support environment, so my support team can one-way synchronise the live database, and then play with certain things as required. I want to launch these two new environments in AWS. I've read up about the multi provider support which all makes sense (by appending --provider=aws for example).

I'm using the shell provider for my setup.

In my current Vagrantfile, I run a bash script to set some environment variables. These environment variables are for my development machine. What is the best approach to have a different set of environment variables per environment?

I know I can achieve this based on the provider. But I want to have two different environments hit the same provider... beta on AWS and support on AWS. In fact, now that I write that, I'm not even sure how I'd achieve that...

Are there any best practices for this?

cheers,
Scott.

Alvaro Miranda Aguilera

unread,
Sep 1, 2014, 5:27:26 AM9/1/14
to vagra...@googlegroups.com
Vagrantfile is processed in ruby, so you can use if, unless, etc

if you set a variable outside the vagrantifle, say

TARGET=beta

then you could do

if ENV['TARGET']="beta" then
....
end

will something like this work?

can you explain in pseudo code what you will like to have?





--
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Scott Mebberson

unread,
Sep 1, 2014, 5:48:15 PM9/1/14
to vagra...@googlegroups.com
Hi,

Thanks for getting back to me, that seems to make some sense. This is my vagrant file so far...

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    # Every Vagrant virtual environment requires a box to build off of.
    config.vm.box = "phusion/ubuntu-14.04-amd64"

    # Create a forwarded port mapping which allows access to a specific port
    # within the machine from a port on the host machine. In the example below,
    # accessing "localhost:8080" will access port 80 on the guest machine.
    config.vm.network "forwarded_port", guest: 80, host: 3500

    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    config.vm.network "private_network", ip: "192.168.10.10"

    # If true, then any SSH connections made will enable agent forwarding.
    # Default value: false
    config.ssh.forward_agent = true

    # configuration step 0: provision setup
    config.vm.provision "shell", path: "vagrant/provisioner.sh"

    # configuration step 1: apt-get
    config.vm.provision "shell", path: "vagrant/apt-get.sh"

    # configuration step 2: git
    config.vm.provision "shell", path: "vagrant/git.sh"

    # configuration step 3: nodejs
    config.vm.provision "shell", path: "vagrant/nodejs.sh"

    # configuration step 4: mongodb
    config.vm.provision "shell", path: "vagrant/mongodb.sh"

    # configuration step 5: redis
    config.vm.provision "shell", path: "vagrant/redis.sh"

    # configuration step 6: nginx
    config.vm.provision "shell", path: "vagrant/nginx.sh"

    # configuration step 7. env
    config.vm.provision "shell", path: "vagrant/env.sh"

    # configuration step 8. install
    config.vm.provision "shell", path: "vagrant/install.sh"
  
end

See step 7 is the point at which I'd like to customise it. In step 7, I actually run a nodejs bash script (because I'm more comfortable with that, and by the time step 7 runs it's been installed) which loads a JSON file containing environment variables.

At this point, I'd want to load a different set of environment variables per environment, for configuration of the application. For example, I'd want to define a different URL for my application to run on, a different database to connect to (for the various environments, one per environment), a different redis cache URL, etc. I think I can actually understand how to do this now using environment variables, something like:

    # configuration step 7. env
    config.vm.provision :shell, :path => "vagrant/env.sh", :args => ENV['TARGET']

Then in my shell script, I can use the value of ENV['TARGET'] to load in other files like env.development.json, or env.staging.json, env.beta.json or whatever.

That solves one problem, thank you! But now I'm a little confused about how you might target different environments on the same provider. It seems as though you can only have one instance per provider (unless you do a multiple machine setup, but I wouldn't always want my code to be the same per provider), because you run vagrant up --provider=aws? How would I vagrant up my staging env on aws, or my beta env on aws?

I hope my question makes sense?

cheers,
Scott.

Alvaro Miranda Aguilera

unread,
Sep 1, 2014, 6:49:05 PM9/1/14
to vagra...@googlegroups.com
well, you can do, 2 variables for aws or do 2 awss

TARGET=virtualbox
TARGET=aws
TARGET=awsbeta

?


here between line 184 and 200 i do some tasks per machine being provisioned, you may do something similar for variable/provisioner.

https://github.com/racattack/vagrantfile/blob/master/OracleLinux/racattack12cR1/Vagrantfile





--

Scott Mebberson

unread,
Sep 3, 2014, 3:23:12 AM9/3/14
to vagra...@googlegroups.com
Hey there,

Thanks for getting back to me.

I guess I'm confused about the command: vagrant up. I know you can use --provider=aws or --provider=vmware_fusion to target specific providers. But how do I have multiple machines for the same provider and (vagrant up them at different times?), and not include these machines when I vagrant up for another provider? Do you have to use a multi-machine Vagrantfile?

For example, I want to:
  • vagrant up --provider=vmware_fusion, on my local machine to fire up one server for development
  • then at a different time, I'd like to ENV['beta'] vagrant up --provider=aws, on my local machine to fire up the beta version on AWS
  • then at another time again, I'd like to ENV['support'] vagrant up --provider=aws, on my local machine to fire up the latest version of the code on AWS for my support environment
Is this all possible and I'm just getting confused?

cheers,
Scott.

Alvaro Miranda Aguilera

unread,
Sep 3, 2014, 4:26:38 AM9/3/14
to vagra...@googlegroups.com
the vagrantfile is parsed and is basically ruby code

so what I would do is this, in seudo code.

if variable beta defined, then script beta.sh, otherwise normal.sh

and that will setup beta, either local or aws

and code yould be something like this:


if ENV[beta] 
  script="beta.sh"
else
  script="normal.sh"
end

then later on the provisioner shell script, you use script instead of "normal.sh" or "beta.sh"

make sense?

if not, send if you can the vagrantfile you have to have a look.



--
Reply all
Reply to author
Forward
0 new messages