Skip to main content
0 reactions

How to Use tofu output -raw and -json in OpenTofu

Card titled tofu output -raw and -json, showing a terminal with three ticked commands: tofu output, tofu output -raw and tofu output -json.
How to Use tofu output -raw and -json in OpenTofu.

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

TerminalBash
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 for NAME.
  • -show-sensitive: show values marked sensitive. -raw and -json always show them.

The full list of options is in Command: output (opens in a new tab) in the OpenTofu docs.

Prerequisites

Step 1: Save Three Outputs

In an empty folder, create outputs.tf:

outputs.tfHCL
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:

TerminalBash
tofu apply

Step 2: Print All Outputs

TerminalBash
tofu output
OutputPlain text
genres = [
  "Fiction",
  "Travel",
  "Poetry",
]
host_port = 8080
website_url = "http://localhost:8080"

Step 3: Print One Output

TerminalBash
tofu output website_url
OutputPlain text
"http://localhost:8080"

The value keeps its quotes, as OpenTofu writes text.

Step 4: Print It as Plain Text with -raw

TerminalBash
tofu output -raw website_url
OutputPlain text
http://localhost:8080

No quotes and no line break, so a script can use the value as it is. -raw handles only single values:

TerminalBash
tofu output -raw genres
OutputPlain text
╷
│ 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

TerminalBash
tofu output -json
OutputJSON
{
  "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:

TerminalBash
tofu output -json genres
OutputJSON
["Fiction","Travel","Poetry"]

Clean Up

Run tofu destroy and type yes to remove the saved outputs, then delete the folder.

Common Mistakes

MistakeWhat happensDo this instead
Running tofu output before tofu applyThere are no saved outputs yet.Apply first. A plan doesn’t save outputs.
Reading the human format in a scriptThe 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.

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