# Render a template inside a cloud-init config file

**URL:** <https://discuss.hashicorp.com/t/render-a-template-inside-a-cloud-init-config-file/35036>\
**Category:** Terraform\
**Created:** [January 31, 2022, 8:43pm UTC](https://discuss.hashicorp.com/t/render-a-template-inside-a-cloud-init-config-file/35036 "2022-01-31T20:43:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lethargosapatheia](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lethargosapatheia/32/13120_2.png) [@lethargosapatheia](https://discuss.hashicorp.com/u/lethargosapatheia)\
**Post date:** [January 31, 2022, 8:43pm UTC](https://discuss.hashicorp.com/t/render-a-template-inside-a-cloud-init-config-file/35036/1 "2022-01-31T20:43:17Z")

</div>

Hello,

I’m trying to use a template inside the cloud-init config file and the only thing I’ve come up with is the following:

```auto
data "template_file" "consul_config" {
        template = file("${path.module}/files/consul-config.tftpl")
        vars = {
                consul_datacenter = var.consul_datacenter
                ui_config = var.ui_config
                server_role = var.server_role
                retry_join = var.retry_join
                consul_domain = var.consul_domain
        }
}
# In Terraform 0.12 and later, the templatefile function offers a built-in mechanism for rendering a template from a file. Use that instead of `template_file` data source.

# Source the cloud-init config file
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
                })
        }
}

```

The issue I have with this is that the terraform documentation clearly states that you should switch to `templatefile` function instead of using the `template_file` data. In this case I used the latter simply because I don’t know how I would be able to integrate it in the `template_cloudinit_config` stanza.

Is there any way I can write this better? Any help is much appreciated!

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [February 1, 2022, 6:18pm UTC](https://discuss.hashicorp.com/t/render-a-template-inside-a-cloud-init-config-file/35036/2 "2022-02-01T18:18:02Z")

</div>

Hi @lethargosapatheia!

The result of `templatefile` is just another string, so there’s no reason why you can’t use another call to `templatefile` with a different template as the definition of one of your variables passed to the outer template.

For example:

```auto
content = templatefile("${path.module}/files/userdata.tpl", {
  root_ssh_key = file("${path.module}/files/id_ed25519.pub")
  consul_config = templatefile("${path.module}/files/consul-config.tftpl", {
    consul_datacenter = var.consul_datacenter
    ui_config = var.ui_config
    # (etc...)
  })
})

```

---

<div class="post-metadata">

**Author:** ![lethargosapatheia](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lethargosapatheia/32/13120_2.png) [@lethargosapatheia](https://discuss.hashicorp.com/u/lethargosapatheia)\
**Post date:** [February 3, 2022, 2:11pm UTC](https://discuss.hashicorp.com/t/render-a-template-inside-a-cloud-init-config-file/35036/3 "2022-02-03T14:11:55Z")

</div>

Thank you, I appreciate it. I tried it initially, but I had made a mistake and I thought that it wouldn’t work 🙂
