I have a TF module (TF 1.2.8) which creates a vSphere VM. In the metadata template file I want to have it so I can specify X number of nameservers. The current template looks like this:-
metadata.tftpl
instance-id: ${vm_name}
local-hostname: ${vm_name}
network:
version: 2
ethernets:
ens192:
dhcp4: false
addresses:
- ${vm_ip}
gateway4: ${vm_gateway}
nameservers:
addresses:
%{ for addr in vm_nameservers ~}
- ${addr}
%{ endfor ~}
and the template block for this is:-
data "template_file" "metadata" {
template = "${file("${path.module}/templates/metadata.tftpl")}"
vars = {
vm_name = var.vm_name
vm_ip = var.vm_ip
vm_gateway = var.vm_gateway
vm_nameservers = var.vm_nameservers
}
}
with the variable declarations being
variable "vm_nameservers" {
type = list
}
and an example of how I want to lay it out in the tfvars file:
vm_nameservers = ["10.10.0.2", "10.10.0.3"]
I've tried looking through the docs for this but there doesn't seem to be anything obvious that would solve the issue, I think I may have declared the variable type incorrectly.
When I run a plan I get the following error:-
Error: Incorrect attribute value type
│
│ on .terraform/modules/vm/main.tf line 37, in data "template_file" "metadata":
│ 37: vars = {
│ 38: vm_name = var.vm_name
│ 39: vm_ip = var.vm_ip
│ 40: vm_gateway = var.vm_gateway
│ 41: vm_nameservers = var.vm_nameservers
│ 42: }
│ ├────────────────
│ │ var.vm_gateway is a string, known only after apply
│ │ var.vm_ip is a string, known only after apply
│ │ var.vm_name is a string, known only after apply
│ │ var.vm_nameservers is a list of dynamic, known only after apply
│
│ Inappropriate value for attribute "vars": element "vm_nameservers": string
│ required.
╵
Honestly I didn't understand the scenario you try to explain, but there are 2 ways to create multiple resources of the same type: count or for_each.
Count would go as:
or for_each:
So I guess it would be something like
PS: This is just an example based on the concepts of how to use count and for_each, the code above should not work as there is no resource "whatever"