在Vagrantfile中要求安装一个Vagrant插件?

85

执行一个 Vagrantfile 需要安装特定的 Vagrant 插件。因此,基本上你需要做的是

$ vagrant plugin install foobar-plugin
$ vagrant up

如果你跳过第一步,vagrant up 将失败。

Vagrant 中是否有选项可以自动安装插件?换句话说:是否可以在 Vagrantfile 中指定要在创建和启动虚拟机之前自动安装的插件?

12个回答

0

这是我在Vagrant 1.8上使用的内容,它可以正常工作。将此内容放置在Vagrantfile中的configure块之前。

# install required plugins if necessary
if ARGV[0] == 'up'
    # add required plugins here
    required_plugins = %w( plugin1 plugin2 plugin3 )
    missing_plugins = []
    required_plugins.each do |plugin|
        missing_plugins.push(plugin) unless Vagrant.has_plugin? plugin
    end

    if ! missing_plugins.empty?
        install_these = missing_plugins.join(' ')
        puts "Found missing plugins: #{install_these}.  Installing using sudo..."
        exec "sudo vagrant plugin install #{install_these}; vagrant up"
    end
end

似乎这不是跨平台的。 - thorr18

0

我在安装Vagrant时遇到了问题,.vagrant.d目录尚未创建。通过捕获异常,我能够使Luis St. Amour的代码片段正常工作。

required_plugins = %w(vagrant-share vagrant-vbguest)

begin
    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
rescue
    exec "vagrant #{ARGV.join(' ')}"
end

在Windows上,这会给我带来错误提示:“使用'virtualbox'提供程序将机器'default'启动...但是另一个进程已经在执行该机器的操作”,因为正在运行up。有没有办法避免这种情况而不必运行两次vagrant up - Tom B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接