Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

terraform templatefile interpolation issue with list

I have the following list of maps:

variable "virtual_machines" {
  default = {
    "master1" = {
       name         = "z-ca-bdc-master1"
       worker_node  = false
       ipv4_address = "192.168.113.79"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },
    "master2" =  {
       name         = "z-ca-bdc-master2"
       worker_node  = false
       ipv4_address = "192.168.113.80"
       ipv4_netmask = "22"
       ipv4_gateway = "192.168.112.1"
       dns_server   = "192.168.112.2"
       ram          = 8192
       logical_cpu  = 4
       disk0_size   = 40
       disk1_size   = 0
    },
.
.
.

I would like to collect all the virtual machine names: z-ca-bdc-master1, z-ca-bdc-master2 etc and use them with templatefile:

locals {
   vm_names = values(var.virtual_machines)[*].name
}

resource "local_file" "kubespray_inventory" {
  content = templatefile("templates/kubespray_inventory.tpl", {
    k8s_master_host = local.vm_names
    k8s_node_host   = local.vm_names
  })
  filename = "k8s_hosts"
}

However, when I run terraform plan, I get:

Error: Error in function call

  on main.tf line 49, in resource "local_file" "kubespray_inventory":
  49:   content = templatefile("templates/kubespray_inventory.tpl", {
  50:     k8s_master_host = local.vm_names
  51:     k8s_node_host   = local.vm_names
  52:   })
    |----------------
    | local.vm_names is tuple with 5 elements

Call to function "templatefile" failed:
templates/kubespray_inventory.tpl:2,3-16: Invalid template interpolation
value; Cannot include the given value in a string template: string required.,
and 2 other diagnostic(s).

The template file I'm using looks like this:

[all]
${k8s_node_host}

[kube-master]
${k8s_master_host}

[etcd]

[kube-node]
${k8s_node_host}

[calico-rr]

[k8s-cluster:children]
kube-master
kube-node
calico-rr

I know that I need to add some logic to differentiate between master and worker nodes, but in the first instance I want to get the file created.

Can someone please advise how I go about fixing this - thanks

question from:https://stackoverflow.com/questions/65936964/terraform-templatefile-interpolation-issue-with-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...