متقدم
البحث فيالمعامل
طريقة البحث
  • ابحث عن مهمة أو أداة (lvm، selinux، ‎@podman)؛ وصفِّ حسب المستوى والكتاب أو الدورة.
  • مع مربع بحث فارغ، الفلتر وحده يعرض المعامل — مثلًا كل المعامل الصعبة لدورة واحدة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

معمل 9.1Terraform, الفصل 9

Work with state: inspect, move, remove, import and find drift

المستوى
متوسط
45 دقيقة
الأجهزة
any computer with Terraform 1.6 or newer (no cloud account needed)
الحالة
لم يبدأ

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

الهدف

State is where most real Terraform trouble starts. In this lab you look inside the state, rename a resource without destroying it (moved block and terraform state mv), stop managing a resource without deleting it, import an existing file, and find and fix drift. Everything uses the local provider, so you can break things safely.

قبل أن تبدأ

  • Make the folder ~/tf-lab2. Terraform 1.6 or newer (import and removed blocks need 1.5 / 1.7; terraform state rm works everywhere).
  • Make this main.tf and run terraform init && terraform apply -auto-approve:
    main.tf
    resource "local_file" "web" {
      filename = "${path.module}/web.conf"
      content  = "port=80\n"
    }
     
    resource "local_file" "db" {
      filename = "${path.module}/db.conf"
      content  = "port=5432\n"
    }

المهام

  1. List all resources in the state and show the details of local_file.web.
  2. Rename local_file.web to local_file.frontend in the code, and use a moved block so the plan shows no destroy.
  3. Remove local_file.db from Terraform management without deleting the file db.conf.
  4. Make a file cache.conf by hand (port=6379). Write a resource for it and bring it into the state with an import block. The plan must show import, not create.
  5. Make drift: change web.conf by hand to port=8080. Find the drift with a refresh-only plan, then fix it with a normal apply.
  6. Make a backup of the state and explain where the lock file and the backup are.

تحقّق من عملك

terraform state list
terraform plan
ls
cat web.conf
  • terraform state list shows local_file.cache and local_file.frontend, and not local_file.db or local_file.web.
  • terraform plan says No changes.
  • db.conf still exists on disk.
  • web.conf has port=80 again.

تلميحات

اعرض التلميحات

الحل

اعرض الحل
  1. Inspect the state.
    terraform state list
    local_file.db
    local_file.web
    terraform state show local_file.web
  2. Rename with a moved block (change the resource name too).
    main.tf (part)
    resource "local_file" "frontend" {
      filename = "${path.module}/web.conf"
      content  = "port=80\n"
    }
     
    moved {
      from = local_file.web
      to   = local_file.frontend
    }
    terraform plan
    terraform apply -auto-approve

    The plan says local_file.web has moved to local_file.frontend and 0 to destroy. The CLI way is terraform state mv local_file.web local_file.frontend.

  3. Forget the database file but keep it.
    terraform state rm local_file.db
    Removed local_file.db
    Successfully removed 1 resource instance(s).

    Then delete the local_file.db block from main.tf; ls still shows db.conf.

  4. Import an existing file.
    printf 'port=6379\n' > cache.conf
    main.tf (part)
    resource "local_file" "cache" {
      filename = "${path.module}/cache.conf"
      content  = "port=6379\n"
    }
     
    import {
      to = local_file.cache
      id = "./cache.conf"
    }
    terraform plan
    terraform apply -auto-approve
  5. Find and fix drift.
    printf 'port=8080\n' > web.conf
    terraform plan -refresh-only
    terraform apply -auto-approve
    cat web.conf
    port=80
  6. Back up the state.
    cp terraform.tfstate state-backup-$(date +%F).tfstate
    ls -a

    terraform.tfstate.backup is the state before the last change. .terraform.tfstate.lock.info exists only while a command runs (the local lock). With a remote backend (Azure Storage, S3 + DynamoDB, HCP Terraform) the lock and the versions live in the backend.

ملاحظاتي

لا توجد ملاحظات بعد.

دروس هذا المعمل

الأوامر في هذا المعمل