Check vagrant version in Vagrantfile

To access the used version of vagrant in a vagrant file you can use the (undocumented) Vagrant::VERSIONconstant. Combine this with Ruby's own Gem::Version and you can create a nice switch to handle deprecated configuration in the Vagrantfile.

Here is an example:

if Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.2')
  share_folder_options = {:create => true, :nfs => false, :mount_options => ['dmode=777','fmode=777']}
else
  share_folder_options = {:create => true, :nfs => false, :extra => 'dmode=777,fmode=777'}
end

One Comment

Respond to this post

Steffen Gebert wrote

Nice post, Christian!

In the upcoming version 1.4 of Vagrant, there will be even a function for that:

> The Vagrant.require_version function can be used at the top of a Vagrantfile to enforce a minimum/maximum Vagrant version.

https://github.com/mitchellh/vagrant/blob/master/CHANGELOG.md

But yes, you would lock out all users below 1.4, if you would use it once it's released ;-)

Steffen

2013-12-02 12:11