Vagrant - how to have host platform specific provisioning steps

Steve Childs picture Steve Childs · Nov 7, 2014 · Viewed 13.3k times · Source

We've got a diverse dev team, one on Windows, another on Ubuntu and another on OSX. Being windows boy, I setup the first version of the vagrant setup script which works fabulously ;)

However, when running it on the Ubuntu host, the first time it gets to a provision step that calls a bash script, it fails due to permissions.

On windows, this doesn't matter as the samba share automatically has sufficient permissions to run the bash script (which resides within the project hierarchy, so is present in the /vagrant share on the VM), but with ubuntu I need to set the permissions on this file in the provision script before I call it.

This isn't the problem and to be honest I suspect even with the extra "chmod" step it would still work fine under windows, but, is there a way in the vagrant file to flag certain provisioning steps as 'Windows Only', 'Linux Only' or 'Mac Only'?

i.e. in pseduo code, something like.

.
.
if (host == windows) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_windows.sh"
else if (host == linux) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_linux.sh"
else if (host == osx) then
  config.vm.provision : shell, : inline => "/vagrant/provisioning/only_run_this_on_osx.sh"
end if
.
.

Thanks in advance.

Answer

Tomas Creemers picture Tomas Creemers · Aug 24, 2016

Note that Vagrant itself, in the Vagrant::Util::Platform class already implements a more advanced version of the platform checking logic in the answer by BernardoSilva.

So in a Vagrantfile, you can simply use the following:

if Vagrant::Util::Platform.windows? then
    myHomeDir = ENV["USERPROFILE"]
else
    myHomeDir = "~"
end