A conditional expression chooses between two values: if a condition is true it gives the first, otherwise the second. It’s how an OpenTofu® configuration says “this, or else that” in one line.
This guide picks a shop’s opening hours depending on whether it’s the weekend.
Syntax
Syntax
<CONDITION> ? <TRUE_VALUE> : <FALSE_VALUE>
<CONDITION>: any expression that givestrueorfalse, such as a bool variable.<TRUE_VALUE>: the result when the condition istrue.<FALSE_VALUE>: the result when it’sfalse. Both results must have the same type.
The rules are in Conditional Expressions in the OpenTofu docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu in the OpenTofu docs covers every system.
Step 1: Write the Conditional
In an empty folder, create main.tf with a bool variable and an output that uses it:
main.tf
variable "is_weekend" {
description = "Whether today is Saturday or Sunday."
type = bool
default = false
}
output "opening_hours" {
value = var.is_weekend ? "10:00 to 16:00" : "09:00 to 18:00"
}
Step 2: Apply with the Default
This configuration uses no providers, so there’s nothing for tofu init to download: tofu apply works straight away. Run it and type yes:
Terminal
tofu apply
Output (the lines that matter)
Outputs:
opening_hours = "09:00 to 18:00"
is_weekend is false, so the output is the second value.
Step 3: Flip the Condition
Terminal
tofu apply -var is_weekend=true
Output (the lines that matter)
Outputs:
opening_hours = "10:00 to 16:00"
Same file, the other value.
Keep Both Results the Same Type
OpenTofu has to know what type the whole expression gives before it knows the condition, so both results must match. Here, one result is a list and the other is text:
main.tf
variable "is_weekend" {
type = bool
default = false
}
output "opening_hours" {
value = var.is_weekend ? ["10:00", "16:00"] : "09:00 to 18:00"
}
Terminal
tofu plan
Output
╷
│ Error: Inconsistent conditional result types
│
│ on main.tf line 7, in output "opening_hours":
│ 7: value = var.is_weekend ? ["10:00", "16:00"] : "09:00 to 18:00"
│ ├────────────────
│ │ var.is_weekend is a bool
│
│ The true and false result expressions must have consistent types. The 'true' value is
│ tuple, but the 'false' value is string.
╵
Make both results text, or both lists. A number and a text value are fine together: the docs say OpenTofu converts the number to text.
Clean Up
This example created nothing but a state file. Delete the folder when you’re done.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing the condition as text, such as "true" ? 1 : 2 | OpenTofu converts "true" to a bool, which works, but other text fails. | Use a bool, or a comparison that gives one. |
| Nesting several conditionals in one line | The expression gets hard to read. | Move parts into local values with clear names. |
FAQ
Is there an if statement in OpenTofu?
Not as a statement. You choose values with a conditional expression, and inside text with the %{if} directive of a string template.
Can the condition compare two values?
Yes. The docs say it’s usually an expression with the equality, comparison or logical operators, as long as it gives a bool.
Conclusion
<CONDITION> ? <TRUE_VALUE> : <FALSE_VALUE> picks one of two values in a single line. Keep both results the same type, and give long conditions a name with a local value.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- How to Use for Expressions in OpenTofu
- OpenTofu String Templates and Heredocs Explained
Additional Resources
OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.
