Instruqt Labs (beta)
  • Instruqt
  • Getting started
    • Setting up Version Control
    • Install Instruqt CLI
    • Creating your first lab
    • Configuration basics
    • Exploring the lab configuration
    • Adding your first chapter
    • Configuring sandboxes
    • Adding quizzes
    • Adding tasks and gating content
    • Finishing up
  • Documentation
    • Writing Lab Content
      • Project Structure
      • Markdown and Components
    • Integrations
      • Version Control
    • Lab reference
      • Content
        • Lab
        • Page
        • Activities
          • Task
          • Quiz
            • Multiple Choice
            • Single Choice
            • Text Answer
            • Numeric Answer
        • Layout
        • Tabs
          • Terminal
          • Service
          • Editor
          • External Website
          • Note
      • Sandbox
        • Containers
          • Container
          • Sidecar Container
        • Kubernetes
          • Cluster
          • Config
          • Helm
        • Nomad
          • Cluster
          • Job
        • Networking
          • Network
          • Ingress
        • Cloud Accounts
          • AWS
          • Azure
          • Google Cloud
        • Terraform
        • Template
        • Exec
        • Copy
        • Certificates
          • Root
          • Leaf
        • Random
          • Number
          • ID
          • UUID
          • Password
          • Creature
      • Functions
    • Tools
      • Instruqt CLI
    • Glossary
Powered by GitBook
On this page
  • Template
  • Examples
Edit on GitHub
Export as PDF
  1. Documentation
  2. Lab reference
  3. Sandbox

Template

PreviousTerraformNextExec

Last updated 11 days ago

Template

The Template resource allows the processing of templates, outputing the result as a file.

Templating uses the Handlebars language which is Mustache template language can be found at the following location:.


resource "template" "name" {
  ...
}

Template Functions

The template resource provides custom functions that can be used inside your templates as shown in the example below.

resource "template" "consul_config" {

  source = <<-EOF

  file_content = "{{ file "./myfile.txt" }}"
  quote = {{quote something}}
  trim = {{quote (trim with_whitespace)}}

  EOF

  destination = "./consul_config/consul.hcl"
}

quote [string]

Returns the original string wrapped in quotations, quote can be used with the Go template pipe modifier.

// given the string abc

quote "abc" // would return the value "abc"

trim [string]

Removes whitespace such as carrige returns and spaces from the begining and the end of the string, can be used with the Go template pipe modifier.

// given the string abc

trim " abc " // would return the value "abc"

Attributes

Attribute
Description

Source source required type: string

Local path to the template source file

Destination destination required type: string

The destination to write the processed template to.

Variables variables type: map[string]any

Variables to use with the template, variables are available to be used within the template using the go template syntax.

Given the above variables, these could be used within a template with the following convention.

Computed Attributes

These attributes are computed when the config is parsed and applied, and are therefore only known at parsetime or runtime.

Attribute
Description

Meta ID meta.id string

The full ID of the resource e.g. resource.type.name. This is computed from the full resource path:

Meta Type meta.type string

The type of the resource. This taken from the type label of the resource definition.

Meta Name meta.name string

The name of the resource. This taken from the name label of the resource definition.

Examples

Template using HereDoc

resource "template" "consul_config" {

  source = <<-EOF
  data_dir = "{{data_dir}}"
  log_level = "DEBUG"

  datacenter = "dc1"
  primary_datacenter = "dc1"

  server = true

  bootstrap_expect = 1
  ui = true

  bind_addr = "0.0.0.0"
  client_addr = "0.0.0.0"
  advertise_addr = "10.6.0.200"

  ports {
    grpc = 8502
  }

  connect {
    enabled = true
  }
  EOF

  destination = "./consul_config/consul.hcl"

  variables = {
    data_dir = "/tmp"
  }
}

data_dir = "/tmp"
log_level = "DEBUG"

datacenter = "dc1"
primary_datacenter = "dc1"

server = true

bootstrap_expect = 1
ui = true

bind_addr = "0.0.0.0"
client_addr = "0.0.0.0"
advertise_addr = "10.6.0.200"

ports {
  grpc = 8502
}

connect {
  enabled = true
}

External Files

resource "template "consul_config" {

  source = file("./mytemplate.hcl")
  destination = "./consul_config/consul.hcl"

  variables = {
    data_dir = "/tmp"
  }
}

container "consul" {
  depends_on = ["template.consul_config"]

  image   {
    name = "consul:${variable.consul_version}"
  }

  command = ["consul", "agent", "-config-file=/config/consul.hcl"]

  volume {
    source      = resource.template.consul_config.destination
    destination = "/config/consul.hcl"
  }
}

Inline variables

resource "template" "consul_config" {

source = <<-EOF
data_dir = "${data("test")}"
log_level = "DEBUG"

datacenter = "${variable.datacenter}"

server = ${variable.server}
EOF

destination = "./consul_config/consul.hcl"

}
source = "myfile.txt"
source = <<-EOF
My inline content
EOF
destination = "${data("config")}/config.toml"
variables = {
  data_dir = "/tmp"
}
data_dir = "{{data_dir}}"
// given the following resource
resource "container" "ubuntu" {
  ...
}

// the resulting id will be
resource.container.ubuntu
// given the following resource
resource "container" "ubuntu" {
  ...
}

// the resulting type will be
container
// given the following resource
resource "container" "ubuntu" {
  ...
}

// the resulting name will be
ubuntu
Mustache templating language details