A splat expression takes the same attribute from every item of a list in one short step. Instead of writing a for expression to get every user’s name, you write var.users[*].name.
This guide takes the names and roles from a list of team members.
Syntax
<LIST>[*].<ATTRIBUTE>
<LIST>: a list, set or tuple, such as a variable or a resource created withcount.[*]: the splat operator. It goes through every item of the list.<ATTRIBUTE>: what to take from each item. It can continue, as in[*].address.city.
It’s the same as [for item in <LIST> : item.<ATTRIBUTE>]. The rules are in Splat Expressions (opens in a new tab) in the OpenTofu® docs.
Prerequisites
- OpenTofu installed. Installing OpenTofu (opens in a new tab) in the OpenTofu docs covers every system.
Step 1: Write the Splat Expressions
In an empty folder, create main.tf with a list of users and three outputs. The last one does with a for expression what the first does with a splat:
variable "users" {
description = "Team members and their roles."
type = list(object({
name = string
role = string
}))
default = [
{ name = "ana", role = "admin" },
{ name = "ben", role = "editor" },
{ name = "cai", role = "viewer" },
]
}
output "names" {
value = var.users[*].name
}
output "roles" {
value = var.users[*].role
}
output "names_with_for" {
value = [for user in var.users : user.name]
}
Step 2: Apply It
This configuration uses no providers, so there’s nothing for tofu init to download: tofu apply works straight away. Run it and type yes:
tofu apply
Outputs:
names = tolist([
"ana",
"ben",
"cai",
])
names_with_for = [
"ana",
"ben",
"cai",
]
roles = tolist([
"admin",
"editor",
"viewer",
])
names and names_with_for hold the same three names. The splat result is printed as tolist([…]) because it keeps the list type of var.users; the for expression makes a tuple, printed as […]. You use them the same way.
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 |
|---|---|---|
Using a splat on a map, or on a resource created with for_each | It doesn’t go through the items: the docs say splats work only on lists, sets and tuples. | Use a for expression. |
Writing the old form, var.users.*.name | It still works but behaves differently with indexes, and the docs advise against it. | Use [*]. |
FAQ
What does a splat do with a single value?
The docs say it turns a value that isn’t a list into a one-item tuple, and null into an empty one. They advise using that only where a list is clearly expected, such as for_each.
Can I filter with a splat?
No. To keep only some items, use a for expression with if.
Conclusion
<LIST>[*].<ATTRIBUTE> is the short way to take one attribute from every item of a list. For maps, or to filter, use a for expression.
Related
- OpenTofu Tutorial — Part 5: Locals, Expressions and tofu console
- How to Use for Expressions in OpenTofu
- OpenTofu Variable Types 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.
0 comments