github.com/jrxfive/nomad@v0.6.1-0.20170802162750-1fef470e89bf/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      node_id:     {{ env "node.unique.id" }}
   123      service_key: {{ key "service/my-key" }}
   124    EOH
   125  
   126    destination = "local/file.yml"
   127  }
   128  ```
   129  
   130  ### Remote Template
   131  
   132  This example uses an [`artifact`][artifact] stanza to download an input template
   133  before passing it to the template engine:
   134  
   135  ```hcl
   136  artifact {
   137    source      = "https://example.com/file.yml.tpl"
   138    destination = "local/file.yml.tpl"
   139  }
   140  
   141  template {
   142    source      = "local/file.yml.tpl"
   143    destination = "local/file.yml"
   144  }
   145  ```
   146  
   147  ### Node Variables
   148  
   149  As of Nomad v0.5.6 it is possible to access the Node's attributes and metadata.
   150  
   151  ```hcl
   152  template {
   153    data = <<EOH
   154    ---
   155      node_dc:    {{ env "node.datacenter" }}
   156      node_cores: {{ env "attr.cpu.numcores" }}
   157      meta_key:   {{ env "meta.node_meta_key" }}
   158    EOH
   159  
   160    destination = "local/file.yml"
   161  }
   162  ```
   163  
   164  ### Environment Variables
   165  
   166  Since v0.6.0 templates may be used to create environment variables for tasks.
   167  Env templates work exactly like other templates except once they're written,
   168  they're read back in as `KEY=value` pairs. Those key value pairs are included
   169  in the task's environment.
   170  
   171  For example the following template stanza:
   172  
   173  ```hcl
   174  template {
   175    data = <<EOH
   176  # Lines starting with a # are ignored
   177  
   178  # Empty lines are also ignored
   179  LOG_LEVEL="{{key "service/geo-api/log-verbosity"}}"
   180  API_KEY="{{with secret "secret/geo-api-key"}}{{.Data.key}}{{end}}"
   181  EOH
   182  
   183    destination = "secrets/file.env"
   184    env         = true
   185  }
   186  ```
   187  
   188  The task's environment would then have environment variables like the
   189  following:
   190  
   191  ```
   192  LOG_LEVEL=DEBUG
   193  API_KEY=12345678-1234-1234-1234-1234-123456789abc
   194  ```
   195  
   196  This allows [12factor app](https://12factor.net/config) style environment
   197  variable based configuration while keeping all of the familiar features and
   198  semantics of Nomad templates.
   199  
   200  If a value may include newlines you should JSON encode it:
   201  
   202  ```
   203  CERT_PEM={{ file "path/to/cert.pem" | toJSON }}
   204  ```
   205  
   206  The parser will read the JSON string, so the `$CERT_PEM` environment variable
   207  will be identical to the contents of the file.
   208  
   209  For more details see [go-envparser's
   210  README](https://github.com/schmichael/go-envparse#readme).
   211  
   212  ## Client Configuration
   213  
   214  The `template` block has the following [client configuration
   215  options](/docs/agent/configuration/client.html#options):
   216  
   217  * `template.allow_host_source` - Allows templates to specify their source
   218    template as an absolute path referencing host directories. Defaults to `true`.
   219  
   220  [ct]: https://github.com/hashicorp/consul-template "Consul Template by HashiCorp"
   221  [artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
   222  [env]: /docs/runtime/environment.html "Nomad Runtime Environment"
   223  [nodevars]: /docs/runtime/interpolation.html#interpreted_node_vars "Nomad Node Variables"