This commit converts the previous URL for this content to a landing page, which captures all of the previous in-page anchors and directs readers to the new home for each section.
3.1 KiB
layout, page_title
| layout | page_title |
|---|---|
| language | Operators - Configuration Language |
Arithmetic and Logical Operators
An operator is a type of expression that transforms or combines one or more other expressions. Operators either combine two values in some way to produce a third result value, or transform a single given value to produce a single result.
Operators that work on two values place an operator symbol between the two
values, similar to mathematical notation: 1 + 2. Operators that work on
only one value place an operator symbol before that value, like
!true.
The Terraform language has a set of operators for both arithmetic and logic, which are similar to operators in programming languages such as JavaScript or Ruby.
When multiple operators are used together in an expression, they are evaluated in the following order of operations:
!,-(multiplication by-1)*,/,%+,-(subtraction)>,>=,<,<===,!=&&||
Parentheses can be used to override the default order of operations. Without
parentheses, higher levels are evaluated first, so 1 + 2 * 3 is interpreted
as 1 + (2 * 3) and not as (1 + 2) * 3.
The different operators can be gathered into a few different groups with similar behavior, as described below. Each group of operators expects its given values to be of a particular type. Terraform will attempt to convert values to the required type automatically, or will produce an error message if this automatic conversion is not possible.
Arithmetic Operators
The arithmetic operators all expect number values and produce number values as results:
a + breturns the result of addingaandbtogether.a - breturns the result of subtractingbfroma.a * breturns the result of multiplyingaandb.a / breturns the result of dividingabyb.a % breturns the remainder of dividingabyb. This operator is generally useful only when used with whole numbers.-areturns the result of multiplyingaby-1.
Equality Operators
The equality operators both take two values of any type and produce boolean values as results.
a == breturnstrueifaandbboth have the same type and the same value, orfalseotherwise.a != bis the opposite ofa == b.
Comparison Operators
The comparison operators all expect number values and produce boolean values as results.
a < breturnstrueifais less thanb, orfalseotherwise.a <= breturnstrueifais less than or equal tob, orfalseotherwise.a > breturnstrueifais greater thanb, orfalseotherwise.a >= breturnstrueifais greater than or equal tob, orfalseotherwise.
Logical Operators
The logical operators all expect bool values and produce bool values as results.
a || breturnstrueif eitheraorbistrue, orfalseif both arefalse.a && breturnstrueif bothaandbaretrue, orfalseif either one isfalse.!areturnstrueifaisfalse, andfalseifaistrue.