github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/website/source/docs/runtime/interpolation.html.md.erb (about) 1 --- 2 layout: "docs" 3 page_title: "Variable Interpolation" 4 sidebar_current: "docs-variable-interpolation" 5 description: |- 6 Learn about the Nomad's interpolation and interpreted variables. 7 --- 8 9 # Variable 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.<property>}</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.<key>}</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.cpu.numcores}</tt></td> 119 <td>Number of CPU cores on the client</td> 120 </tr> 121 <tr> 122 <td><tt>${attr.cpu.totalcompute}</tt></td> 123 <td> 124 <tt>cpu.frequency × cpu.numcores</tt> but may be overridden by <tt>client.cpu_total_compute</tt> 125 </td> 126 </tr> 127 <tr> 128 <td><tt>${attr.consul.datacenter}</tt></td> 129 <td>The Consul datacenter of the client (if Consul is found)</td> 130 </tr> 131 <tr> 132 <td><tt>${attr.driver.<property>}</tt></td> 133 <td>See the [task drivers](/docs/drivers/index.html) for property documentation</td> 134 </tr> 135 <tr> 136 <td><tt>${attr.unique.hostname}</tt></td> 137 <td>Hostname of the client</td> 138 </tr> 139 <tr> 140 <td><tt>${attr.unique.network.ip-address}</tt></td> 141 <td>The IP address fingerprinted by the client and from which task ports are allocated</td> 142 </tr> 143 <tr> 144 <td><tt>${attr.kernel.name}</tt></td> 145 <td>Kernel of the client (e.g. <tt>linux</tt>, <tt>darwin</tt>)</td> 146 </tr> 147 <tr> 148 <td><tt>${attr.kernel.version}</tt></td> 149 <td>Version of the client kernel (e.g. <tt>3.19.0-25-generic</tt>, <tt>15.0.0</tt>)</td> 150 </tr> 151 <tr> 152 <td><tt>${attr.platform.aws.ami-id}</tt></td> 153 <td>AMI ID of the client (if on AWS EC2)</td> 154 </tr> 155 <tr> 156 <td><tt>${attr.platform.aws.instance-type}</tt></td> 157 <td>Instance type of the client (if on AWS EC2)</td> 158 </tr> 159 <tr> 160 <td><tt>${attr.os.name}</tt></td> 161 <td>Operating system of the client (e.g. <tt>ubuntu</tt>, <tt>windows</tt>, <tt>darwin</tt>)</td> 162 </tr> 163 <tr> 164 <td><tt>${attr.os.version}</tt></td> 165 <td>Version of the client OS</td> 166 </tr> 167 </table> 168 169 Here are some examples of using node attributes and properties in a job file: 170 171 ```hcl 172 job "docs" { 173 # This will constrain this job to only run on 64-bit clients. 174 constraint { 175 attribute = "${attr.cpu.arch}" 176 value = "amd64" 177 } 178 179 # This will restrict the job to only run on clients with 4 or more cores. 180 # Note: you may also declare a resource requirement for CPU for a task. 181 constraint { 182 attribute = "${cpu.numcores}" 183 operator = ">=" 184 value = "4" 185 } 186 187 # Only run this job on a memory-optimized AWS EC2 instance. 188 constraint { 189 attribute = "${attr.platform.aws.instance-type}" 190 value = "m4.xlarge" 191 } 192 } 193 ``` 194 195 ## Environment Variables <a id="interpreted_env_vars"></a> 196 197 The following are runtime environment variables that describe the environment 198 the task is running in. These are only defined once the task has been placed on 199 a particular node and as such can not be used in constraints. 200 201 Environment variables should be enclosed in brackets `${...}` for 202 interpolation. 203 204 ### Dots in Variables <a id="dots_in_vars"></a> 205 206 Starting in Nomad 0.9, task configuration interpolation requires variables to 207 be valid identifiers. While this does not affect default variables or common 208 custom variables, it is possible to define a variable that is not a valid 209 identifier: 210 211 ```hcl 212 env { 213 "valid.name" = "ok" 214 "invalid...name" = "not a valid identifier" 215 } 216 ``` 217 218 The environment variable `invalid...name` cannot be interpolated using the 219 standard `"${invalid...name}"` syntax. The dots will be interpreted as object 220 notation so multiple consecutive dots are invalid. 221 222 To continue supporting all user environment variables Nomad 0.9 added a new 223 `env` variable which allows accessing any environment variable through index 224 syntax: 225 226 ```hcl 227 task "redis" { 228 driver = "docker" 229 config { 230 image = "redis:3.2" 231 labels { 232 label1 = "${env["invalid...name"]}" 233 label2 = "${env["valid.name"]}" 234 } 235 } 236 } 237 ``` 238 239 <%= partial "envvars.html.md" %>