- Invalid template interpolation value: Cannot include the given value in a string template: string required

Dear Team,

I have faced this error while trying to create null_resource using output ip address from module.par1

The question is what is the correct format to get the host ip address from the module.par1?

Terraform plan and terraform validate works perfect without errors.

output "par1-name" {
  value = module.par1.name
}

output "par-1-public_ip" {
  value = module.par1.public_ip
}

output results:
terraform output

par-1-public_ip = [
  "163.172.x.x",
  "51.15.x.x",
  "163.x.x.111",
]
par1-name = [
  "YB-DB-1-par1",
  "YB-DB-2-par1",
  "YB-DB-3-par1",
]

Here is Null_resource with the problem.

resource "null_resource" "set_hosts_file" {
  depends_on=[module.par1]
  count = module.par1.node_count

  provisioner "remote-exec" {
      inline = [<<EOT
        %{ for ip in module.par1.*.private_ip ~}
          echo ${ip} >> /root/ips.txt
        %{ endfor ~}
        %{ for name in module.par1.*.name ~}
          echo ${name} >> /root/names.txt
        %{ endfor ~}
        paste /root/ips.txt /root/names.txt > /root/hosts.final
       EOT
      ]
    connection {
      type     = "ssh"
      user     = "root"
      private_key = file("~/.ssh/id_rsa")
      host = element(module.par1.*.public_ip, count.index)
    }
  }
}

thank you in advance for your support.

Vasilios Tzanoudakis

Hi @vtzan!

Could you please share the full error message, including the source code snippet it includes, and indicate which specific part of it was highlighted by Terraform as being the subject of the error?

Thanks!

Dear @apparentlymart,

Thank you for your reply.
I was just replying with what I did to fix this issue to help other community members.

Inside child module I have output:

output "public_ip" {
    value = scaleway_instance_server.db_nodes.*.public_ip
}

and outside of the module I have a null_resource with the following.

resource “null_resource” “set_global_hostnames” {
count = local.total-nodes

provisioner "remote-exec" {
      inline = [<<EOT
       ..........
      EOT
      ]

    connection {
      type     = "ssh"
      user     = "root"
      private_key = file("~/.ssh/id_rsa")
      host = element(concat(module.par1.public_ip), count.index)

    }
  }

  provisioner "remote-exec" {
      inline = [
          "echo test",
      ]
    connection {
      type     = "ssh"
      user     = "root"
      private_key = file("~/.ssh/id_rsa")
      host = module.par1.public_ip[count.index]
    }
  }
}

so both
host = element(concat(module.par1.public_ip), count.index)
and
host = module.par1.public_ip[count.index]

worked as expected

thank you for your support once again.
Regards

Vasilios Tzanoudakis