github.com/maier/nomad@v0.4.1-0.20161110003312-a9e3d0b8549d/website/source/docs/job-specification/constraint.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "constraint Stanza - Job Specification" 4 sidebar_current: "docs-job-specification-constraint" 5 description: |- 6 The "constraint" stanza allows restricting the set of eligible nodes. 7 Constraints may filter on attributes or metadata. Additionally constraints may 8 be specified at the job, group, or task levels for ultimate flexibility. 9 --- 10 11 # `constraint` Stanza 12 13 <table class="table table-bordered table-striped"> 14 <tr> 15 <th width="120">Placement</th> 16 <td> 17 <code>job -> **constraint**</code> 18 <br> 19 <code>job -> group -> **constraint**</code> 20 <br> 21 <code>job -> group -> task -> **constraint**</code> 22 </td> 23 </tr> 24 </table> 25 26 The `constraint` allows restricting the set of eligible nodes. Constraints may 27 filter on [attributes][interpolation] or [metadata][meta]. Additionally 28 constraints may be specified at the [job][job], [group][group], or [task][task] 29 levels for ultimate flexibility. 30 31 ```hcl 32 job "docs" { 33 # All tasks in this job must run on linux. 34 constraint { 35 attribute = "${attr.kernel.name}" 36 value = "linux" 37 } 38 39 group "example" { 40 # All groups in this job should be scheduled on different hosts. 41 constraint { 42 operator = "distinct_hosts" 43 value = "true" 44 } 45 46 task "server" { 47 # All tasks must run where "my_custom_value" is greater than 3. 48 constraint { 49 attribute = "${meta.my_custom_value}" 50 operator = ">" 51 value = "3" 52 } 53 } 54 } 55 } 56 ``` 57 58 Placing constraints at both the job level and at the group level is redundant 59 since constraints are applied hierarchically. The job constraints will affect 60 all groups (and tasks) in the job. 61 62 ## `constraint` Parameters 63 64 - `attribute` `(string: "")` - Specifies the name or reference of the attribute 65 to examine for the constraint. This can be any of the [Nomad interpolated 66 values](/docs/runtime/interpolation.html#interpreted_node_vars). 67 68 - `operator` `(string: "=")` - Specifies the comparison operator.The ordering is 69 compared lexically. Possible values include: 70 71 ```text 72 = 73 != 74 > 75 >= 76 < 77 <= 78 regexp 79 set_contains 80 version 81 ``` 82 83 For a detailed explanation of these values and their behavior, please see 84 the [operator values section](#operator-values). 85 86 - `value` `(string: "")` - Specifies the value to compare the attribute against 87 using the specified operation. This can be a literal value, another attribute, 88 or any [Nomad interpolated 89 values](/docs/runtime/interpolation.html#interpreted_node_vars). 90 91 ### `operator` Values 92 93 This section details the specific values for the "operator" parameter in the 94 Nomad job specification for constraints. The operator is always specified as a 95 string, but the string can take on different values which change the behavior of 96 the overall constraint evaluation. 97 98 ```hcl 99 constraint { 100 operator = "..." 101 } 102 ``` 103 104 - `"distinct_hosts"` - Instructs the scheduler to not co-locate any groups on 105 the same machine. When specified as a job constraint, it applies to all groups 106 in the job. When specified as a group constraint, the effect is constrained to 107 that group. Note that the `attribute` parameter should be omitted when using 108 this constraint. 109 110 ```hcl 111 constraint { 112 operator = "distinct_hosts" 113 value = "true" 114 } 115 ``` 116 117 - `"regexp"` - Specifies a regular expression constraint against the attribute. 118 The syntax of the regular expressions accepted is the same general syntax used 119 by Perl, Python, and many other languages. More precisely, it is the syntax 120 accepted by RE2 and described at in the [Google RE2 121 syntax](https://golang.org/s/re2syntax). 122 123 ```hcl 124 constraint { 125 attribute = "..." 126 operator = "regexp" 127 value = "[a-z0-9]" 128 } 129 ``` 130 131 - `"set_contains"` - Specifies a contains constraint against the attribute. The 132 attribute and the list being checked are split using commas. This will check 133 that the given attribute contains **all** of the specified elements. 134 135 ```hcl 136 constraint { 137 attribute = "..." 138 operator = "set_contains" 139 value = "a,b,c" 140 } 141 ``` 142 143 - `"version"` - Specifies a version constraint against the attribute. This 144 supports a comma-separated list of constraints, including the pessimistic 145 operator. For more examples please see the [go-version 146 repository](https://github.com/hashicorp/go-version) for more specific 147 examples. 148 149 ```hcl 150 constraint { 151 attribute = "..." 152 operator = "version" 153 value = ">= 0.1.0, < 0.2" 154 } 155 ``` 156 157 ## `constraint` Examples 158 159 The following examples only show the `constraint` stanzas. Remember that the 160 `constraint` stanza is only valid in the placements listed above. 161 162 ### Kernel Data 163 164 This example restricts the task to running on nodes which have a kernel version 165 higher than "3.19". 166 167 ```hcl 168 constraint { 169 attribute = "${attr.kernel.version}" 170 operator = "version" 171 value = "> 3.19" 172 } 173 ``` 174 175 ### Operating Systems 176 177 This example restricts the task to running on nodes that are running Ubuntu 178 14.04 179 180 ```hcl 181 constraint { 182 attribute = "${attr.os.name}" 183 value = "ubuntu" 184 } 185 186 constraint { 187 attribute = "${attr.os.version}" 188 value = "14.04" 189 } 190 ``` 191 192 ### Cloud Metadata 193 194 When possible, Nomad populates node attributes from the cloud environment. These 195 values are accessible as filters in constraints. This example constrains this 196 task to only run on nodes that are memory-optimized on AWS. 197 198 ```hcl 199 constraint { 200 attribute = "${attr.platform.aws.instance-type}" 201 value = "m4.xlarge" 202 } 203 ``` 204 205 ### User-Specified Metadata 206 207 This example restricts the task to running on nodes where the binaries for 208 redis, cypress, and nginx are all cached locally. This particular example is 209 utilizing node [metadata][meta]. 210 211 ```hcl 212 constraint { 213 attribute = "${node.meta.cached_binaries}" 214 set_contains = "redis,cypress,nginx" 215 } 216 ``` 217 218 [job]: /docs/job-specification/job.html "Nomad job Job Specification" 219 [group]: /docs/job-specification/group.html "Nomad group Job Specification" 220 [meta]: /docs/job-specification/meta.html "Nomad meta Job Specification" 221 [task]: /docs/job-specification/task.html "Nomad task Job Specification" 222 [interpolation]: /docs/runtime/interpolation.html "Nomad interpolation"