how to conditionally load a file for aws provider

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

how to conditionally load a file for aws provider

Mark Phillips
I'm writing a Vagrant file that I want to work for both the virtualbox and aws providers, and I want to keep my sensitive AWS credentials in a separate, non-version-controlled file.  The behavior that I want is that when I type "vagrant up --provider=aws", vagrant loads the credentials from the external file, or if the external file is not present, exits with an error message.  Also, when I type "vagrant up --provider=virtualbox" (or just "vagrant up", since virtualbox is the default), I'd like vagrant to spin up the VM regardless of whether the AWS credentials file is present, since it isn't needed in that case.

My Vagrant file looks something like the following:

require 'yaml'
Vagrant.configure("2") do |config|
  config.vm.provider :aws do |aws, override|
    if File.exist?("aws.yml")
      aws_config                   = YAML.load_file('aws.yml')["aws"]
      aws.instance_type            = aws_config["instance_type"]
      aws.access_key_id            = aws_config["access_key_id"]
      aws.secret_access_key        = aws_config["secret_access_key"]
      ...
    else
      puts "File aws.yml not present!"
      exit
    end
  end
  ...
end

What is happening, though, is that the code inside the "config.vm.provider :aws do |aws, override|" block is being executed regardless of which provider I invoke "vagrant up" with, so if the aws.yml file is missing, vagrant always exits with the message "File aws.yml not present!".  

Is there a way that I can write code in the Vagrantfile that tests which provider is requested, so that I can only execute a certain block of code for particular provider?

Thanks!

--Mark


--
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 [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.