github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/website/source/docs/job-specification/template.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "template Stanza - Job Specification" 4 sidebar_current: "docs-job-specification-template" 5 description: |- 6 The "template" block instantiates an instance of a template renderer. This 7 creates a convenient way to ship configuration files that are populated from 8 environment variables, Consul data, Vault secrets, or just general 9 configurations within a Nomad task. 10 --- 11 12 # `template` Stanza 13 14 <table class="table table-bordered table-striped"> 15 <tr> 16 <th width="120">Placement</th> 17 <td> 18 <code>job -> group -> task -> **template**</code> 19 </td> 20 </tr> 21 </table> 22 23 The `template` block instantiates an instance of a template renderer. This 24 creates a convenient way to ship configuration files that are populated from 25 environment variables, Consul data, Vault secrets, or just general 26 configurations within a Nomad task. 27 28 ```hcl 29 job "docs" { 30 group "example" { 31 task "server" { 32 template { 33 source = "local/redis.conf.tpl" 34 destination = "local/redis.conf" 35 change_mode = "signal" 36 change_signal = "SIGINT" 37 } 38 } 39 } 40 } 41 ``` 42 43 Nomad utilizes a tool called [Consul Template][ct]. Since Nomad v0.5.3, the 44 template can reference [Nomad's runtime environment variables][env]. Since Nomad 45 v0.5.6, the template can reference [Node attributes and metadata][nodevars]. For 46 a full list of the API template functions, please refer to the [Consul Template 47 README][ct]. 48 49 ## `template` Parameters 50 51 - `change_mode` `(string: "restart")` - Specifies the behavior Nomad should take 52 if the rendered template changes. Nomad will always write the new contents of 53 the template to the specified destination. The possible values below describe 54 Nomad's action after writing the template to disk. 55 56 - `"noop"` - take no action (continue running the task) 57 - `"restart"` - restart the task 58 - `"signal"` - send a configurable signal to the task 59 60 - `change_signal` `(string: "")` - Specifies the signal to send to the task as a 61 string like `"SIGUSR1"` or `"SIGINT"`. This option is required if the 62 `change_mode` is `signal`. 63 64 - `data` `(string: "")` - Specifies the raw template to execute. One of `source` 65 or `data` must be specified, but not both. This is useful for smaller 66 templates, but we recommend using `source` for larger templates. 67 68 - `destination` `(string: <required>)` - Specifies the location where the 69 resulting template should be rendered, relative to the task directory. 70 71 * `left_delimiter` `(string: "{{")` - Specifies the left delimiter to use in the 72 template. The default is "{{" for some templates, it may be easier to use a 73 different delimiter that does not conflict with the output file itself. 74 75 - `perms` `(string: "666")` - Specifies the rendered template's permissions. 76 File permissions are given as octal of the unix file permissions rwxrwxrwx. 77 78 * `right_delimiter` `(string: "}}")` - Specifies the right delimiter to use in the 79 template. The default is "}}" for some templates, it may be easier to use a 80 different delimiter that does not conflict with the output file itself. 81 82 - `source` `(string: "")` - Specifies the path to the template to be rendered. 83 One of `source` or `data` must be specified, but not both. This source can 84 optionally be fetched using an [`artifact`][artifact] resource. This template 85 must exist on the machine prior to starting the task; it is not possible to 86 reference a template inside of a Docker container, for example. 87 88 - `splay` `(string: "5s")` - Specifies a random amount of time to wait between 89 0ms and the given splay value before invoking the change mode. This is 90 specified using a label suffix like "30s" or "1h", and is often used to 91 prevent a thundering herd problem where all task instances restart at the same 92 time. 93 94 ## `template` Examples 95 96 The following examples only show the `template` stanzas. Remember that the 97 `template` stanza is only valid in the placements listed above. 98 99 ### Inline Template 100 101 This example uses an inline template to render a file to disk. This file watches 102 various keys in Consul for changes: 103 104 ```hcl 105 template { 106 data = "---\nkey: {{ key \"service/my-key\" }}" 107 destination = "local/file.yml" 108 } 109 ``` 110 111 It is also possible to use heredocs for multi-line templates, like: 112 113 ```hcl 114 template { 115 data = <<EOH 116 --- 117 bind_port: {{ env "NOMAD_PORT_db" }} 118 scratch_dir: {{ env "NOMAD_TASK_DIR" }} 119 service_key: {{ key "service/my-key" }} 120 EOH 121 122 destination = "local/file.yml" 123 } 124 ``` 125 126 ### Remote Template 127 128 This example uses an [`artifact`][artifact] stanza to download an input template 129 before passing it to the template engine: 130 131 ```hcl 132 artifact { 133 source = "https://example.com/file.yml.tpl" 134 destination = "local/file.yml.tpl" 135 } 136 137 template { 138 source = "local/file.yml.tpl" 139 destination = "local/file.yml" 140 } 141 ``` 142 143 ### Node Variables 144 145 As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata. 146 147 ```hcl 148 template { 149 data = <<EOH 150 --- 151 node_dc: {{ env "node.datacenter" }} 152 node_cores: {{ env "attr.cpu.numcores" }} 153 meta_key: {{ env "meta.node_meta_key" }} 154 EOH 155 156 destination = "local/file.yml" 157 } 158 ``` 159 160 ## Client Configuration 161 162 The `template` block has the following [client configuration 163 options](/docs/agent/config.html#options): 164 165 * `template.allow_host_source` - Allows templates to specify their source 166 template as an absolute path referencing host directories. Defaults to `true`. 167 168 [ct]: https://github.com/hashicorp/consul-template "Consul Template by HashiCorp" 169 [artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification" 170 [env]: /docs/runtime/environment.html "Nomad Runtime Environment" 171 [nodevars]: /docs/runtime/interpolation.html#interpreted_node_vars "Nomad Node Variables"