yungchin
yungchin

Reputation: 1599

Can one reference the command-line selected provider from within a Vagrantfile

I want to be able to change behaviour of my puppet manifests, depending on where they run. At the moment, I'm using this hack to export the vagrant provider (via facter):

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

[... stuff ...]

config.vm.provision "puppet" do |pp|
    pp.manifests_path =  ".puppet"
    pp.facter = {"vagrant_provider" => ""}
end

config.vm.provider :rackspace do |rs, override|
    [... stuff ...]    
    override.vm.provision "puppet" do |pp|
        pp.manifests_path =  ".puppet"
        pp.facter =  {"vagrant_provider" => "rackspace"}
    end

[... more stuff ...]

But obviously that feels a bit dirty (and it gets uglier when adding more providers). Is there a way to write just this single block

config.vm.provision "puppet" do |pp|
    pp.facter = {"vagrant_provider" => Vagrant.selected_provider} ## pseudocode!
end

so that, when calling vagrant like so

$ vagrant up --provider=rackspace

we hand the right provider info to facter? I can't figure this out, either because I don't get ruby or I don't get vagrant, or probably both, so many thanks!

Upvotes: 2

Views: 87

Answers (1)

Matt Cooper
Matt Cooper

Reputation: 10860

My ruby is not the greatest so there may be a more efficient way to do this, but the following should work:

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # Get the provider!
  provider = ''
  ARGV.each do|a|
    if a.include?('--provider=')
      provider = a[11, a.length]
    end
  end

  config.vm.provision "puppet" do |pp|
    pp.facter = {"vagrant_provider" => provider}
  end

end

Upvotes: 1

Related Questions