github.com/ncodes/nomad@v0.5.7-0.20170403112158-97adf4a74fb3/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. The possible values are:
    53  
    54    - `"noop"` - take no action (continue running the task)
    55    - `"restart"` - restart the task
    56    - `"signal"` - send a configurable signal to the task
    57  
    58  - `change_signal` `(string: "")` - Specifies the signal to send to the task as a
    59    string like `"SIGUSR1"` or `"SIGINT"`. This option is required if the
    60    `change_mode` is `signal`.
    61  
    62  - `data` `(string: "")` - Specifies the raw template to execute. One of `source`
    63    or `data` must be specified, but not both. This is useful for smaller
    64    templates, but we recommend using `source` for larger templates.
    65  
    66  - `destination` `(string: <required>)` - Specifies the location where the
    67    resulting template should be rendered, relative to the task directory.
    68  
    69  * `left_delimiter` `(string: "{{")` - Specifies the left delimiter to use in the
    70    template. The default is "{{" for some templates, it may be easier to use a
    71    different delimiter that does not conflict with the output file itself.
    72  
    73  - `perms` `(string: "666")` - Specifies the rendered template's permissions.
    74    File permissions are given as octal of the unix file permissions rwxrwxrwx.
    75  
    76  * `right_delimiter` `(string: "}}")` - Specifies the right delimiter to use in the
    77    template. The default is "}}" for some templates, it may be easier to use a
    78    different delimiter that does not conflict with the output file itself.
    79  
    80  - `source` `(string: "")` - Specifies the path to the template to be rendered.
    81    One of `source` or `data` must be specified, but not both. This source can
    82    optionally be fetched using an [`artifact`][artifact] resource. This template
    83    must exist on the machine prior to starting the task; it is not possible to
    84    reference a template inside of a Docker container, for example.
    85  
    86  - `splay` `(string: "5s")` - Specifies a random amount of time to wait between
    87    0ms and the given splay value before invoking the change mode. This is
    88    specified using a label suffix like "30s" or "1h", and is often used to
    89    prevent a thundering herd problem where all task instances restart at the same
    90    time.
    91  
    92  ## `template` Examples
    93  
    94  The following examples only show the `template` stanzas. Remember that the
    95  `template` stanza is only valid in the placements listed above.
    96  
    97  ### Inline Template
    98  
    99  This example uses an inline template to render a file to disk. This file watches
   100  various keys in Consul for changes:
   101  
   102  ```hcl
   103  template {
   104    data        = "---\nkey: {{ key \"service/my-key\" }}"
   105    destination = "local/file.yml"
   106  }
   107  ```
   108  
   109  It is also possible to use heredocs for multi-line templates, like:
   110  
   111  ```hcl
   112  template {
   113    data = <<EOH
   114    ---
   115      bind_port:   {{ env "NOMAD_PORT_db" }}
   116      scratch_dir: {{ env "NOMAD_TASK_DIR" }}
   117      service_key: {{ key "service/my-key" }}
   118    EOH
   119  
   120    destination = "local/file.yml"
   121  }
   122  ```
   123  
   124  ### Remote Template
   125  
   126  This example uses an [`artifact`][artifact] stanza to download an input template
   127  before passing it to the template engine:
   128  
   129  ```hcl
   130  artifact {
   131    source      = "https://example.com/file.yml.tpl"
   132    destination = "local/file.yml.tpl"
   133  }
   134  
   135  template {
   136    source      = "local/file.yml.tpl"
   137    destination = "local/file.yml"
   138  }
   139  ```
   140  
   141  ### Node Variables
   142  
   143  As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata.
   144  
   145  ```hcl
   146  template {
   147    data = <<EOH
   148    ---
   149      node_dc:    {{ env "node.datacenter" }}
   150      node_cores: {{ env "attr.cpu.numcores" }}
   151      meta_key:   {{ env "meta.node_meta_key" }}
   152    EOH
   153  
   154    destination = "local/file.yml"
   155  }
   156  ```
   157  
   158  ## Client Configuration
   159  
   160  The `template` block has the following [client configuration
   161  options](/docs/agent/config.html#options):
   162  
   163  * `template.allow_host_source` - Allows templates to specify their source
   164    template as an absolute path referencing host directories. Defaults to `true`.
   165  
   166  [ct]: https://github.com/hashicorp/consul-template "Consul Template by HashiCorp"
   167  [artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
   168  [env]: /docs/runtime/environment.html "Nomad Runtime Environment"
   169  [nodevars]: /docs/runtime/interpolation.html#interpreted_node_vars "Nomad Node Variables"