I had a scenario which for some reason vagrant created one VM in virtualbox while it tried to create the other two in hyperv, weird enough I am unsure about the right way to either detect or force a specific provider for the whole Vagrantfile or a specific VM inside Vagrantfile.
(And yes, Hyper-v and Virtualbox can co-exist on their latest versions)
I have been trying to use vagrant in the labs of:
However these was created for virtualbox and will only work with virtualbox.
Since I am working mostly with hyper-v I wanted to modify the Vagrantfile to be able to work with both hyper-v and virtualbox.
Specifically this file:
I do not know how to properly detect the current provider being used at run time.
Also I assumed that adding node.vm.provider "hyperv"
Will automatically acs as a case for a “switch” and will only perform the name provider related actions.
For example hyperv uses vmname instead of the used name
I have tried using the next switch however I am not sure if it’s the right thing to do:
case ENV["VAGRANT_DEFAULT_PROVIDER"]
when "virtualbox"
node.vm.provider :virtualbox do |vbox|
vbox.memory = RAM
vbox.gui = GUI
vbox.name = "#{vm_name_prefix}#{item}"
end
when "hyperv"
node.vm.provider :hyperv do |vbox|
vbox.memory = RAM
vbox.vmname = "#{vm_name_prefix}#{item}"
end
end
You can just define the provider specific configuration directly:
Vagrant.configure(2) do |config|
config.vm.synced_folder "..", "/vagrant"
instances.each_with_index.map do |item, index|
config.vm.define "#{item}" do |node|
node.vm.box = BOX
node.vm.provider "virtualbox" do |vbox|
vbox.gui = GUI
vbox.memory = RAM
vbox.name = item
end
node.vm.provider :hyperv do |hv|
hv.memory = RAM
hv.vmname = "#{vm_name_prefix}#{item}"
end
node.vm.hostname = item + DOMAIN
node.vm.network 'private_network', ip: NETWORK + "#{index + 10}", netmask: NETMASK
node.vm.provision "shell", path: "provision/hosts.sh", args: [NETWORK, DOMAIN, 10]
node.vm.provision "shell", path: "provision/#{item}.sh"
node.vm.provision "shell", path: "provision/node_exporter.sh"
end
end
end
Now, regardless of this configuration, if you already have a guest created with the VirtualBox provider, it will continue to use that provider until it has been destroyed. If you destroy all the guests for the current project to start with a clean slate (vagrant destroy -f), then you can create new guests by either setting the VAGRANT_DEFAULT_PROVIDER environment variable to hyperv in your environment, or you can specify it when running the command:
@chrisroberts The issue with the hyper-v and virtualbox mixed situation was that I had no VM’s and it was after a vagrant destroy -f and a manual verification for a cleanup.
And forcing the VAGRANT_DEFAULT_PROVIDER still doesn’t answer how do I detect the current provider of the VM.
I might try later to run this lab again to make sure if I got it right.