Site icon JD Bots

OpenTofu .tfvars vs .auto.tfvars: Which Files Load Automatically

Card titled .tfvars vs .auto.tfvars, listing terraform.tfvars and sale.auto.tfvars as loaded and winter.tfvars as needing -var-file.

OpenTofu .tfvars vs .auto.tfvars.

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

Syntax

<NAME> = <VALUE>

Terminal

tofu plan -var-file=<FILE>

See Variable Definitions (.tfvars) Files in the OpenTofu docs.

Prerequisites

Step 1: Declare Two Variables

In an empty folder, create variables.tf and outputs.tf:

variables.tf

variable "shop_name" {
  type    = string
  default = "Bookshop"
}

variable "featured_book" {
  type    = string
  default = "Salt and Starlight by Nadia Quill"
}

outputs.tf

output "shop_name" {
  value = var.shop_name
}

output "featured_book" {
  value = var.featured_book
}

Step 2: Add terraform.tfvars and Another .tfvars File

terraform.tfvars

shop_name = "Riverside Bookshop"

winter.tfvars

featured_book = "The Clockmaker's Garden by Mira Kale"

Terminal

tofu plan

Output (the lines that matter)

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

sale.auto.tfvars

featured_book = "A Map of Quiet Places by Tomas Vell"

Terminal

tofu plan

Output (the lines that matter)

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

Terminal

tofu plan -var-file=winter.tfvars

Output (the lines that matter)

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

FileLoadedGood for
terraform.tfvarsAlwaysThe project’s usual values.
*.auto.tfvarsAlways, in alphabetical order, after terraform.tfvarsValues you add or remove as a whole file, such as a sale.
Any other .tfvarsOnly with -var-fileOne set of values per environment, such as winter.tfvars.

Clean Up

Nothing was created. Delete the folder.

Common Mistakes

MistakeWhat happensDo this instead
Naming a file terraform.tfvars.txtIt 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 filesThe 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).

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

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