open-nomad/website/source/docs/runtime/interpolation.html.md

261 lines
7.6 KiB
Markdown
Raw Normal View History

2016-02-06 23:34:21 +00:00
---
layout: "docs"
page_title: "Interpolation - Runtime"
sidebar_current: "docs-runtime-interpolation"
2016-02-06 23:34:21 +00:00
description: |-
Learn about the Nomad's interpolation and interpreted variables.
2016-02-06 23:34:21 +00:00
---
# Interpolation
Nomad supports interpreting two classes of variables, node attributes and
runtime environment variables. Node attributes are interpretable in constraints,
task environment variables and certain driver fields. Runtime environment
variables are not interpretable in constraints because they are only defined
once the scheduler has placed them on a particular node.
2016-02-06 23:34:21 +00:00
The syntax for interpreting variables is `${variable}`. An example and a
comprehensive list of interpretable fields can be seen below:
2016-10-03 19:50:35 +00:00
```hcl
task "docs" {
2016-10-03 19:50:35 +00:00
driver = "docker"
# Drivers support interpreting node attributes and runtime environment
# variables
config {
image = "my-app"
# Interpret runtime variables to inject the address to bind to and the
# location to write logs to.
args = [
"--bind", "${NOMAD_ADDR_RPC}",
"--logs", "${NOMAD_ALLOC_DIR}/logs",
]
port_map {
RPC = 6379
2016-02-06 23:34:21 +00:00
}
2016-10-03 19:50:35 +00:00
}
# Constraints only support node attributes as runtime environment variables
# are only defined after the task is placed on a node.
constraint {
attribute = "${attr.kernel.name}"
value = "linux"
}
# Environment variables are interpreted and can contain both runtime and
# node attributes. There environment variables are passed into the task.
env {
"DC" = "Running on datacenter ${node.datacenter}"
"VERSION" = "Version ${NOMAD_META_VERSION}"
}
# Meta keys are also interpretable.
meta {
VERSION = "v0.3"
}
2016-02-06 23:34:21 +00:00
}
```
## Node Variables <a id="interpreted_node_vars"></a>
Below is a full listing of node attributes that are interpretable. These
attributes are interpreted by __both__ constraints and within the task and
2016-02-06 23:34:21 +00:00
driver.
<table class="table table-bordered table-striped">
<tr>
<th>Variable</th>
<th>Description</th>
<th>Example Value</th>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${node.unique.id}</tt></td>
<td>36 character unique client identifier</td>
<td><tt>9afa5da1-8f39-25a2-48dc-ba31fd7c0023</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${node.datacenter}</tt></td>
<td>Client's datacenter</td>
<td><tt>dc1</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${node.unique.name}</tt></td>
<td>Client's name</td>
<td><tt>nomad-client-10-1-2-4</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${node.class}</tt></td>
<td>Client's class</td>
<td><tt>linux-64bit</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${attr.&lt;property&gt;}</tt></td>
<td>Property given by <tt>property</tt> on the client</td>
<td><tt>${attr.arch} => amd64</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${meta.&lt;key&gt;}</tt></td>
<td>Metadata value given by <tt>key</tt> on the client</td>
<td><tt>${meta.foo} => bar</tt></td>
2016-02-06 23:34:21 +00:00
</tr>
</table>
Below is a table documenting common node properties:
2016-02-06 23:34:21 +00:00
<table class="table table-bordered table-striped">
<tr>
<th>Property</th>
2016-02-06 23:34:21 +00:00
<th>Description</th>
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.arch}</tt></td>
<td>CPU architecture of the client (e.g. <tt>amd64</tt>, <tt>386</tt>)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.consul.datacenter}</tt></td>
<td>The Consul datacenter of the client (if Consul is found)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.cpu.numcores}</tt></td>
2016-02-06 23:34:21 +00:00
<td>Number of CPU cores on the client</td>
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.driver.&lt;property&gt;}</tt></td>
<td>See the [task drivers](/docs/drivers/index.html) for property documentation</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.unique.hostname}</tt></td>
2016-02-06 23:34:21 +00:00
<td>Hostname of the client</td>
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.kernel.name}</tt></td>
<td>Kernel of the client (e.g. <tt>linux</tt>, <tt>darwin</tt>)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.kernel.version}</tt></td>
<td>Version of the client kernel (e.g. <tt>3.19.0-25-generic</tt>, <tt>15.0.0</tt>)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.platform.aws.ami-id}</tt></td>
<td>AMI ID of the client (if on AWS EC2)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.platform.aws.instance-type}</tt></td>
<td>Instance type of the client (if on AWS EC2)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.os.name}</tt></td>
<td>Operating system of the client (e.g. <tt>ubuntu</tt>, <tt>windows</tt>, <tt>darwin</tt>)</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
2017-02-12 03:47:17 +00:00
<td><tt>${attr.os.version}</tt></td>
2016-02-06 23:34:21 +00:00
<td>Version of the client OS</td>
</tr>
</table>
Here are some examples of using node attributes and properties in a job file:
```hcl
job "docs" {
# This will constrain this job to only run on 64-bit clients.
constraint {
attribute = "${attr.arch}"
value = "amd64"
}
# This will restrict the job to only run on clients with 4 or more cores.
# Note: you may also declare a resource requirement for CPU for a task.
constraint {
attribute = "${cpu.numcores}"
operator = ">="
value = "4"
}
# Only run this job on a memory-optimized AWS EC2 instance.
constraint {
attribute = "${attr.platform.aws.instance-type}"
value = "m4.xlarge"
}
}
```
2016-02-06 23:34:21 +00:00
## Environment Variables <a id="interpreted_env_vars"></a>
The following are runtime environment variables that describe the environment
the task is running in. These are only defined once the task has been placed on
a particular node and as such can not be used in constraints.
<table class="table table-bordered table-striped">
<tr>
<th>Variable</th>
<th>Description</th>
</tr>
<tr>
<td><tt>${NOMAD_ALLOC_DIR}</tt></td>
<td>The path to the shared <tt>alloc/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${NOMAD_TASK_DIR}</tt></td>
<td>The path to the task <tt>local/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${NOMAD_MEMORY_LIMIT}</tt></td>
<td>The memory limit in MBytes for the task</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${NOMAD_CPU_LIMIT}</tt></td>
2016-02-06 23:34:21 +00:00
<td>The CPU limit in MHz for the task</td>
</tr>
<tr>
<td><tt>${NOMAD_ALLOC_ID}</tt></td>
<td>The allocation ID of the task</td>
</tr>
<tr>
<td><tt>${NOMAD_ALLOC_NAME}</tt></td>
<td>The allocation name of the task</td>
</tr>
<tr>
<td><tt>${NOMAD_ALLOC_INDEX}</tt></td>
2016-12-09 09:24:30 +00:00
<td>The allocation index; useful to distinguish instances of task groups. From 0 to (count - 1).</td>
</tr>
<tr>
<td><tt>${NOMAD_TASK_NAME}</tt></td>
<td>The task's name</td>
</tr>
2016-10-11 18:22:09 +00:00
<tr>
<td><tt>${NOMAD_JOB_NAME}</tt></td>
<td>The job's name</td>
</tr>
2016-02-06 23:34:21 +00:00
<tr>
<td><tt>${NOMAD_IP_&lt;label&gt;}</tt></td>
<td>The IP for the given port <tt>label</tt>. See
[here](/docs/job-specification/network.html) for more information.</td>
2016-04-15 17:27:51 +00:00
</tr>
<tr>
<td><tt>${NOMAD_PORT_&lt;label&gt;}</tt></td>
<td>The port for the port <tt>label</tt>. See [here](/docs/job-specification/network.html) for more information.</td>
2016-04-15 17:27:51 +00:00
</tr>
<tr>
<td><tt>${NOMAD_ADDR_&lt;label&gt;}</tt></td>
<td>The <tt>ip:port</tt> pair for the given port <tt>label</tt>. See
[here](/docs/job-specification/network.html) for more information.</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${NOMAD_HOST_PORT_&lt;label&gt;}</tt></td>
2016-02-06 23:34:21 +00:00
<td>The port on the host if port forwarding is being used for the port
<tt>label</tt>. See [here](/docs/job-specification/network.html#mapped_ports) for more
2016-02-06 23:34:21 +00:00
information.</td>
</tr>
<tr>
<td><tt>${NOMAD_META_&lt;key&gt;}</tt></td>
<td>The metadata value given by <tt>key</tt> on the task's metadata</td>
2016-02-06 23:34:21 +00:00
</tr>
<tr>
<td><tt>${"env_key"}</tt></td>
<td>Interpret an environment variable with key <tt>env_key</tt> set on the task.</td>
2016-02-06 23:34:21 +00:00
</tr>
</table>