如何从一个模块引用数据源到另一个模块,并将其作为变量传递给根模块?

问题描述 投票:2回答:1

我具有如下的terraform目录结构:

terraform/
main.tf  modules  outputs.tf  provider.tf  variables.tf

./modules:
compute  network  resourcegroup

./modules/compute:
main.tf  outputs.tf  variables.tf

./modules/network:
main.tf  outputs.tf  variables.tf

./modules/resourcegroup:
main.tf  outputs.tf  variables.tf

resourcegroup模块配置文件如下:

目的:在此模块中,我引用了一个现有的资源组,我想利用该资源组来创建虚拟机及其关联的对象。

main.tf

data "azurerm_resource_group" "tf-rg-external" {
  name = var.rg_name
}

variables.tf

variable "rg_name" {
  type = string

}

网络模块

目的:我想使用来自resourcegroup模块的资源组,以在此模块中进行引用。这样,我可以在一个地方定义并在根目录和其他模块示例中使用它,例如计算,应用程序服务,aks等

main.tf

# Reference existing Virtual Network
data "azurerm_virtual_network" "tf-vn" {
  name                = var.vnet_name
  resource_group_name = module.resource_groups.external_rg_name
}

# Reference existing subnet
data "azurerm_subnet" "tf-sn" {
  name                 = var.subnet_name
  virtual_network_name = data.azurerm_virtual_network.tf-vn.name
  resource_group_name  = module.resource_groups.external_rg_name
}

variables.tf

# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}

compute模块。

目的:定义compute(VM)的所有属性。这个想法是,根模块将使用该模块来提升不同的VM角色。

main.tf

module "vm_iis" {
  source                        = "Azure/compute/azurerm"
  location                      = data.resourcegroup.tf-rg-external.location
  vnet_subnet_id                = data.network.tf-sn.id
  admin_password                = var.admin_password
  data_sa_type                  = var.data_sa_type
  delete_os_disk_on_termination = var.delete_os_disk_on_termination
  nb_instances                 = var.nb_instances
  nb_public_ip                 = var.nb_public_ip
  public_ip_address_allocation = var.public_ip_address_allocation
  resource_group_name          = data.resourcegroup.tf-rg-external.name
  .
  .
  .
}

variables.tf

variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...

terraform根模块。

目的:这应该利用定义的模块来创建各种大小和主机名不同的VM

main.tf:

module "sql_vm" {
  source                        = "./modules/compute/"
  #location                      = data.resourcegroup.tf-rg-external.location
  #vnet_subnet_id                = data.network.tf-sn.id
  public_ip_address_allocation  = var.public_ip_address_allocation
  #resource_group_name          = data.resourcegroup.tf-rg-external.name
  storage_account_type          = var.storage_account_type
  vm_hostname                   = var.vm_hostname
}

variables.tf:声明main.tf文件中的所有变量。

注意:我有意将变量硬编码在根模块主文件/变量文件中。这只是使模块之间的通信正确。理解和使用模块的正确方法。

但是,当我在根模块中运行Terraform Plan时。我收到以下错误:

Error: Reference to undeclared resource

  on modules/compute/main.tf line 3, in module "vm_iis":
   3:   location                      = data.resourcegroup.tf-rg-external.location

A data resource "resourcegroup" "tf-rg-external" has not been declared in
sql_vm.


Error: Reference to undeclared resource

  on modules/compute/main.tf line 4, in module "vm_iis":
   4:   vnet_subnet_id                = data.network.tf-sn.id

A data resource "network" "tf-sn" has not been declared in sql_vm.


Error: Reference to undeclared resource

  on modules/compute/main.tf line 22, in module "vm_iis":
  22:   resource_group_name          = data.resourcegroup.tf-rg-external.name

A data resource "resourcegroup" "tf-rg-external" has not been declared in
sql_vm.

问题是什么以及如何解决?

还可以通过某个循环创建不同的(角色)vm吗?例如sql-vm,iis-vm,testvm,abcvm?将会改变的是它们的主机名和虚拟机大小。

===========

发布答案更改

===========

我更新了子网,资源组和位置的值compute / main.tfterraform / main.tf如下所示:

location                      = module.resourcegroup.tf-rg-external-location
vnet_subnet_id                = module.network.subnet-id
resource_group_name           = module.resourcegroup.tf-rg-external-name

很遗憾,我仍然收到如下错误

Error: Unsupported argument

  on main.tf line 3, in module "sql_vm":
   3:   location                      = module.resourcegroup.tf-rg-external-location

An argument named "location" is not expected here.


Error: Unsupported argument

  on main.tf line 4, in module "sql_vm":
   4:   vnet_subnet_id                = module.network.subnet-id

An argument named "vnet_subnet_id" is not expected here.


Error: Unsupported argument

  on main.tf line 5, in module "sql_vm":
   5:   resource_group_name           = module.resourcegroup.tf-rg-external-name

An argument named "resource_group_name" is not expected here.

所以,看来我们不应该在根模块中引用它们?

此外,应该在根模块variables.tf文件中定义变量的位置,因为我相信您可以覆盖根模块中模块变量的值?

如果我看起来很蠢,请原谅我。我正在尝试了解它在现实生活中的工作原理。

terraform azure-virtual-machine azure-virtual-network terraform-provider-azure
1个回答
1
投票

没有配置output,您不能在模块中引用该值

© www.soinside.com 2019 - 2024. All rights reserved.