Add different values (iteration) in cloud-init template by current instance


I’m using the template_cloud_init_config as a data source in order to provision several virtual machines with cloud-init configuration. And I’m using part blocks so that I can separate the user-data cloud-init config from the additional bash scripts that the virtual machines will run afterwards.

I’m trying to use a different value in the cloud-init template for each virtual machine. The issue I’m having is that I cannot use count.index in the part blocks (within template_cloud_init_config).
This is my config:

data "template_cloudinit_config" "work" {
        # split in parts - 1st is cloud-init cfg as such; from 2nd onwards, shell scripts.
        # default gzip is true + base64 encoded (for proxmox don't encode or zip the cloud-init)
        gzip = false
        base64_encode = false
        part {
                filename = "cloud-init.cfg"
                content_type = "text/cloud-config"
                content = templatefile("${path.module}/files/cloud_init_ub2004.tpl", {
                        root_ssh_key = file("${path.module}/files/")
                        ejobs-cert = file("${path.module}/files/comp-cert.yaml")
                        haproxy-deploy = file("${path.module}/files/haproxy-deploy.yaml")
                        haproxy-ingress = file("${path.module}/files/haproxy-ingress.yaml")
                        dockerconfigjson = file("${path.module}/files/dockerconfigjson")
                        instance_ip = var.ip[count.index + 1]
        part {
                filename = ""
                content_type = "text/x-shellscript"
                content = templatefile("${path.module}/files/", {
                        cidr = local.cidr

With this config I get:

│ Error: Reference to "count" in non-counted context
│   on line 49, in data "template_cloudinit_config" "work":
│   49: 			instance_ip = var.ip[count.index + 1]
│ The "count" object can only be used in "module", "resource", and "data" blocks, and only when the
│ "count" argument is set.

the ip variable is a list with IPs.
Any ideas how I could get around this issue?

Hi @lethargosapatheia,

You can use the same count argument on your data "template_cloudinit_config" "work" block in order to generate multiple instances of the template:

data "template_cloudinit_config" "work" {
  count = length(var.ip) # or whatever you have in the virtual machine resource

When you do this, will itself be a list of objects representing the different results of this data resource, and so you can refer to them using count.index in the virtual machine:[count.index].rendered
1 Like

Thank you. Only now did I read your answer. I was able to understand eventually that I needed to define count there too. I thought you could call it without definining it previously as a sort of global variable. I was simply ignoring the second part of the error, which was explicit enough.
Reading your answer faster would have saved me some trouble :slight_smile: Unfortunately I’m not getting any notifications per e-mails, I’m not sure why, although I did activate them.