template Stanza
Placement | job -> group -> task -> template |
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.
Nomad utilizes a tool called Consul Template. Since Nomad v0.5.3, the template can reference Nomad's runtime environment variables. Since Nomad v0.5.6, the template can reference Node attributes and metadata. For a full list of the API template functions, please refer to the Consul Template README. Since Nomad v0.6.0, templates can be read as environment variables.
template
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 possible values below describe Nomad's action after writing the template to disk.change_signal
(string: "")
- Specifies the signal to send to the task as a string like"SIGUSR1"
or"SIGINT"
. This option is required if thechange_mode
issignal
.data
(string: "")
- Specifies the raw template to execute. One ofsource
ordata
must be specified, but not both. This is useful for smaller templates, but we recommend usingsource
for larger templates.destination
(string: <required>)
- 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 theNOMAD_ALLOC_DIR
,NOMAD_TASK_DIR
, orNOMAD_SECRETS_DIR
. For more details on howdestination
interacts with task drivers, see the Filesystem internals documentation.env
(bool: false)
- Specifies the template should be read back in as environment variables for the task (see below). To update the environment on changes, you must setchange_mode
torestart
. Settingenv
when thechange_mode
issignal
will return a validation error. Settingenv
when thechange_mode
isnoop
is permitted but will not update the environment variables in the task.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 permissionsrwxrwxrwx
.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 ofsource
ordata
must be specified, but not both. This source can optionally be fetched using anartifact
resource. This template must exist on the machine prior to starting the task; it is not possible to reference a template inside of 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.vault_grace
(string: "15s")
- Deprecated
template
Examples
The following examples only show the template
stanzas. Remember that the
template
stanza 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:
It is also possible to use heredocs for multi-line templates, like:
Remote Template
This example uses an artifact
stanza to download an input template
before passing it to the template engine:
Node Variables
As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata.
Environment Variables
Since v0.6.0 templates may be used to create environment variables for tasks.
Env templates work exactly like other templates except once the templates are
written, they are parsed as KEY=value
pairs. Those key value pairs are
included in the task's environment.
For example the following template stanza:
The task's environment would then have environment variables like the following:
This allows 12factor app style environment variable based configuration while keeping all of the familiar features and semantics of Nomad templates.
Secrets or certificates may contain a wide variety of characters such as newlines, quotes, and backslashes which may be difficult to quote or escape properly.
Whenever a templated variable may include special characters, use the toJSON
function to ensure special characters are properly parsed by Nomad:
The parser will read the JSON string, so the $CERT_PEM
environment variable
will be identical to the contents of the file.
Likewise when evaluating a password that may contain quotes or #
, use the
toJSON
function to ensure Nomad passes the password to task unchanged:
For more details see go-envparser's README.
Template Destinations
Templates are rendered into the task working directory. Drivers without
filesystem isolation (such as raw_exec
) or drivers that build a chroot in
the task working directory (such as exec
) can have templates rendered to
arbitrary paths in the task. But task drivers such as docker
can only access
templates rendered into the NOMAD_ALLOC_DIR
, NOMAD_TASK_DIR
, or
NOMAD_SECRETS_DIR
. To workaround this restriction, you can create a mount
from the template destination
to another location in the task.
Vault Integration
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.
Note: generate_lease
must be set to true
(non-default) on the Vault PKI
role.
Failure to do so will cause the template to frequently render a new
certificate, approximately every minute. This creates a significant number of
certificates to be expired in Vault and could ultimately lead to Vault performance
impacts and failures.
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.
These are three different input templates, but when run under the Nomad job, they are compressed into a single call, sharing the resulting data.
As a PEM formatted file
This example acquires a PKI certificate from Vault in PEM format, concatenates the elements into a bundle, and stores it into your application's secret directory.
Vault KV API v1
Under Vault KV API v1, paths start with secret/
, and the response returns the
raw key/value data. This secret was set using
vault kv put secret/aws/s3 aws_access_key_id=somekeyid
.
Note that if the name of a secret includes the -
character, you must access
it by index. This secret was set using vault kv put secret/app db-password=somepassword
.
Vault KV API v2
Under Vault KV API v2, paths start with secret/data/
, and the response returns
metadata in addition to key/value data. This secret was set using
vault kv put secret/aws/s3 aws_access_key_id=somekeyid
.
Notice the addition of data
in both the path and the field accessor string.
Additionally, when using the Vault v2 API, the Vault policies applied to your
Nomad jobs will need to grant permissions to read
under secret/data/...
rather than secret/...
.
Similar to KV API v1, if the name of a secret includes the -
character, you
must access it by index. This secret was set using vault kv put secret/app db-password=somepassword
.
Client Configuration
The template
block has the following client configuration
options:
function_denylist
([]string: ["plugin"])
- Specifies a list of template rendering functions that should be disallowed in job specs. By default theplugin
function is disallowed as it allows running arbitrary commands on the host as root (unless Nomad is configured to run as a non-root user).disable_file_sandbox
(bool: false)
- Allows templates access to arbitrary files on the client host via thefile
function. By default templates can access files only within the task working directory.