الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

الفصل 10الاسم في الدورة: Objective 7: Maintain infrastructure with Terraform

Import, refactor and debug existing infrastructure

Real life is not a green field: resources already exist, code gets reorganised, and runs fail with strange errors. This chapter covers importing existing resources, moving and removing state entries without destroying anything, and verbose logs.

  • 4 الدروس
  • 2 الأوامر

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

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

المهارات في هذا الفصل

  • استيراد الموارد وفحص الحالة والسجلات المفصلة

10.1

Import existing resources

An import block brings an existing resource under Terraform's control. With -generate-config-out, Terraform even writes the resource code for you.

import.tf
import {
  to = azurerm_resource_group.legacy
  id = "/subscriptions/SUB_ID/resourceGroups/rg-legacy"
}
terraform plan -generate-config-out=generated.tf
terraform apply
terraform import azurerm_resource_group.legacy /subscriptions/SUB_ID/resourceGroups/rg-legacy
MethodNotes
import blockReviewed in a plan, works in CI, can generate config — the modern way
terraform import commandOld way: changes state at once, you write the code yourself

تذكّر

After an import, run plan until it shows no changes — then the code matches reality.

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

10.2

Inspect the state with the CLI

The state commands read and change state entries safely.

terraform state list
terraform state list module.network
terraform state show 'azurerm_subnet.this["web"]'
terraform state mv azurerm_subnet.web 'azurerm_subnet.this["web"]'
terraform state rm azurerm_resource_group.old
terraform state pull | jq '.resources | length'
CommandDoes
state listAll resource addresses
state show ADDRAll attributes of one resource
state mv OLD NEWRename / move an entry (no destroy)
state rm ADDRForget a resource; it stays in the cloud
state pull / pushDownload / upload the raw state (careful!)

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

10.3

Refactor with moved and removed blocks

Instead of state commands, write the change into the code — it is reviewed and works for every environment.

refactor.tf
moved {
  from = azurerm_subnet.web
  to   = azurerm_subnet.this["web"]
}
 
moved {
  from = azurerm_virtual_network.app
  to   = module.network.azurerm_virtual_network.this
}
 
removed {
  from = azurerm_resource_group.old
  lifecycle {
    destroy = false
  }
}

نصيحة للامتحان

moved = new address, same object, nothing destroyed. removed with destroy = false = stop managing it, keep it in the cloud.

10.4

Turn on verbose logging

TF_LOG prints Terraform's internal logs — API calls, provider errors, timing.

TF_LOG=DEBUG terraform plan 2> debug.log
export TF_LOG=TRACE TF_LOG_PATH=./terraform.log
TF_LOG_PROVIDER=DEBUG terraform apply
unset TF_LOG TF_LOG_PATH
VariableMeaning
TF_LOGLevel: TRACE, DEBUG, INFO, WARN, ERROR, OFF
TF_LOG_CORE / TF_LOG_PROVIDEROnly Terraform core / only providers
TF_LOG_PATHWrite the log to a file

انتبه

TRACE logs can contain secrets and tokens. Delete them after use, never attach them to a public issue.

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