tofu output prints the output values that OpenTofu® saved at the last apply. With -raw it prints one value as plain text for a script, and with -json it prints values in a format other tools read.
This guide saves three outputs and reads them every way the command offers.
Syntax
tofu output [options] [NAME]
[NAME]: one output to print. Leave it out to print them all.-raw: print one string, number or boolean as it is, without quotes or a line break.-json: print JSON, for all outputs or forNAME.-show-sensitive: show values marked sensitive.-rawand-jsonalways show them.
The full list of options is in Command: output (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: Save Three Outputs
In an empty folder, create outputs.tf:
output "website_url" {
value = "http://localhost:8080"
}
output "host_port" {
value = 8080
}
output "genres" {
value = ["Fiction", "Travel", "Poetry"]
}
Apply and type yes. OpenTofu saves the three values in its state:
tofu apply
Step 2: Print All Outputs
tofu output
genres = [
"Fiction",
"Travel",
"Poetry",
]
host_port = 8080
website_url = "http://localhost:8080"
Step 3: Print One Output
tofu output website_url
"http://localhost:8080"
The value keeps its quotes, as OpenTofu writes text.
Step 4: Print It as Plain Text with -raw
tofu output -raw website_url
http://localhost:8080
No quotes and no line break, so a script can use the value as it is. -raw handles only single values:
tofu output -raw genres
╷
│ Error: Unsupported value for raw output
│
│ The -raw option only supports strings, numbers, and boolean values, but output value
│ "genres" is tuple.
│
│ Use the -json option for machine-readable representations of output values that have
│ complex types.
╵
Step 5: Print JSON with -json
tofu output -json
{
"genres": {
"sensitive": false,
"type": [
"tuple",
[
"string",
"string",
"string"
]
],
"value": [
"Fiction",
"Travel",
"Poetry"
]
},
"host_port": {
"sensitive": false,
"type": "number",
"value": 8080
},
"website_url": {
"sensitive": false,
"type": "string",
"value": "http://localhost:8080"
}
}
Each output has its value, its type and whether it’s sensitive. With a name, you get just the value:
tofu output -json genres
["Fiction","Travel","Poetry"]
Clean Up
Run tofu destroy and type yes to remove the saved outputs, then delete the folder.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running tofu output before tofu apply | There are no saved outputs yet. | Apply first. A plan doesn’t save outputs. |
| Reading the human format in a script | The docs say it can change between versions. | Use -json or -raw in scripts. |
FAQ
Why is genres a tuple?
A list written in brackets, without a type, is a tuple in OpenTofu. A variable with type = list(string) gives a list.
Does -json reveal sensitive values?
Yes. The docs warn that -json and -raw print sensitive values in plain text.
Conclusion
Use tofu output to read values, -raw to hand one value to a script, and -json for lists, maps and other tools.
Related
- OpenTofu Tutorial — Part 4: Variables, tfvars and Outputs
- How to Use Output Values in OpenTofu
- How to Use Sensitive Variables in OpenTofu
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