Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

tofu

OpenTofu: the open-source fork of Terraform. Same HCL code and the same workflow — tofu init, plan, apply.

Niveau
DevOps-tool
Groep
Infrastructuur als code en cloud
Root nodig
Nee
Pakket
opentofu
Ook
opentofu

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

OpenTofu started in 2023 when Terraform changed its licence (BSL). It is run by the Linux Foundation and stays open source (MPL). Most Terraform code and providers work without changes: replace terraform with tofu.

Extra features: state encryption built in, early variable evaluation, and its own registry (registry.opentofu.org).

Zo schrijf je het

tofu COMMAND [OPTIONS]

Belangrijkste delen

Commands

init
Download providers and modules, set up the backend.
fmt / validate
Format code / check syntax.
plan [-out FILE]
Show the changes.
apply [FILE]
Make the changes.
destroy
Delete everything in the state.
output [NAME]
Show outputs.
state list|show|mv|rm
Work with the state.
import ADDR ID
Take an existing resource into the state.
workspace list|new|select
Several states for one config.
test
Run .tftest.hcl tests.
providers lock
Write provider checksums for all platforms.

Opties 9

Meest gebruikt

Options

-var 'k=v' / -var-file FILE
Input variables.
-out FILE
Save the plan (apply exactly this plan).
-auto-approve
Apply without a question (CI only).
-target ADDR
Only this resource (for emergencies).
-refresh-only
Only read the real world (find drift).
-upgrade
Upgrade providers in init.
-lock-timeout 60s
Wait for the state lock.
-json / -no-color
Machine output / no colours.
-chdir DIR
Run in another folder.

Voorbeelden 11

$ als gewone gebruiker# als root (of met sudo)

  1. 1The normal workflow

    tofu init
    tofu fmt -recursive && tofu validate
    tofu plan -out tfplan
    tofu apply tfplan
  2. 2Move a Terraform project to OpenTofu

    terraform version
    tofu init -upgrade
    tofu plan

    The plan should show no changes. Make a backup of the state first.

  3. 3Encrypt the state with a key from an environment variable

    cat encryption.tf
    terraform {
      encryption {
        key_provider "pbkdf2" "main" { passphrase = var.state_passphrase }
        method "aes_gcm" "main" { keys = key_provider.pbkdf2.main }
        state { method = method.aes_gcm.main }
      }
    }
  4. 4Variables per environment

    tofu plan -var-file envs/prod.tfvars -out tfplan
  5. 5Find drift

    tofu plan -refresh-only
  6. 6Read an output in a script

    tofu output -raw lb_ip
  7. 7Import an existing resource

    tofu import azurerm_resource_group.main /subscriptions/SUB/resourceGroups/rg-shop
  8. 8List and move state

    tofu state list
    tofu state mv azurerm_storage_account.old azurerm_storage_account.logs
  9. 9Run tests

    tofu test
  10. 10Lock providers for Linux and Mac

    tofu providers lock -platform=linux_amd64 -platform=darwin_arm64
  11. 11In CI

    tofu init -input=false
    tofu plan -input=false -no-color -out tfplan

Let op

  • Do not run terraform and tofu on the same state at the same time; choose one tool per project.
  • New Terraform-only features (after 1.6) may not exist in OpenTofu, and the other way.

Bestanden

*.tf / *.tofu
The code (.tofu files are read only by OpenTofu).
.terraform.lock.hcl
Provider versions and checksums.

Tips om te onthouden

  • Learn with Terraform docs (more examples); the commands are the same.

Exitcodes

0OK (plan: no changes).
1Error.
2plan -detailed-exitcode: there are changes.