Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant - how to have host platform specific provisioning steps

Tags:

ruby

vagrant

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.

like image 376
Steve Childs Avatar asked Nov 07 '14 22:11

Steve Childs


People also ask

How do I use vagrant provision?

Command: vagrant provision [vm-name]Runs any configured provisioners against the running Vagrant managed machine. This command is a great way to quickly test any provisioners, and is especially useful for incremental development of shell scripts, Chef cookbooks, or Puppet modules.

What is the provision that used in vagrant by default?

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy , unless the --provision flag is set, as noted above.

What is Provisioner in vagrant?

Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.


1 Answers

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 
like image 149
Tomas Creemers Avatar answered Oct 07 '22 17:10

Tomas Creemers