Site icon JD Bots

How to Use the alltrue Function in OpenTofu

Card titled The alltrue Function, labelled OpenTofu Functions, showing alltrue([true, false]) and the result false.

Check that every value in an OpenTofu list is true with alltrue, and see what an empty list returns

alltrue checks a whole list at once: it returns true only when every item is true. Combined with a for expression, it checks one rule for every item, such as “every name has text”.

Syntax

Syntax

alltrue(<LIST>)

The function is described in alltrue Function in the OpenTofu® docs.

Prerequisites

Step 1: Try It in tofu console

Open a terminal in any empty folder and start tofu console, which evaluates expressions as you type them:

Terminal

tofu console

Type each expression to see the result:

tofu console

> alltrue([true, true, true])
true
> alltrue([true, false])
false
> alltrue([])
true
> alltrue(["true", true])
true
> exit

Clean Up

Type exit to leave the console. The example creates nothing, so there’s nothing else to remove.

Common Mistakes

MistakeWhat happensDo this instead
Expecting alltrue([]) to be falseAn empty list returns true: there’s no item that isn’t true.Check the list’s length separately when an empty list isn’t allowed.
Using it to check whether any item is trueIt needs all of them.Use anytrue for “at least one”.

FAQ

What can the list contain?

Bool values, or the text "true", as the docs say. Anything else counts as not true.

Where is alltrue used?

Often in a variable’s validation block, with a for expression that turns each item into a true or false.

Conclusion

alltrue(<LIST>) is true when every item is true, and also when the list is empty.

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.

Exit mobile version