Template 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: Mustache templating language details

Properties

  • Name
    source
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    Local path to the template source file

    Local path

      source = "myfile.txt"
    

    HereDoc

      source = <<-EOF
      My inline content
      EOF
    
  • Name
    destination
    Type
    (string: "")
    Required
    required
    Readonly
    Description

    The destination to write the processed template to.

  • Name
    variables
    Type
    (map[string]interface: map[]{})
    Required
    Readonly
    Description

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

      variables = {
        data_dir = "/tmp"
      }
    

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

      data_dir = "{{data_dir}}"
    

Meta Properties

In addition to the main properties, all resources have meta properties, such as the id of the resource. To see the list of these properties please see the Meta Properties section in the documentation /docs/resources/meta.

Examples

Template using HereDoc

The following example shows how the template can be embedded into the resource stanza using HereDoc syntax.

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"
  }
}

The file produced from this example would look like the following

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

To leverage external files you can use the file function which loads a file returning a string. This example also shows how interpolation can be used to ensure a template is processed before it is consumed.

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"

}

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"