# Hcl2 Template Task

Hcl2 Template task enables rendering source with given context.

Added in v0.3.0.

# Example Usage

# File: template.hcl
flow "template" {

  variable "global" {
    default = "global"
  }

  task "hcl2_template" "this" {
    # Set the source for the template.
    # You can interpolate variable via `${...}` syntax.
    source = <<EOT
${ answer }
${ final.answer }
${ var.global }
${ 40 + 2 }
${ sum([20, 20, 2]) }
EOT

    # Set the context for the template rendering.
    # It will be merged with global context.
    context = {
      answer = 42
      final = {
        answer = 42
      }
    }
  }

  task "file_write" "this" {
    filename = "/dev/stdout"
    content = task.hcl2_template.this.content
  }
}

Click me to view the run output

Run:

$ runflow run examples/template.hcl --var global=42
[2021-06-12 20:19:34,003] "task.template.this" is started.
[2021-06-12 20:19:34,003] "task.template.this" is successful.
[2021-06-12 20:19:34,017] "task.file_write.this" is started.
42
42
42
42
42
[2021-06-12 20:19:34,022] "task.file_write.this" is successful.

# Argument Reference

The following arguments are supported:

  • source - (Required, str) The template string.
  • context - (Optional, map) The context for rendering the template. The context will be merged with global execution context so you are free to use something like ${task.bash_run.TASK_NAME.stdout} in the source.

# Attributes Reference

The following attributes are supported:

  • content - The rendered content.