A new platform for documentation and tutorials is launching soon.
We are migrating Hashicorp documentation into HashiCorp Developer, our new developer experience.
Variables store values that can be used later.
Most notably, a variable assignment of
main is required for all
Sentinel policies. This is the main rule that
is executed to determine the result of a policy. The
main rule may
use other variables that are assigned in the policy.
a = 1 b = a + 1
In this example, two variables are assigned:
a is given
the value of "1" and
b uses the
a to calculate its own value.
The syntax for assigning a variable is:
IDENTIFIER = VALUE
On the left of the equal sign is an identifier. This is a name for your
variable and will be how you reference it later. An identifier is any
combination of letters and digits, but must start with a letter. An
_ is also a valid letter.
On the right is any valid Sentinel value or expression. A value is a literal value such as a number or string. An expression is some computed value such as doing math, calling a function, etc.
»Assignment and Reassignment
A variable is assigned when it is given a value. You can also reassign variables at any time by setting it to a new value. The new value takes effect for any subsequent use of that variable. A variable can be reassigned to a different type.
a = 1 // a = 1 b = a // b = 1 a = "value" // a = "value", b = 1 c = a // c = "value", b = 1
In the above example you can see that the variables are set and reassigned.
Notice that the value of a variable is the current value, and that reassigning
a variable only affects future uses of that variable. You can see this with
b being different values.
Using a variable that is unassigned is an error.
In the example below, the first line would result in the policy erroring.
Sentinel is executed top-down and the value of
c is not available yet
on the first line.
a = c // Error! c = 1
While a topic more related to values, variables can be assigned (or-reassigned) a different value type via type conversion.
s = "1.1" a = int(s) // a = 1 a = float(s) // a = 1.1 a = 1 s = string(a) // s = "1"