github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/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.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>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>consul.datacenter</tt></td>
   119      <td>The Consul datacenter of the client (if Consul is found)</td>
   120    </tr>
   121    <tr>
   122      <td><tt>cpu.numcores</tt></td>
   123      <td>Number of CPU cores on the client</td>
   124    </tr>
   125    <tr>
   126      <td><tt>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>unique.hostname</tt></td>
   131      <td>Hostname of the client</td>
   132    </tr>
   133    <tr>
   134      <td><tt>kernel.name</tt></td>
   135      <td>Kernel of the client (e.g. <tt>linux</tt>, <tt>darwin</tt>)</td>
   136    </tr>
   137    <tr>
   138      <td><tt>kernel.version</tt></td>
   139      <td>Version of the client kernel (e.g. <tt>3.19.0-25-generic</tt>, <tt>15.0.0</tt>)</td>
   140    </tr>
   141    <tr>
   142      <td><tt>platform.aws.ami-id</tt></td>
   143      <td>AMI ID of the client (if on AWS EC2)</td>
   144    </tr>
   145    <tr>
   146      <td><tt>platform.aws.instance-type</tt></td>
   147      <td>Instance type of the client (if on AWS EC2)</td>
   148    </tr>
   149    <tr>
   150      <td><tt>os.name</tt></td>
   151      <td>Operating system of the client (e.g. <tt>ubuntu</tt>, <tt>windows</tt>, <tt>darwin</tt>)</td>
   152    </tr>
   153    <tr>
   154      <td><tt>os.version</tt></td>
   155      <td>Version of the client OS</td>
   156    </tr>
   157  </table>
   158  
   159  Here are some examples of using node attributes and properties in a job file:
   160  
   161  ```hcl
   162  job "docs" {
   163    # This will constrain this job to only run on 64-bit clients.
   164    constraint {
   165      attribute = "${attr.arch}"
   166      value     = "amd64"
   167    }
   168  
   169    # This will restrict the job to only run on clients with 4 or more cores.
   170    # Note: you may also declare a resource requirement for CPU for a task.
   171    constraint {
   172      attribute = "${cpu.numcores}"
   173      operator  = ">="
   174      value     = "4"
   175    }
   176  
   177    # Only run this job on a memory-optimized AWS EC2 instance.
   178    constraint {
   179      attribute = "${attr.platform.aws.instance-type}"
   180      value     = "m4.xlarge"
   181    }
   182  }
   183  ```
   184  
   185  ## Environment Variables <a id="interpreted_env_vars"></a>
   186  
   187  The following are runtime environment variables that describe the environment
   188  the task is running in. These are only defined once the task has been placed on
   189  a particular node and as such can not be used in constraints.
   190  
   191  <table class="table table-bordered table-striped">
   192    <tr>
   193      <th>Variable</th>
   194      <th>Description</th>
   195    </tr>
   196    <tr>
   197      <td><tt>${NOMAD_ALLOC_DIR}</tt></td>
   198      <td>The path to the shared <tt>alloc/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
   199    </tr>
   200    <tr>
   201      <td><tt>${NOMAD_TASK_DIR}</tt></td>
   202      <td>The path to the task <tt>local/</tt> directory. See [here](/docs/runtime/environment.html#task-directories) for more information.</td>
   203    </tr>
   204    <tr>
   205      <td><tt>${NOMAD_MEMORY_LIMIT}</tt></td>
   206      <td>The memory limit in MBytes for the task</td>
   207    </tr>
   208    <tr>
   209      <td><tt>${NOMAD_CPU_LIMIT}</tt></td>
   210      <td>The CPU limit in MHz for the task</td>
   211    </tr>
   212    <tr>
   213      <td><tt>${NOMAD_ALLOC_ID}</tt></td>
   214      <td>The allocation ID of the task</td>
   215    </tr>
   216    <tr>
   217      <td><tt>${NOMAD_ALLOC_NAME}</tt></td>
   218      <td>The allocation name of the task</td>
   219    </tr>
   220    <tr>
   221      <td><tt>${NOMAD_ALLOC_INDEX}</tt></td>
   222      <td>The allocation index; useful to distinguish instances of task groups</td>
   223    </tr>
   224    <tr>
   225      <td><tt>${NOMAD_TASK_NAME}</tt></td>
   226      <td>The task's name</td>
   227    </tr>
   228    <tr>
   229      <td><tt>${NOMAD_JOB_NAME}</tt></td>
   230      <td>The job's name</td>
   231    </tr>
   232    <tr>
   233      <td><tt>${NOMAD_IP_&lt;label&gt;}</tt></td>
   234      <td>The IP for the given port <tt>label</tt>. See
   235      [here](/docs/job-specification/network.html) for more information.</td>
   236    </tr>
   237    <tr>
   238      <td><tt>${NOMAD_PORT_&lt;label&gt;}</tt></td>
   239      <td>The port for the port <tt>label</tt>. See [here](/docs/job-specification/network.html) for more information.</td>
   240    </tr>
   241    <tr>
   242      <td><tt>${NOMAD_ADDR_&lt;label&gt;}</tt></td>
   243      <td>The <tt>ip:port</tt> pair for the given port <tt>label</tt>. See
   244      [here](/docs/job-specification/network.html) for more information.</td>
   245    </tr>
   246    <tr>
   247      <td><tt>${NOMAD_HOST_PORT_&lt;label&gt;}</tt></td>
   248      <td>The port on the host if port forwarding is being used for the port
   249      <tt>label</tt>. See [here](/docs/job-specification/network.html#mapped_ports) for more
   250      information.</td>
   251    </tr>
   252    <tr>
   253      <td><tt>${NOMAD_META_&lt;key&gt;}</tt></td>
   254      <td>The metadata value given by <tt>key</tt> on the task's metadata</td>
   255    </tr>
   256    <tr>
   257      <td><tt>${"env_key"}</tt></td>
   258      <td>Interpret an environment variable with key <tt>env_key</tt> set on the task.</td>
   259    </tr>
   260  </table>