Ok, so what I tried to do (being inspired by your answer here Terraform conditional operator and long string) was this:
packages = [
(can(regex("^kube-.+", each.key)) ?
[
"kubeadm",
"1.19.16-00"
],
[
"kubectl",
"1.19.16-00"
],
[
"kubelet",
"1.19.16-00"
],
[
"kubeadm",
"1.19.16-00"
], :
[
"kubeadm",
"1.19.16-00"
],
)
]
Unfortunately it doesn’t like the syntax, and I get:
│ Call to function "templatefile" failed: ./files/kubernetes/userdata.tpl:28,6-7: Missing false expression in conditional; The conditional operator (...?...:...) requires a false expression, delimited by a
│ colon..
I’m not sure how I’m supposed to do this right.
For testing purposes I turned this into:
packages = [
(each.key == "etcd-1" ?
"[ 'kubeadm', '1.19.16-00' ]," :
"[ 'kubelet', '1.19.16-00' ],"
)
]
This seems to be the right syntax, at least terraform doesn’t output that error anymore. But now I have the issue that the template doesn’t have access the to the for_each
variable:
Invalid value for “vars” parameter: vars map does not contain key “var”, referenced at ./files/kubernetes/userdata.tpl:24,4-7.
But in the data block I do have it defined:
data "template_cloudinit_config" "kubernetes" {
for_each = var.kubernetes_servers
# 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)
part {
filename = "cloud-init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/files/kubernetes/userdata.tpl", {
[...]
[Later edit:]
Ok, I think I got it. This is of course related to the fact that the template cannot directly access a variable defined in terraform, it needs to be passed somehow, so I added node_name = each.key
in the templatefile
function and called node_name
directly in the template.
So it seems to be working, I guess I’ll get back if I come across something else.
Thank you!