متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 52OpenTofu · IaC and automation

Install and set up OpenTofu

OpenTofu is the open-source fork of Terraform (MPL license, a Linux Foundation project). It reads the same .tf files, uses the same providers and modules, and the commands are the same with tofu instead of terraform. It also adds features Terraform does not have, like state encryption. This guide installs OpenTofu on RHEL and Ubuntu, sets up a project with encrypted state, migrates an existing Terraform project safely, and covers the differences and fixes.

  • 9 الدروس
  • 9 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

52.1

Know it before you install

OpenTofu = Terraform's workflow (init, plan, apply) + an open license + extra features. Providers come from registry.opentofu.org (it mirrors the common providers). Choose one tool per project — do not mix terraform and tofu on the same state.

ItemValue
Packagetofu (repository packages.opentofu.org)
Program/usr/bin/tofu
Project files*.tf (and *.tofu for OpenTofu-only code), .terraform.lock.hcl
CLI settings~/.tofurc (or ~/.terraformrc)
Extra featuresstate and plan encryption, early variable evaluation, provider for_each, -exclude

الأوامر في هذا الدرس

52.2

Install on RHEL and Ubuntu

The official installer script adds the right repository for your system. Download it, read it, then run it with your package type:

curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
less install-opentofu.sh
chmod +x install-opentofu.sh
./install-opentofu.sh --install-method rpm
./install-opentofu.sh --install-method deb
tofu version
OpenTofu v1.10.6
on linux_amd64

(Use rpm on RHEL/Fedora, deb on Ubuntu/Debian.) After that, dnf upgrade / apt upgrade keeps tofu updated.

52.3

First configuration: a project with encrypted state

Pin the versions, as with Terraform:

versions.tf
terraform {
  required_version = ">= 1.10"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

State encryption — the state often holds passwords. OpenTofu can encrypt state and plan files itself (here with AWS KMS; for labs a passphrase also works):

encryption.tf
terraform {
  encryption {
    key_provider "aws_kms" "main" {
      kms_key_id = "alias/tofu-state"
      region     = "eu-central-1"
      key_spec   = "AES_256"
    }
    method "aes_gcm" "main" {
      keys = key_provider.aws_kms.main
    }
    state {
      method   = method.aes_gcm.main
      enforced = true
    }
    plan {
      method   = method.aes_gcm.main
      enforced = true
    }
  }
}

Plus a remote backend with locking (same as Terraform, for example S3 with use_lockfile = true) and the .gitignore (.terraform/, *.tfstate*, *.tfvars).

SettingWhy
encryption with KMSState and plans are encrypted before they reach the backend; only people with the KMS key can read them.
enforced = trueOpenTofu refuses to write unencrypted state by mistake.
Pinned versions + lock fileSame providers on every laptop and in CI.
tofu init
tofu fmt -recursive
tofu validate
tofu plan -out=tfplan
tofu apply tfplan

الأوامر في هذا الدرس

52.4

Migrate a Terraform project

Do it on a clean state: no pending changes, everyone stopped working on it.

terraform plan
No changes. Your infrastructure matches the configuration.
cp terraform.tfstate terraform.tfstate.backup-before-tofu
tofu init -upgrade
tofu plan
No changes. Your infrastructure matches the configuration.

(With a remote backend, make a copy of the state file in the bucket first.) If tofu plan shows no changes, the migration is done — use only tofu from now on, in CI too. Check the OpenTofu migration notes for your Terraform version (state from very new Terraform versions may need the matching OpenTofu version).

52.5

Daily use: the commands and the real uses

tofu plan -out=tfplan
tofu show tfplan
tofu apply tfplan
tofu state list
tofu output -json
tofu plan -exclude=module.legacy
tofu import aws_s3_bucket.logs acme-logs
tofu providers lock -platform=linux_amd64 -platform=darwin_arm64
tofu test

Real uses at work:

  • The same IaC work as Terraform, with an open license (important for some companies and products).
  • Encrypted state without extra tools.
  • tofu test for module tests; provider for_each for many regions or accounts.

الأوامر في هذا الدرس

52.6

Make it safe

Security checklist:

  • State encryption (enforced = true) and a private, versioned backend with locking.
  • No secrets in .tf or committed .tfvars; sensitive = true on secret variables and outputs.
  • Short-lived cloud credentials (SSO, OIDC in CI).
  • Pin OpenTofu and provider versions; review every plan.
  • Scan the code with trivy config . or checkov.
  • Keep the KMS key and its permissions safe — losing it means losing the state.

الأوامر في هذا الدرس

52.7

Update and remove

dnf -y upgrade tofu
apt-get install --only-upgrade -y tofu
tofu init -upgrade
dnf -y remove tofu

الأوامر في هذا الدرس

52.8

Fix common problems

ProblemCauseFix
Failed to query available provider packagesProvider not found in the OpenTofu registry or no network.Check the provider name; network to registry.opentofu.org.
State lock errorAnother run or a crashed run.Wait, or tofu force-unlock LOCK_ID when the run is really dead.
encryption … key provider errorNo access to the KMS key.Check your cloud login and the KMS key policy.
State can not be read after switching toolsState written by a newer Terraform version.Use a matching OpenTofu version; restore the backup; read the migration notes.
Unsupported OpenTofu Core versionrequired_version does not match.Install the right version or change the constraint.
CI still runs terraformOld pipeline.Change the image and commands to tofu; one tool per state.

52.9

Next steps

1How do you know a Terraform → OpenTofu migration worked?

tofu plan after tofu init shows "No changes".

2What does enforced = true in the state encryption block do?

OpenTofu refuses to write the state without encryption.

كل الأوامر في هذا الفصل