OpenTofu® reads variable values from .tfvars files, but not from every one. terraform.tfvars and any file ending in .auto.tfvars load by themselves; every other .tfvars file loads only when you name it with -var-file.
This guide puts all three kinds of file in one folder and shows which values OpenTofu uses.
Syntax
<NAME> = <VALUE>
- A
.tfvarsfile holds only these assignments, one per variable.<VALUE>is written as in a.tffile: text in quotes, numbers as they are. - Files ending in
.tfvars.jsonhold the same values as a JSON object.
tofu plan -var-file=<FILE>
-var-file=<FILE>loads one more file. Repeat it to load several.
See Variable Definitions (.tfvars) Files (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: Declare Two Variables
In an empty folder, create variables.tf and outputs.tf:
variable "shop_name" {
type = string
default = "Bookshop"
}
variable "featured_book" {
type = string
default = "Salt and Starlight by Nadia Quill"
}
output "shop_name" {
value = var.shop_name
}
output "featured_book" {
value = var.featured_book
}
Step 2: Add terraform.tfvars and Another .tfvars File
shop_name = "Riverside Bookshop"
featured_book = "The Clockmaker's Garden by Mira Kale"
tofu plan
Changes to Outputs:
+ featured_book = "Salt and Starlight by Nadia Quill"
+ shop_name = "Riverside Bookshop"
You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.
shop_name came from terraform.tfvars. winter.tfvars was ignored, so featured_book kept its default.
Step 3: Add an .auto.tfvars File
featured_book = "A Map of Quiet Places by Tomas Vell"
tofu plan
Changes to Outputs:
+ featured_book = "A Map of Quiet Places by Tomas Vell"
+ shop_name = "Riverside Bookshop"
You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.
OpenTofu loaded sale.auto.tfvars without being told to.
Step 4: Load the Other File with -var-file
tofu plan -var-file=winter.tfvars
Changes to Outputs:
+ featured_book = "The Clockmaker's Garden by Mira Kale"
+ shop_name = "Riverside Bookshop"
You can apply this plan to save these new output values to the OpenTofu state, without
changing any real infrastructure.
A file named on the command line is read last, so its value wins over sale.auto.tfvars.
Which File to Use
| File | Loaded | Good for |
|---|---|---|
terraform.tfvars | Always | The project’s usual values. |
*.auto.tfvars | Always, in alphabetical order, after terraform.tfvars | Values you add or remove as a whole file, such as a sale. |
Any other .tfvars | Only with -var-file | One set of values per environment, such as winter.tfvars. |
Clean Up
Nothing was created. Delete the folder.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Naming a file terraform.tfvars.txt | It isn’t a .tfvars file, so OpenTofu never reads it. | Check the full file name, including the ending your editor may hide. |
Setting the same variable in two .auto.tfvars files | The file whose name comes last alphabetically wins, which is easy to miss. | Set each variable in one file. |
FAQ
What’s the full order?
OpenTofu reads environment variables, terraform.tfvars, terraform.tfvars.json, the .auto.tfvars files, and then -var and -var-file in the order you give them. The last value wins (Variable Definition Precedence (opens in a new tab)).
Do .tfvars files work for modules?
No. They set the root module’s variables. A module gets its values from the module block that calls it.
Conclusion
Put everyday values in terraform.tfvars, add .auto.tfvars files for values that should always load, and pass any other file with -var-file.
Related
- OpenTofu Tutorial — Part 4: Variables, tfvars and Outputs
- How to Use Input Variables in OpenTofu
- OpenTofu Required vs Optional Variables
Additional Resources
- Variable Definitions (.tfvars) Files (opens in a new tab)
- Variable Definition Precedence (opens in a new tab)
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