-1

is it possible so do loops in packer ? I have a Vagrantfile with a loop and want to convert it to packer. I already searched on google and stack overflow but didn't find the right solution.

I have problems converting this into a json format: (0..$NODE_COUNT).each do |I|

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

    $BOX_IMAGE = "VAGRANT CLOUD IMAGE NAME"
    $TBSP_VERSION = "2.0"

    # number of additional tendermint validation nodes to be provisioned
    $NODE_COUNT = 1

    Vagrant.configure("2") do |config|
      (0..$NODE_COUNT).each do |i|
        config.vm.define "node#{i}" do |subconfig|
          subconfig.vm.box = $BOX_IMAGE
          subconfig.vm.hostname  = "node#{i}"
          subconfig.vm.network "private_network", ip: "192.168.50.#{i + 10}"
          subconfig.disksize.size = '10GB'
          subconfig.vm.provider "virtualbox" do |v|
            v.memory = 4096
            v.cpus = 4
            v.customize ["modifyvm", :id, "--name", "v2.0-abci-tm-d-multi-node#{i}-server"]
          end
          subconfig.trigger.after :up do |trigger|
            if(i == $NODE_COUNT)
              trigger.info = "last Node-#{$NODE_COUNT} is up"
              trigger.run = {path: "provision_tendermint.sh", :args => "'#{$NODE_COUNT}'"}
            end
          end
          subconfig.vm.provision "shell", path: "provision.sh", :args => "'#{i}' '#{$NODE_COUNT}'"
        end
      end
    end

1 Answers1

0

No, Packer JSON doesn't currently support doing any kind of looping. You will have to approach this in a different manner. I think is you are just using Vagrant to provision VMs and not create an Image, then you should consider using Terraform instead.

Jamie
  • 3,094
  • 1
  • 18
  • 28