执行一个 Vagrantfile
需要安装特定的 Vagrant 插件。因此,基本上你需要做的是
$ vagrant plugin install foobar-plugin
$ vagrant up
如果你跳过第一步,vagrant up
将失败。
Vagrant 中是否有选项可以自动安装插件?换句话说:是否可以在 Vagrantfile
中指定要在创建和启动虚拟机之前自动安装的插件?
执行一个 Vagrantfile
需要安装特定的 Vagrant 插件。因此,基本上你需要做的是
$ vagrant plugin install foobar-plugin
$ vagrant up
如果你跳过第一步,vagrant up
将失败。
Vagrant 中是否有选项可以自动安装插件?换句话说:是否可以在 Vagrantfile
中指定要在创建和启动虚拟机之前自动安装的插件?
更新于2018年8月31日:有关Vagrant的更新版本(1.8及以上),请参见@Starx下面的修复
这里是基于路易斯·圣·阿摩尔(Louis St. Amour)的解决方案和罗布·金扬(Rob Kinyon)的评论,如果安装了新插件,则重新执行它。我在自己的设置中成功使用它:
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
由于我是Ruby开发者,而Bindler不再维护,所以我觉得最自然的做法就是在我的Vagrantfile顶部编写一些代码来安装所需的插件(例如,在Vagrant.configure
行之前)。
以下方法适用于我:
required_plugins = %w( vagrant-hostmanager vagrant-someotherplugin )
required_plugins.each do |plugin|
system "vagrant plugin install #{plugin}" unless Vagrant.has_plugin? plugin
end
system
与使用反引号不同,它将命令回显到标准输出(stdout),就像你自己运行命令一样。这种方式不需要再使用另一个奇怪的插件或系统来跟踪所需的插件,因为 Vagrant 可以更新它们。
Vagrant.has_plugin?
命令或者只需通过vagrant plugin install
检查插件是否已存在,这将非常棒。 - sakoviasif [ $(vagrant plugin list | egrep 'vagrant-hostsupdater|vagrant-share' -c) == 2 ] ; then echo "All plugins installed." ; else echo "Missing plugin"; fi
,但是如果有其他选择的话,没有人会写bash脚本的。也许可以进一步尝试vagrant插件命令? - Louis St-Amour正如我在回答你另一个问题时 指出的那样, 你可以使用bindler通过一个命令安装一组特定于项目的插件。
如果已经安装了bindler但所需的插件未安装,bindler将报错并中止进程。此外,还有一个相关的问题涉及在vagrant up
时自动安装插件,但迄今为止还没有人签名参与。
如果您不想使用bindler,您可以在Vagrantfile
的顶部使用Vagrant.has_plugin?
(1.3.0+可用)并在所需的插件未安装时报错。
像这样:
unless Vagrant.has_plugin?("vagrant-some-plugin")
raise 'some-plugin is not installed!'
end
Vagrant.configure("2") do |config|
config.vm.box = "box-name"
end
更新:Bindler已不再支持,截至2015年5月11日,Vagrant核心也没有提供相应的功能。
Gemfile
中指定依赖项,并将它们放在一个 :plugins
组下,Vagrant 将会检查这个组中的内容。请看下面的答案获取更多细节。 - Jonathan BenderGemfile
旨在用于 Vagrant 插件开发。请参阅 https://github.com/mitchellh/vagrant/issues/8370 - mixel2019年更新:Vagrant现在可以通过 Vagrantfile
要求插件功能:
Vagrant.configure("2") do |config|
config.vagrant.plugins = "vagrant-some-plugin"
# or as array:
config.vagrant.plugins = ["vagrant-some-plugin", "vagrant-some-other-plugin"]
# or as hash
config.vagrant.plugins = {"vagrant-some-plugin" => {"version" => "1.0.0"}}
end
如果Vagrant检测到有未安装的插件,它会提示用户自行安装:
$ vagrant up
Vagrant has detected project local plugins configured for this
project which are not installed.
vagrant-some-plugin
Install local plugins (Y/N) [N]: y
Installing the 'vagrant-some-plugin' plugin. This can take a few minutes...
Fetching vagrant-some-plugin-1.0.0.gem
Installed the plugin 'vagrant-some-plugin (1.0.0)'!
Vagrant has completed installing local plugins for the current Vagrant
project directory. Please run the requested command again.
请查看https://www.vagrantup.com/docs/vagrantfile/vagrant_settings.html
Gemfile
中指定依赖项。根据更新的博客文章:
Now, Vagrant 1.5 will automatically load any gems in the "plugins" group in your Gemfile. As an example, here is the Gemfile for a "vagrant-bar" plugin:
source "https://rubygems.org" group :development do gem "vagrant", git: "https://github.com/mitchellh/vagrant.git" end group :plugins do gem "vagrant-foo", gem "vagrant-bar", path: "." end
Gemfile
文件,所以你能解释一下如何设置它吗?我创建了一个类似于你示例的文件,但 vagrant up
并没有自动加载任何内容。尝试过 bundle install
但会提示系统Rubygems权限的问题,这听起来不太对。 - Don McCurdygem install bundler
安装了bundle
命令,您应该能够使用bundle init
在当前目录中创建一个(相对空白的)Gemfile。这些gems可以是git repos或rubygems,甚至是git repos的分支或标记为特定版本/提交,并使用bundle install
为该特定文件夹或应用程序的gem要求进行安装。我不知道它们如何与Vagrant一起使用,只是想分享一些背景信息。 - Louis St-Amour无法对Louis St-Amour的答案添加评论,但我想发布这篇文章以帮助那些需要扩展他的解决方案的人。
# Check for missing plugins
required_plugins = %w(vagrant-list)
plugin_installed = false
required_plugins.each do |plugin|
unless Vagrant.has_plugin?(plugin)
system "vagrant plugin install #{plugin}"
plugin_installed = true
end
end
# If new plugins installed, restart Vagrant process
if plugin_installed === true
exec "vagrant #{ARGV.join' '}"
end
vagrant-list
只是一个插件示例,而不是代码的必要部分。您可以在此处查看其他Vagrant插件:http://vagrant-lists.github.io/ - Nathan Basanesevagrant plugin install
时都会处理Vagrantfile
,并在处理时再次执行与安装插件相关的代码,如此反复进行。
这里是我的解决方案,避免了循环。# Plugins
#
# Check if the first argument to the vagrant
# command is plugin or not to avoid the loop
if ARGV[0] != 'plugin'
# Define the plugins in an array format
required_plugins = [
'vagrant-vbguest', 'vagrant-hostmanager',
'vagrant-disksize'
]
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
end
Vagrantfile
顶部为每个插件添加一行,例如:vagrant-vbguest
。 raise "vagrant-vbguest plugin must be installed" unless Vagrant.has_plugin? "vagrant-vbguest"
您可以使用这个项目(我是作者):https://github.com/DevNIX/Vagrant-dependency-manager
它基于类似的答案,但试图更完整和稳定。这个想法的巨大优势是,您可以分发您的Vagrantfile,并且它将在每台计算机上运行,而不管该环境中安装的插件。
它很容易使用:
Include it and call check_plugins
passing your dependencies as an array
Example:
# -*- mode: ruby -*-
# vi: set ft=ruby :
require File.dirname(__FILE__)+"./dependency_manager"
check_plugins ["vagrant-exec", "vagrant-hostsupdater", "vagrant-cachier", "vagrant-triggers"]
Vagrant.configure(2) do |config|
config.vm.box = "base"
end
???
Profit!
我刚注意到这里 http://docs.vagrantup.com/v2/plugins/packaging.html 有一条指示
Vagrant.require_plugin "vagrant-aws"
这个功能与fgrehm所描述的完全一致:如果插件未安装,则快速引发错误。
据我所知,仍然没有自动安装插件的方法。
Vagrant.require_plugin
已经被弃用。已经在 Vagrant 1.7.4
中进行了检查。 - czerasz
git clone...; cd ...; vagrant up
即可)。 - Capt. Crunch