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

الفصل 8الاسم في الدورة: Objective 5: Terraform modules

Reuse code with modules

A module is a folder of Terraform files used as one building block: "a network", "a web server", "a database". Every configuration is already a module (the root module); it can call child modules from local folders, Git or a registry.

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

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

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

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

  • استخدام الوحدات وإدارة إصداراتها

8.1

Know where modules come from

The source argument tells Terraform where to get the module.

terraform init
terraform get -update

8.2

Call a module

A module block gives the module's input variables and reads its outputs.

main.tf
module "network" {
  source        = "./modules/network"
  name          = "vnet-shop-${var.env}"
  address_space = ["10.30.0.0/16"]
  subnets = {
    web = "10.30.1.0/24"
    app = "10.30.2.0/24"
  }
  tags = local.tags
}
 
resource "azurerm_network_interface" "web" {
  # ...
  ip_configuration {
    name                          = "ipconfig1"
    subnet_id                     = module.network.subnet_ids["web"]
    private_ip_address_allocation = "Dynamic"
  }
}

8.3

Write a module

A good module has a clear interface: variables.tf (inputs), main.tf (resources), outputs.tf (results), versions.tf (providers), and a README.

modules/network/outputs.tf
output "vnet_id" {
  value = azurerm_virtual_network.this.id
}
 
output "subnet_ids" {
  value = { for k, s in azurerm_subnet.this : k => s.id }
}

8.4

Understand variable scope

A module sees only its own variables and locals. It can not read var. of the caller. Everything must be passed in as inputs, and comes back only through outputs.

FromToHow
Root moduleChild moduleModule arguments (name = …)
Child moduleRoot modulemodule.NAME.OUTPUT
Child AChild BThrough the root: module.a.x as input of module "b"

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

Resources inside a module are addressed as module.network.azurerm_virtual_network.this in state commands.

8.5

Pin module versions

Registry modules use version; Git modules use ?ref=TAG. Pin them, and upgrade on purpose.

main.tf
module "vnet" {
  source  = "Azure/avm-res-network-virtualnetwork/azurerm"
  version = "~> 0.8"
  # ...
}
terraform init -upgrade

تذكّر

version works only for registry modules. For Git sources, the version is in the URL (?ref=v1.2.0).

في العمل

Review public modules before you use them: read the code, the open issues and how often it is updated. A module runs with the rights of your pipeline.

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

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