Hello,
I have an issue when trying to render a template inside cloud-init using yamlencode. It seems that certain files are not properly parsed for some reason.
#cloud-config
${yamlencode({
users = [
{
name = "root"
groups = "users"
ssh_authorized_keys = [
root_ssh_key,
]
shell = "/bin/bash"
},
]
write_files = [
{
content = consul_template_systemd
path = "/etc/systemd/system/consul-template.service",
},
]
})}
And I end up with:
"write_files":
- "content": "# /etc/systemd/system/consul-template.service\n[Unit]\nDescription=consul-template\nWants=network-online.target\nAfter=network-online.target\n\n[Service]\nType=simple\n#
EnvironmentFile=/etc/consul.d/metadata\nExecStart=/usr/bin/consul-template -config
/etc/consul-template.d/consul-template.hcl \nKillSignal=SIGINT\n\n[Install]\nWantedBy=multi-user.target\n"
"path": "/etc/consul-template.d/ssh_template.json"
Any ideas why this is happening and what I’m doing wrong?
Thanks!
I’ve just realised that cloud-init actually interprets it all correctly and write the files as it should. I still find it weird though that terraform chooses to write one file in one way and another one in a different way.
[later edit:]
Ok, I’ve figured it why.
I have a template for which there’s only one varibale which is null in this case. I’m going to add values later on for other stuations. And this results in terraform not properly inserting newlines and indenting the text properly.
Anyway, it isn’t necessarily the worst thing, as - I’ve already said - it works
That’s the relevant code snippet (in case someone wonders):
data "template_file" "consul_template_systemd" {
template = file("${path.module}/files/consul/consul-template-systemd.tftpl")
vars = {
consul_template_conf_dir = ""
}
}
cloud-init template:
data "template_cloudinit_config" "consul" {
for_each = var.consul_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)
gzip = false
base64_encode = false
part {
filename = "cloud-init.cfg"
content_type = "text/cloud-config"
content = templatefile("${path.module}/files/userdata.tpl", {
root_ssh_key = file("${path.module}/files/id_ed25519.pub")
consul_config = data.template_file.consul_config.rendered
consul_template_systemd = data.template_file.consul_template_systemd.rendered
ssh_template = data.template_file.ssh_template[each.key]
})
}
part {
filename = "initialise.sh"
content_type = "text/x-shellscript"
content = templatefile("${path.module}/files/consul/initialise.sh", {
tf_hostname = each.key
})
}
}
If I assign a consul_template_conf_dir
a value, it displays it correctly.