>_ IzzyLab

Hands-on task

This task is step 5 of the course “Terraform and Ansible”

Terraform: file from a template

medium~15 min

In /root/tf, use the templatefile function to generate /opt/render/greeting.txt, substituting the variable name = "IzzyLab" into the template so the result is the string Hello, IzzyLab. Run apply. There's no internet in the sandbox: only the hashicorp/local provider is available (it sits in a local mirror, terraform init will find it on its own). Write the config in the Editor tab (the /root/tf directory is already created) and run terraform in the terminal.

What you'll practise:
  • terraform
  • templatefile
  • tftpl templates
  • path.module
  • local_file

Topic breakdown

The templatefile function in Terraform reads a template file, substitutes the variables you pass in, and returns the finished string — this is how configs are generated right during apply. Templates are usually kept in separate files with the .tftpl extension, and the placeholder for a variable is written inside as ${name}. The function takes two arguments: the path to the template (the current config folder is given by path.module) and a map of values to substitute. The result goes into the content of a local_file resource.

What's checked

  1. /opt/render/greeting.txt contains Hello, IzzyLab
  2. the content is built with the templatefile function
  3. infrastructure matches the config (plan shows no changes)

What it looks like