github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/website/source/docs/runtime/interpolation.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Interpolation - Runtime"
     4  sidebar_current: "docs-runtime-interpolation"
     5  description: |-
     6    Learn about the Nomad's interpolation and interpreted variables.
     7  ---
     8  
     9  # Interpolation
    10  
    11  Nomad supports interpreting two classes of variables, node attributes and
    12  runtime environment variables. Node attributes are interpretable in constraints,
    13  task environment variables and certain driver fields. Runtime environment
    14  variables are not interpretable in constraints because they are only defined
    15  once the scheduler has placed them on a particular node.
    16  
    17  The syntax for interpreting variables is `${variable}`. An example and a
    18  comprehensive list of interpretable fields can be seen below:
    19  
    20  ```hcl
    21  task "docs" {
    22    driver = "docker"
    23  
    24    # Drivers support interpreting node attributes and runtime environment
    25    # variables
    26    config {
    27      image = "my-app"
    28  
    29      # Interpret runtime variables to inject the address to bind to and the
    30      # location to write logs to.
    31      args = [
    32        "--bind", "${NOMAD_ADDR_RPC}",
    33        "--logs", "${NOMAD_ALLOC_DIR}/logs",
    34      ]
    35  
    36      port_map {
    37        RPC = 6379
    38      }
    39    }
    40  
    41    # Constraints only support node attributes as runtime environment variables
    42    # are only defined after the task is placed on a node.
    43    constraint {
    44      attribute = "${attr.kernel.name}"
    45      value     = "linux"
    46    }
    47  
    48    # Environment variables are interpreted and can contain both runtime and
    49    # node attributes. There environment variables are passed into the task.
    50    env {
    51      "DC"      = "Running on datacenter ${node.datacenter}"
    52      "VERSION" = "Version ${NOMAD_META_VERSION}"
    53    }
    54  
    55    # Meta keys are also interpretable.
    56    meta {
    57      VERSION = "v0.3"
    58    }
    59  }
    60  ```
    61  
    62  ## Node Variables <a id="interpreted_node_vars"></a>
    63  
    64  Below is a full listing of node attributes that are interpretable. These
    65  attributes are interpreted by __both__ constraints and within the task and
    66  driver.
    67  
    68  <table class="table table-bordered table-striped">
    69    <tr>
    70      <th>Variable</th>
    71      <th>Description</th>
    72      <th>Example Value</th>
    73    </tr>
    74    <tr>
    75      <td><tt>${node.unique.id}</tt></td>
    76      <td>36 character unique client identifier</td>
    77      <td><tt>9afa5da1-8f39-25a2-48dc-ba31fd7c0023</tt></td>
    78    </tr>
    79    <tr>
    80      <td><tt>${node.datacenter}</tt></td>
    81      <td>Client's datacenter</td>
    82      <td><tt>dc1</tt></td>
    83    </tr>
    84    <tr>
    85      <td><tt>${node.unique.name}</tt></td>
    86      <td>Client's name</td>
    87      <td><tt>nomad-client-10-1-2-4</tt></td>
    88    </tr>
    89    <tr>
    90      <td><tt>${node.class}</tt></td>
    91      <td>Client's class</td>
    92      <td><tt>linux-64bit</tt></td>
    93    </tr>
    94    <tr>
    95      <td><tt>${attr.&lt;property&gt;}</tt></td>
    96      <td>Property given by <tt>property</tt> on the client</td>
    97      <td><tt>${attr.cpu.arch} => amd64</tt></td>
    98    </tr>
    99    <tr>
   100      <td><tt>${meta.&lt;key&gt;}</tt></td>
   101      <td>Metadata value given by <tt>key</tt> on the client</td>
   102      <td><tt>${meta.foo} => bar</tt></td>
   103    </tr>
   104  </table>
   105  
   106  Below is a table documenting common node properties:
   107  
   108  <table class="table table-bordered table-striped">
   109    <tr>
   110      <th>Property</th>
   111      <th>Description</th>
   112    </tr>
   113    <tr>
   114      <td><tt>${attr.cpu.arch}</tt></td>
   115      <td>CPU architecture of the client (e.g. <tt>amd64</tt>, <tt>386</tt>)</td>
   116    </tr>
   117    <tr>
   118      <td><tt>${attr.consul.datacenter}</tt></td>
   119      <td>The Consul datacenter of the client (if Consul is found)</td>
   120    </tr>
   121    <tr>
   122      <td><tt>${attr.cpu.numcores}</tt></td>
   123      <td>Number of CPU cores on the client</td>
   124    </tr>
   125    <tr>
   126      <td><tt>${attr.driver.&lt;property&gt;}</tt></td>
   127      <td>See the [task drivers](/docs/drivers/index.html) for property documentation</td>
   128    </tr>
   129    <tr>
   130      <td><tt>${attr.unique.hostname}</tt></td>
   131      <td>Hostname of the client</td>
   132    </tr>
   133    <tr>
   134      <td><tt>${attr.unique.network.ip-address}</tt></td>
   135      <td>The IP address fingerprinted by the client and from which task ports are allocated</td>
   136    </tr>
   137    <tr>
   138      <td><tt>${attr.kernel.name}</tt></td>
   139      <td>Kernel of the client (e.g. <tt>linux</tt>, <tt>darwin</tt>)</td>
   140    </tr>
   141    <tr>
   142      <td><tt>${attr.kernel.version}</tt></td>
   143      <td>Version of the client kernel (e.g. <tt>3.19.0-25-generic</tt>, <tt>15.0.0</tt>)</td>
   144    </tr>
   145    <tr>
   146      <td><tt>${attr.platform.aws.ami-id}</tt></td>
   147      <td>AMI ID of the client (if on AWS EC2)</td>
   148    </tr>
   149    <tr>
   150      <td><tt>${attr.platform.aws.instance-type}</tt></td>
   151      <td>Instance type of the client (if on AWS EC2)</td>
   152    </tr>
   153    <tr>
   154      <td><tt>${attr.os.name}</tt></td>
   155      <td>Operating system of the client (e.g. <tt>ubuntu</tt>, <tt>windows</tt>, <tt>darwin</tt>)</td>
   156    </tr>
   157    <tr>
   158      <td><tt>${attr.os.version}</tt></td>
   159      <td>Version of the client OS</td>
   160    </tr>
   161  </table>
   162  
   163  Here are some examples of using node attributes and properties in a job file:
   164  
   165  ```hcl
   166  job "docs" {
   167    # This will constrain this job to only run on 64-bit clients.
   168    constraint {
   169      attribute = "${attr.cpu.arch}"
   170      value     = "amd64"
   171    }
   172  
   173    # This will restrict the job to only run on clients with 4 or more cores.
   174    # Note: you may also declare a resource requirement for CPU for a task.
   175    constraint {
   176      attribute = "${cpu.numcores}"
   177      operator  = ">="
   178      value     = "4"
   179    }
   180  
   181    # Only run this job on a memory-optimized AWS EC2 instance.
   182    constraint {
   183      attribute = "${attr.platform.aws.instance-type}"
   184      value     = "m4.xlarge"
   185    }
   186  }
   187  ```
   188  
   189  ## Environment Variables <a id="interpreted_env_vars"></a>
   190  
   191  The following are runtime environment variables that describe the environment
   192  the task is running in. These are only defined once the task has been placed on
   193  a particular node and as such can not be used in constraints.
   194  
   195  <table class="table table-bordered table-striped">
   196    <tr>
   197      <th>Variable</th>
   198      <th>Description</th>
   199    </tr>
   200    <tr>
   201      <td><tt>${NOMAD_ALLOC_DIR}</tt></td>
   202      <td>The path to the shared <tt>alloc/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
   203    </tr>
   204    <tr>
   205      <td><tt>${NOMAD_TASK_DIR}</tt></td>
   206      <td>The path to the task <tt>local/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
   207    </tr>
   208    <tr>
   209      <td><tt>${NOMAD_MEMORY_LIMIT}</tt></td>
   210      <td>The memory limit in MBytes for the task</td>
   211    </tr>
   212    <tr>
   213      <td><tt>${NOMAD_CPU_LIMIT}</tt></td>
   214      <td>The CPU limit in MHz for the task</td>
   215    </tr>
   216    <tr>
   217      <td><tt>${NOMAD_ALLOC_ID}</tt></td>
   218      <td>The allocation ID of the task</td>
   219    </tr>
   220    <tr>
   221      <td><tt>${NOMAD_ALLOC_NAME}</tt></td>
   222      <td>The allocation name of the task</td>
   223    </tr>
   224    <tr>
   225      <td><tt>${NOMAD_ALLOC_INDEX}</tt></td>
   226      <td>The allocation index; useful to distinguish instances of task groups. From 0 to (count - 1).</td>
   227    </tr>
   228    <tr>
   229      <td><tt>${NOMAD_TASK_NAME}</tt></td>
   230      <td>The task's name</td>
   231    </tr>
   232    <tr>
   233      <td><tt>${NOMAD_JOB_NAME}</tt></td>
   234      <td>The job's name</td>
   235    </tr>
   236    <tr>
   237      <td><tt>${NOMAD_IP_&lt;label&gt;}</tt></td>
   238      <td>The IP for the given port <tt>label</tt>. See
   239      [here](/docs/job-specification/network.html) for more information.</td>
   240    </tr>
   241    <tr>
   242      <td><tt>${NOMAD_PORT_&lt;label&gt;}</tt></td>
   243      <td>The port for the port <tt>label</tt>. See [here](/docs/job-specification/network.html) for more information.</td>
   244    </tr>
   245    <tr>
   246      <td><tt>${NOMAD_ADDR_&lt;label&gt;}</tt></td>
   247      <td>The <tt>ip:port</tt> pair for the given port <tt>label</tt>. See
   248      [here](/docs/job-specification/network.html) for more information.</td>
   249    </tr>
   250    <tr>
   251      <td><tt>${NOMAD_HOST_PORT_&lt;label&gt;}</tt></td>
   252      <td>The port on the host if port forwarding is being used for the port
   253      <tt>label</tt>. See [here](/docs/job-specification/network.html#mapped_ports) for more
   254      information.</td>
   255    </tr>
   256    <tr>
   257      <td><tt>${NOMAD_META_&lt;key&gt;}</tt></td>
   258      <td>The metadata value given by <tt>key</tt> on the task's metadata</td>
   259    </tr>
   260    <tr>
   261      <td><tt>${"env_key"}</tt></td>
   262      <td>Interpret an environment variable with key <tt>env_key</tt> set on the task.</td>
   263    </tr>
   264  </table>