Demand a Vagrant plugin within the Vagrantfile?
Supposed execution of a Vagrantfile
requires a specific Vagrant plugin to be installed. So, basically what you need to do is
$ vagrant plugin install foobar-plugin
$ vagrant up
If you skip the first step, vagrant up
fails.
Is there an option in Vagrant to make it install the plugin automatically? In other words: Is it possible to specify within a Vagrantfile
which plugins to install automatically before creating and booting up the machine?
UPDATE Aug 31, 2018: See @Starx's fix below for later versions of Vagrant (1.8 and above)
Here is version based on Louis St. Amour's solution together with Rob Kinyon's comment about re-exec if a new plugin was installeed, I use it successfully in my own setup:
required_plugins = %w(vagrant-share vagrant-vbguest...)
plugins_to_install = required_plugins.select { |plugin| not Vagrant.has_plugin? plugin }
if not plugins_to_install.empty?
puts "Installing plugins: #{plugins_to_install.join(' ')}"
if system "vagrant plugin install #{plugins_to_install.join(' ')}"
exec "vagrant #{ARGV.join(' ')}"
else
abort "Installation of one or more plugins has failed. Aborting."
end
end
Since I'm a Ruby dev, and Bindler is no longer being maintained, I found it most natural to just write some code at the top of my Vagrantfile to install required plugins if missing (e.g. before Vagrant.configure
line)
The following works for me:
required_plugins = %w( vagrant-hostmanager vagrant-someotherplugin )
required_plugins.each do |plugin|
system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
end
system
, unlike using backticks, will echo the command to stdout, just as running the command yourself would. And this way I don't need yet another strangely named plugin or system to keep track of required plugins which can be updated by Vagrant anyway.