Timosen Tommi
Timosen Tommi

Reputation: 23

Vagrant stack level too deep (SystemStackError) when trying to loop create machines

So I am trying to create vagrantfile that loops and creates multiple machines for a school project.

The code is:

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

end

I couldn't put the variable i to where the slave is now so I tried to create definition for it, but it just errors with message:

/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>'

What is wrong with my method of doing this?

Upvotes: 1

Views: 955

Answers (2)

Frederic Henri
Frederic Henri

Reputation: 53793

The main issue is

slave{i}.vm.hostname

Just replace with

slave.vm.hostname

as slave is the name of variable used there.

Upvotes: 1

Raj
Raj

Reputation: 22956

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

Example from https://www.vagrantup.com/docs/vagrantfile/tips.html

Upvotes: 0

Related Questions