--- layout: docs page_title: template block in the job specification description: |- The `template` block instantiates an instance of a template renderer. This creates a convenient way to ship configuration files that are populated from environment variables, Consul data, Vault secrets, or just general configurations within a Nomad task. --- # `template` block in the job specification The `template` block instantiates an instance of a template renderer. This creates a convenient way to ship configuration files that are populated from environment variables, Consul data, Vault secrets, or just general configurations within a Nomad task. ```hcl job "docs" { group "example" { task "server" { template { source = "local/redis.conf.tpl" destination = "local/redis.conf" change_mode = "signal" change_signal = "SIGINT" } } } } ``` Nomad utilizes [Go template][gt] and a tool called [Consul Template][ct], which adds a set of new functions that can be used to retrieve data from Consul and Vault. Nomad templates can reference [Nomad's runtime environment variables][env], [node attributes and metadata][nodevars], [Nomad service registrations][ct_api_nsvc], and [Nomad variables][nvars]. Templates can also be used to provide environment variables to your workload. For a full list of the API template functions, please refer to the [Consul Template documentation][ct_api]. For a an introduction to Go templates, please refer to the [Learn Go Template Syntax][gt_learn] guide. ## Parameters - `change_mode` `(string: "restart")` - Specifies the behavior Nomad should take if the rendered template changes. Nomad will always write the new contents of the template to the specified destination. The following possible values describe Nomad's action after writing the template to disk. - `"noop"` - take no action (continue running the task) - `"restart"` - restart the task - `"signal"` - send a configurable signal to the task - `"script"` - run a script - `change_signal` `(string: "")` - Specifies the signal to send to the task as a string like `"SIGUSR1"` or `"SIGINT"`. This option is required if the `change_mode` is `signal`. - `change_script` ([`ChangeScript`][]: nil) - Configures the script triggered on template change. This option is required if the `change_mode` is `script`. - `data` `(string: "")` - Specifies the raw template to execute. One of `source` or `data` must be specified, but not both. This is useful for smaller templates, but we recommend using `source` for larger templates. - `destination` `(string: )` - Specifies the location where the resulting template should be rendered, relative to the [task working directory][]. Only drivers without filesystem isolation (ex. `raw_exec`) or that build a chroot in the task working directory (ex. `exec`) can render templates outside of the `NOMAD_ALLOC_DIR`, `NOMAD_TASK_DIR`, or `NOMAD_SECRETS_DIR`. For more details on how `destination` interacts with task drivers, see the [Filesystem internals][] documentation. Note that where possible, the `NOMAD_SECRETS_DIR` is mounted `noexec`, so rendered templates can't be used as self-executing scripts. - `env` `(bool: false)` - Specifies the template should be read back in as environment variables for the task ([example](#environment-variables)). To update the environment on changes, you must set `change_mode` to `restart`. Setting `env` when the `change_mode` is `signal` will return a validation error. Setting `env` when the `change_mode` is `noop` is permitted but will not update the environment variables in the task. - `error_on_missing_key` `(bool: false)` - Specifies how the template behaves when attempting to index a map key that does not exist in the map. - When `true`, the template engine will return an error, which will cause the task to fail. - When `false`, the template engine will do nothing and continue executing the template. If printed, the result of the index operation is the string "". - `left_delimiter` `(string: "{{")` - Specifies the left delimiter to use in the template. The default is "{{" for some templates, it may be easier to use a different delimiter that does not conflict with the output file itself. - `perms` `(string: "644")` - Specifies the rendered template's permissions. File permissions are given as octal of the Unix file permissions `rwxrwxrwx`. - `uid` `(int: nil)` - Specifies the rendered template owner's user ID. If negative or not specified (`nil`) the ID of the Nomad agent user will be used. ~> **Caveat:** Works only on Unix-based systems. Be careful when using containerized drivers, such as `docker` or `podman`, as groups and users inside the container may have different IDs than on the host system. This feature will also **not** work with Docker Desktop. - `gid` `(int: nil)` - Specifies the rendered template owner's group ID. If negative or not specified (`nil`) the ID of the Nomad agent group will be used. ~> **Caveat:** Works only on Unix-based systems. Be careful when using containerized drivers, such as `docker` or `podman`, as groups and users inside the container may have different IDs than on the host system. This feature will also **not** work with Docker Desktop. - `Once` `(bool: false)` - Specifies that the client will wait for the template to be rendered, and then no longer watch the dependencies specified in the template. This is useful for templates that do not need to be updated. Once mode implicitly disables wait/quiescence timers for this template, and ignores change mode/signal/script settings. - `right_delimiter` `(string: "}}")` - Specifies the right delimiter to use in the template. The default is "}}" for some templates, it may be easier to use a different delimiter that does not conflict with the output file itself. - `source` `(string: "")` - Specifies the path to the template to be rendered. One of `source` or `data` must be specified, but not both. This source can be fetched using an [`artifact`][artifact] resource. The template must exist in the [task working directory][] prior to starting the task; it is not possible to reference a template whose source is inside a Docker container, for example. - `splay` `(string: "5s")` - Specifies a random amount of time to wait between 0 ms and the given splay value before invoking the change mode. This is specified using a label suffix like "30s" or "1h", and is often used to prevent a thundering herd problem where all task instances restart at the same time. - `wait` `(Code: nil)` - Defines the minimum and maximum amount of time to wait for the Consul cluster to reach a consistent state before rendering a template. This is useful to enable in systems where network connectivity to Consul is degraded, because it will reduce the number of times a template is rendered. This setting can be overridden by the [`client.template.wait_bounds`]. If the template configuration has a `min` lower than `client.template.wait_bounds.min` or a `max` greater than `client.template.wait_bounds.max`, the client's bounds will be enforced, and the template `wait` will be adjusted before being sent to the template engine. ```hcl wait { min = "5s" max = "10s" } ``` - `vault_grace` `(string: "15s")` - [Deprecated](https://github.com/hashicorp/consul-template/issues/1268) ## Examples The following examples only show the `template` blocks. Remember that the `template` block is only valid in the placements listed above. ### Inline template This example uses an inline template to render a file to disk. This file watches various keys in Consul for changes: ```hcl template { data = "---\nkey: {{ key \"service/my-key\" }}" destination = "local/file.yml" } ``` It is also possible to use heredocs for multi-line templates, like: ```hcl template { data = < Using the Nomad integration results in an implicit dependency on the Nomad servers. If the Nomad servers are unavailable (for example, a client is disconnected), then after some retries, running allocations will be stopped. Refer to the [`template.nomad_retry`][] client configuration option for more details and to adjust the retry behaviour. Nomad [variables][] can be queried using the `nomadVar`, `nomadVarList`, `nomadVarListSafe`, and `nomadVarExists` functions. #### `nomadVarList` and `nomadVarListSafe` These functions can be used to list the paths of Nomad variables available to the task based on its [workload identity]. You can provide an optional prefix to filter the path list by as a parameter. The list functions return a slice of `NomadVarMeta` type: ```golang type NomadVarMeta struct { Namespace, Path string CreateIndex, ModifyIndex uint64 CreateTime, ModifyTime nanoTime } ``` The `nanoTime` type contains Unix nanoseconds since the epoch. Its string method prints it out as a formatted date/time value. It also has a `Time` method that can be used to retrieve a Go [`time.Time`] for further manipulation. `NomadVarMeta` objects print their `Path` value when used as a string. For example, these two template blocks produce identical output. ```hcl template { data = < Using the Consul integration results in an implicit dependency on Consul. If Consul is unavailable, then after some retries, running allocations will be stopped. Refer to the [`template.consul_retry`][] client configuration option for more details and to adjust the retry behaviour. ### Consul KV Consul KV values can be accessed using the [`key`][ct_api_key] function to retrieve a single value from a key path. The [`ls`][ct_api_ls] function can be used to retrieve all keys in a path. For deeply nested paths, use the [`tree`][ct_api_tree] function. ```hcl template { data = < Using the Vault integration results in an implicit dependency on Vault. If Vault is unavailable, then after some retries, running allocations will be stopped. Refer to the [`template.vault_retry`][] client configuration option for more details and to adjust the retry behaviour. ### PKI certificate Vault is a popular open source tool for managing secrets. In addition to acting as an encrypted KV store, Vault can also generate dynamic secrets, like PKI/TLS certificates. When generating PKI certificates with Vault, the certificate, private key, and any intermediate certs are all returned as part of the same API call. Most software requires these files be placed in separate files on the system. #### As individual files For templates, all dependencies are mapped into a single list. This means that multiple templates watching the same path return the same data. ```hcl template { data = <