Repeated lines in loop

greetings:
below is my vagrantfile:
the provision line [config.vm.provision “shell” , inline: $script] executes 12 time for each loop where it’s only suppose to execute once per iteration. What am I doing wrong.
As it is now each vm has a 12 duplicated ssh keys and 12 duplicated lines in sudoers.

thanks in advance

#!/usr/bin/ruby
# 4 nodes of ubuntu 22.04

Vagrant.configure("2") do |config|
  num_vms = 111
  ipa = "192.168.200"
  gw = "#{ipa}.1"
  d = ["192.168.200.1","8.8.8.8","8.8.4.4"]

  # add joe username and password
  # config.vm.provision "shell", inline: <<-SHELL
  $script = <<-SCRIPT
    useradd -m -s /bin/bash joe
    echo 'joe:joe' | chpasswd
    echo "joe ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
    sudo -u joe ssh-keygen -t rsa -N '' <<<$'\ny\n'
    echo "ssh-rsa AAAAB3NzaCdjhgAAAADAQABAAABAQDOxfz1v183Sbyce5ttc6TwS/DCcbMqQLIx1xMoZTnCWdtbnB/FQBBMohfjdjnsks9Gx18qeVtTE6am+Abz5sgZ5a6VrGxcr1lonfaCpV5JERPx83+F0AEXhC5XIwEjghf7rjedd/xsgyUt/0cCfMPKcHU+ffspjcD7o41vCe+tGOTCJNjLl8J+0E40xlK7f4j53WmOoYWUt+bqtZcddTpRwissmFRbckv5QKFvmEYxn2gbaStWxLGyLFVXsUYwbwkPHNNQQ5p6cmgjIHLXIPqKh5btjaGAK7ttfcvSG8PmrTzSYnvYecZVaK7f5i8AmszeSeUtHc51/m977^54fd iomari@iomari" >> /home/joe/.ssh/authorized_keys
  SCRIPT

  (100..num_vms).each do |i|
      config.vm.provider "virtualbox" do |vb|
        # 16G memory per box
        vb.memory = "16384"
        vb.cpus = 2
      end
    # run provision script

 config.vm.provision "shell" , inline: $script

config.vm.define "vm#{i}" do |vm|
        vm.vm.box = "bento/ubuntu-22.04"

        # use "private" below if you don't want the VM's to appear to be in same network as host
        vm.vm.network "public_network", ip: "#{ipa}.#{i}", gateway: "#{gw}", dns: "#{d}", bridge: "enp49s0f0"
        vm.vm.hostname = "vmnode#{i}"
    end
  end
end

Hi there,

The cause of the issue is due to where you are defining the provision. With the config.vm.provision call within the loop, it’s registering a new provisioner each time which is then applied to all the guests. Since the same provisioner is being applied to all guests, you can move it (as well as the provider config) out of the loop so that it looks like:

# add joe username and password
$script = <<-SCRIPT
  useradd -m -s /bin/bash joe
  echo 'joe:joe' | chpasswd
  echo "joe ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  sudo -u joe ssh-keygen -t rsa -N '' <<<$'\ny\n'
  echo "ssh-rsa AAAAB3NzaCdjhgAAAADAQABAAABAQDOxfz1v183Sbyce5ttc6TwS/DCcbMqQLIx1xMoZTnCWdtbnB/FQBBMohfjdjnsks9Gx18qeVtTE6am+Abz5sgZ5a6VrGxcr1lonfaCpV5JERPx83+F0AEXhC5XIwEjghf7rjedd/xsgyUt/0cCfMPKcHU+ffspjcD7o41vCe+tGOTCJNjLl8J+0E40xlK7f4j53WmOoYWUt+bqtZcddTpRwissmFRbckv5QKFvmEYxn2gbaStWxLGyLFVXsUYwbwkPHNNQQ5p6cmgjIHLXIPqKh5btjaGAK7ttfcvSG8PmrTzSYnvYecZVaK7f5i8AmszeSeUtHc51/m977^54fd iomari@iomari" >> /home/joe/.ssh/authorized_keys
SCRIPT
Vagrant.configure("2") do |config|
  num_vms = 111
  ipa = "192.168.200"
  gw = "#{ipa}.1"
  d = ["192.168.200.1","8.8.8.8","8.8.4.4"]

  # configure the provider for all guests
  config.vm.provider "virtualbox" do |vb|
    # 16G memory per box
    vb.memory = "16384"
    vb.cpus = 2
  end

  # run provision script
  config.vm.provision "shell" , inline: $script
  (100..num_vms).each do |i|
    config.vm.define "vm#{i}" do |vm|
      vm.vm.box = "bento/ubuntu-22.04"
      # use "private" below if you don't want the VM's to appear to be in same network as host
      vm.vm.network "public_network", ip: "#{ipa}.#{i}", gateway: "#{gw}", dns: "#{d}", bridge: "enp49s0f0"
      vm.vm.hostname = "vmnode#{i}"
    end
  end
end

Cheers!