github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/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]. Since Nomad v0.6.0, templates can be read as environment variables.
    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  - `env` `(bool: false)` - Specifies the template should be read back in as
    72    environment variables for the task. (See below)
    73  
    74  - `left_delimiter` `(string: "{{")` - Specifies the left delimiter to use in the
    75    template. The default is "{{" for some templates, it may be easier to use a
    76    different delimiter that does not conflict with the output file itself.
    77  
    78  - `perms` `(string: "644")` - Specifies the rendered template's permissions.
    79    File permissions are given as octal of the Unix file permissions rwxrwxrwx.
    80  
    81  - `right_delimiter` `(string: "}}")` - Specifies the right delimiter to use in the
    82    template. The default is "}}" for some templates, it may be easier to use a
    83    different delimiter that does not conflict with the output file itself.
    84  
    85  - `source` `(string: "")` - Specifies the path to the template to be rendered.
    86    One of `source` or `data` must be specified, but not both. This source can
    87    optionally be fetched using an [`artifact`][artifact] resource. This template
    88    must exist on the machine prior to starting the task; it is not possible to
    89    reference a template inside of a Docker container, for example.
    90  
    91  - `splay` `(string: "5s")` - Specifies a random amount of time to wait between
    92    0 ms and the given splay value before invoking the change mode. This is
    93    specified using a label suffix like "30s" or "1h", and is often used to
    94    prevent a thundering herd problem where all task instances restart at the same
    95    time.
    96  
    97  ## `template` Examples
    98  
    99  The following examples only show the `template` stanzas. Remember that the
   100  `template` stanza is only valid in the placements listed above.
   101  
   102  ### Inline Template
   103  
   104  This example uses an inline template to render a file to disk. This file watches
   105  various keys in Consul for changes:
   106  
   107  ```hcl
   108  template {
   109    data        = "---\nkey: {{ key \"service/my-key\" }}"
   110    destination = "local/file.yml"
   111  }
   112  ```
   113  
   114  It is also possible to use heredocs for multi-line templates, like:
   115  
   116  ```hcl
   117  template {
   118    data = <<EOH
   119    ---
   120      bind_port:   {{ env "NOMAD_PORT_db" }}
   121      scratch_dir: {{ env "NOMAD_TASK_DIR" }}
   122      service_key: {{ key "service/my-key" }}
   123    EOH
   124  
   125    destination = "local/file.yml"
   126  }
   127  ```
   128  
   129  ### Remote Template
   130  
   131  This example uses an [`artifact`][artifact] stanza to download an input template
   132  before passing it to the template engine:
   133  
   134  ```hcl
   135  artifact {
   136    source      = "https://example.com/file.yml.tpl"
   137    destination = "local/file.yml.tpl"
   138  }
   139  
   140  template {
   141    source      = "local/file.yml.tpl"
   142    destination = "local/file.yml"
   143  }
   144  ```
   145  
   146  ### Node Variables
   147  
   148  As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata.
   149  
   150  ```hcl
   151  template {
   152    data = <<EOH
   153    ---
   154      node_dc:    {{ env "node.datacenter" }}
   155      node_cores: {{ env "attr.cpu.numcores" }}
   156      meta_key:   {{ env "meta.node_meta_key" }}
   157    EOH
   158  
   159    destination = "local/file.yml"
   160  }
   161  ```
   162  
   163  ### Environment Variables
   164  
   165  Since v0.6.0 templates may be used to create environment variables for tasks.
   166  Env templates work exactly like other templates except once they're written,
   167  they're read back in as `KEY=value` pairs. Those key value pairs are included
   168  in the task's environment.
   169  
   170  For example the following template stanza:
   171  
   172  ```hcl
   173  template {
   174    data = <<EOH
   175  # Lines starting with a # are ignored
   176  
   177  # Empty lines are also ignored
   178  CORES={{ env "attr.cpu.numcores" }}
   179  SERVICE_KEY={{ key "service/my-key" }}
   180  EOH
   181  
   182    destination = "local/file.env"
   183    env         = true
   184  }
   185  ```
   186  
   187  The task's environment would then have environment variables like the
   188  following:
   189  
   190  ```
   191  CORES=4
   192  SERVICE_KEY=12345678-1234-1234-1234-1234-123456789abc
   193  ```
   194  
   195  This allows [12factor app](https://12factor.net/config) style environment
   196  variable based configuration while keeping all of the familiar features and
   197  semantics of Nomad templates.
   198  
   199  If a value may include newlines you should JSON encode it:
   200  
   201  ```
   202  CERT_PEM={{ file "path/to/cert.pem" | toJSON }}
   203  ```
   204  
   205  The parser will read the JSON string, so the `$CERT_PEM` environment variable
   206  will be identical to the contents of the file.
   207  
   208  For more details see [go-envparser's
   209  README](https://github.com/schmichael/go-envparse#readme).
   210  
   211  ## Client Configuration
   212  
   213  The `template` block has the following [client configuration
   214  options](/docs/agent/configuration/client.html#options):
   215  
   216  * `template.allow_host_source` - Allows templates to specify their source
   217    template as an absolute path referencing host directories. Defaults to `true`.
   218  
   219  [ct]: https://github.com/hashicorp/consul-template "Consul Template by HashiCorp"
   220  [artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
   221  [env]: /docs/runtime/environment.html "Nomad Runtime Environment"
   222  [nodevars]: /docs/runtime/interpolation.html#interpreted_node_vars "Nomad Node Variables"