Trouble With Provider Specific Provisioning Shell Scripts

Hello All,

I am having trouble with one of my machines getting shell scripts to run only when a specifici provider is selected. For example here is a simplified machine:

  config.vm.define "analyzer" do |analyzer|
    analyzer.vm.box = "centos/8"
    analyzer.vm.box_version = "1905.1"

    analyzer.vm.provider "virtualbox" do |v, override|
      v.memory = 2048
      v.cpus = 2
      v.customize ["modifyvm", :id, "--vram", "256"]
      v.customize ["modifyvm", :id, "--graphicscontroller", "vmsvga"]
      v.customize ["modifyvm", :id, "--accelerate3d", "on"]

      v.customize ["modifyvm", :id, "--nicpromisc2", "allow-all"]
    end

    analyzer.vm.synced_folder ".", "/vagrant", disabled: true

    analyzer.vm.network "private_network", ip: "192.168.70.20"

    analyzer.vm.provider "virtualbox" do |vb|
      analyzer.vm.provision "shell", inline: "echo Inside virtualbox Conditional"
    end

    analyzer.vm.provider "vmware_desktop" do |vmd|
      analyzer.vm.provision "shell", inline: "echo Inside vmware_desktop Conditional"
    end

    analyzer.vm.provider "vmware_esxi" do |vme|
      analyzer.vm.provision "shell", inline: "echo Inside vmware_esxi Conditional"
    end
  end

What I thought would happen was if I ran vagrant up using --provider=virtualbox, only the echo for the virtualbox would trigger. But that does not seem to be how it works. I am getting all of them triggering multiple times.

==> analyzer: Configuring and enabling network interfaces...
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside virtualbox Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_desktop Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_esxi Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside virtualbox Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_desktop Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_esxi Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside virtualbox Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_desktop Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_esxi Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside virtualbox Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_desktop Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_esxi Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside virtualbox Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_desktop Conditional
==> analyzer: Running provisioner: shell...
    analyzer: Running: inline script
    analyzer: Inside vmware_esxi Conditional

Am I doing something wrong? Any help would be appreciated.

Thank you.

Hey there,
Vagrant doesn’t really allow you to define provisioners for providers in this way. You could define provisioners for a particular machine. Something like:

Vagrant.configure("2") do |config|
  config.vm.define "analyzer-vbox" do |analyzer|
     analyzer.vm.box = "centos/8"
     analyzer.vm.box_version = "1905.1"
     analyzer.vm.provider "virtualbox" do |v, override|
      # whatever you have to do
     end 
    analyzer.vm.provision "shell", inline: "provision"
  end

  config.vm.define "analyzer-vmware" do |analyzer|
     analyzer.vm.box = "centos/8"
     analyzer.vm.box_version = "1905.1"
     analyzer.vm.provider "vmware" do |v, override|
      # whatever you have to do 
     end 
    analyzer.vm.provision "shell", inline: "provision"
  end
end

Another way of approaching this is to use typed triggers. Using an action type trigger you can run a command after an action has completed. For example:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"

  config.trigger.after :"VagrantPlugins::ProviderVirtualBox::Action::Boot", type: :action do |t|
    t.info = "After after virtualbox boot!"
    t.run = { inline: "echo yay" } 
  end

  config.trigger.after :"HashiCorp::VagrantVMwareDesktop::Action::Boot", type: :action do |t|
    t.info = "After after vmware up!"
    t.run = { inline: "echo yay" } 
  end
1 Like