尝试循环创建机器时,Vagrant堆栈级别太深(SystemStackError)

时间:2017-11-07 23:35:03

标签: ruby linux vagrant

所以我正在尝试创建vagrantfile,循环并为学校项目创建多台机器。

代码是:

def slave()
  slave{i}
end

Vagrant.configure(2) do |config|
  config.vm.box = "minimal/xenial64"
  config.vm.provision "shell", inline: $ttscript

  (1..3).each do |i|
    config.vm.define "slave{i}" do |slave|
      slave{i}.vm.hostname = "slave{i}"
  end
end

我无法将变量i放到奴隶现在的位置,所以我尝试为它创建定义,但它只是错误消息:

/home/mestari420/Vagrantfile:14:in `slave': stack level too deep (SystemStackError)
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
    from /home/mestari420/Vagrantfile:14:in `slave'
     ... 11894 levels...
    from /usr/share/vagrant/plugins/commands/up/command.rb:85:in `execute'
    from /usr/lib/ruby/vendor_ruby/vagrant/cli.rb:42:in `execute'
    from /usr/lib/ruby/vendor_ruby/vagrant/environment.rb:268:in `cli'
    from /usr/bin/vagrant:173:in `<main>'

我的方法有什么问题?

2 个答案:

答案 0 :(得分:1)

主要问题是

slave{i}.vm.hostname

只需替换为

slave.vm.hostname

因为slave是那里使用的变量的名称。

答案 1 :(得分:0)

(1..3).each do |i|
  config.vm.define "node-#{i}" do |node|
    node.vm.provision "shell",
      inline: "echo hello from node #{i}"
  end
end

来自https://www.vagrantup.com/docs/vagrantfile/tips.html

的示例