Vagrant 配置错误 - “必须指定一个框。”

Vagrant Config Error - “A box must be specified.”

我的配置如下,盒子工作正常:


# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

  (1..4).each do |i|

    config.vm.define "node#{i}", autostart:true do |node|

        config.vm.box = "ubuntu/trusty64"
        config.vm.hostname="node#{i}"
        config.vm.network "private_network", ip: "192.168.59.#{i}"
        config.vm.synced_folder "~/Documents/csunp/unp", "/vagrant/unp"
        config.vm.provider "virtualbox" do |v|
            v.name = "node#{i}"
            v.memory = 512
            v.cpus = 1
        end
    end
  end
end

但是一旦我关闭电脑,我就不能再回去了。 运行 vagrant up,我得到以下错误:

Bringing machine 'node1' up with 'virtualbox' provider...
Bringing machine 'node2' up with 'virtualbox' provider...
Bringing machine 'node3' up with 'virtualbox' provider...
Bringing machine 'node4' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* A box must be specified.

这有什么问题吗?提前致谢。

嗯,你的 Vagrantfile 写得不是很好,你创建了一个循环来创建 4 个带有节点变量的实例,但仍然使用 config.vm

如果你想保持简单,改为

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

  (1..4).each do |i|

    config.vm.define "node#{i}", autostart:true do |node|

        node.vm.box = "ubuntu/trusty64"
        node.vm.hostname="node#{i}"
        node.vm.network "private_network", ip: "192.168.59.#{i}"
        node.vm.synced_folder "~/Documents/csunp/unp", "/vagrant/unp"
        node.vm.provider "virtualbox" do |v|
            v.name = "node#{i}"
            v.memory = 512
            v.cpus = 1
        end
    end
  end
end

如果您对所有 4 个虚拟机使用同一个盒子,您可以写成

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/trusty64"
  config.vm.synced_folder "~/Documents/csunp/unp", "/vagrant/unp"

  (1..4).each do |i|

    config.vm.define "node#{i}", autostart:true do |node|

        node.vm.hostname="node#{i}"
        node.vm.network "private_network", ip: "192.168.59.#{i}"
        node.vm.provider "virtualbox" do |v|
            v.name = "node#{i}"
            v.memory = 512
            v.cpus = 1
        end
    end
  end
end