OpenTofu: A Beginners Guide to a Terraform Fork Including Migration From Terraform

OpenTofu Functions and Conditional Expressions

Conditional Expressions

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:

$ tofu console
> 1 + 2
3
> 10 / 4
2.5
> 7 * (3 - 1)
14

Equality & Inequality Operators

Equality and inequality comparisons return a Boolean. Use them to compare values of any type:

$ tofu console
> 8 == 8
true
> 8 != "8"
true
> 8 == "8"
false

Note

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:

OperatorDescriptionExample
>Greater than5 > 4 → true
>=Greater than or equal to5 >= 5 → true
<Less than3 < 4 → true
<=Less than or equal to3 <= 3 → true
$ tofu console
> 5 > 7
false
> 4 <= 5
true

Logical Operators & NOT

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

$ tofu console
> (8 > 7) && (8 < 10)
true
> (8 > 10) || (8 < 10)
true
> !(8 == 8)
false

Defining Variables and Testing Expressions

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

variable "a" {
  type    = number
  default = 50
}

variable "b" {
  type    = number
  default = 25
}
$ tofu console
> var.a > var.b
true
> var.a + var.b
75

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:

variable "length" {
  type        = number
  description = "Desired password length (minimum 8)"
}

resource "random_password" "password_generator" {
  length = var.length < 8 ? 8 : var.length
}

output "password" {
  value     = random_password.password_generator.result
  sensitive = true
}

Warning

Always enforce a minimum length for generated passwords to maintain security standards.

Initialize, plan, and apply:

$ tofu init
$ tofu plan -var='length=5'
$ tofu apply -var='length=5' -auto-approve

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

Watch video content

Previous
Functions Operators and Conditional Expressions