Skip to main content
Explore how to use arithmetic, comparison, equality, logical, and conditional operators in OpenTofu configurations and the interactive console.

Table of Contents


Interactive Arithmetic in the Console

You can quickly evaluate math expressions in the OpenTofu console:

Equality & Inequality Operators

Equality and inequality comparisons return a Boolean. Use them to compare values of any type:
The operators == and != are type-sensitive. A number and a string holding the same digits are considered unequal.

Comparison Operators

Numeric comparisons also yield Boolean results. Here’s a quick reference:

Logical Operators & NOT

Combine Boolean expressions using AND (&&) and OR (||), and invert them with NOT (!):

Defining Variables and Testing Expressions

You can declare variables in HCL and reference them in the console:

Ternary (Conditional) Expressions in Configurations

Use the ternary operator (condition ? true_val : false_val) to enforce defaults. For example, generate a random password whose length is at least 8 characters:
Always enforce a minimum length for generated passwords to maintain security standards.
Initialize, plan, and apply:
Even if you pass length=5, OpenTofu generates an 8-character password. Passing a higher value (e.g., 12) yields a 12-character result.

References

Watch Video