Independent provider and provisioning in same Vagrantfile

18 views
Skip to first unread message

joao.m...@omibee.com

unread,
Oct 19, 2017, 6:30:22 AM10/19/17
to Vagrant
I have a specific use case for Vagrant with which I'm struggling a bit.

We need to have on the same Vagrantfile a way to roll a local development VM and/or an AWS instance. For this I thought of using 2 different providers, a virtualbox provider and the vagrant-aws provider plugin.

If I have only these providers set up in the Vagrantfile, it works as expected. However, if I

vagrant up --provider=virtualbox

the provision block I have setup in the Virtualbox provider block runs twice. I have tried isolating each provider's configuration and provisioning inside if-then blocks that look at the ARGV array, but Vagrant seems to ignore this.

My final goal is to have at a project level, a way that allows my developer colleagues to vagrant up locally so that they can work on the project, but, at a different time, different host and by a different process, be able to vagrant up --provider=aws to roll a staging environment for the same project. Please note that I need different provisioning for either the local VM with Virtualbox or the staging instance on AWS. I also don't want the provisioning of each provider mixed up or both providers being setup at the same time.

Does anyone have any insight on how to do this? Vagrant's documentation on Hashicorp's website is a bit misleading as I thought I could isolate different procedures in the same Vagrantfile, but I'm not sure if I'm doing it wrong or if it's not possible at all.

My Vagrantfile looks something like this at the moment:


Vagrant.configure(2) do | config |

 
# doesn't work:
 
# provider_is_aws  = (!ARGV.nil? && ARGV.join('').include?('provider=aws'))


 
# unless provider_is_aws # doesn't isolate the configuration

  config
.vm.provider :virtualbox do |v|
   
# several config.vm options
   
# ...
    config
.vm.provision "ansible" do |ansible|
      ansible
.playbook = "local_playbook.yml"
   
end
 
end
 
# end # unless block

 
# if provider_is_aws # doesn't isolate the configuration
  config
.vm.provider :aws do |aws, override|
   
# several aws. options
   
# ...
   
override.vm.provision "Ansible_Local", type: "ansible_local" do |ansible|
      ansible
.playbook = "aws_playbook.yml"
   
end
 
end
 
# end # if block


end


If I vagrant up --provider=virtualbox the local_playbook.yml provisioner runs twice (!). This makes absolutely no sense to me. 

joao.m...@omibee.com

unread,
Oct 19, 2017, 6:34:25 AM10/19/17
to Vagrant
Found a typo: where it reads "If I have only these providers set up" it should be "If I have only one these providers set up"

Alvaro Miranda Aguilera

unread,
Oct 19, 2017, 7:22:02 AM10/19/17
to vagra...@googlegroups.com
Can you try like this:



Vagrant.configure(2) do | config |

  
# doesn't work:
  
# provider_is_aws  = (!ARGV.nil? && ARGV.join('').include?('provider=aws'))


  
# unless provider_is_aws # doesn't isolate the configuration

  config
.vm.provider :virtualbox do |v|
    
# several config.vm options
    
# ...

    v
.vm.provision "ansible" do |ansible|

      ansible
.playbook = "local_playbook.yml"
    
end
  
end
  
# end # unless block

  
# if provider_is_aws # doesn't isolate the configuration
  config
.vm.provider :aws do |aws, override|
    
# several aws. options
    
# ...
    
override.vm.provision "Ansible_Local", type: "ansible_local" do |ansible|
      ansible
.playbook = "aws_playbook.yml"
    
end
  
end
  
# end # if block


end

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/66c66570-f25a-4e58-aa21-72f052050ed5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

joao.m...@omibee.com

unread,
Oct 20, 2017, 4:21:14 AM10/20/17
to Vagrant
Hello Alvaro!

Thanks for your reply. I ended up rethinking my process and decided to keep two distinct Vagrantfiles for each purpose. I'll try your approach when I get the chance and post some feedback here.

Again, thank you for your help.

Cheers!
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages