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

Terraform 如何将数据源从一个模块引用到另一个模块,并将其作为变量传递给根模块?,terraform,azure-virtual-machine,azure-virtual-network,terraform-provider-azure,Terraform,Azure Virtual Machine,Azure Virtual Network,Terraform Provider Azure,我的地形目录结构如下: 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/resource

我的地形目录结构如下:

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
资源组模块配置文件如下:

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
目的:在本模块中,我引用了一个现有的资源组,我想利用该资源组创建虚拟机及其关联对象

main.tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
变量。tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
网络模块

output "subnet-id" {
  value = "data.network.tf-sn.id"
}
output "tf-rg-external-location" {
  value = data.resourcegroup.tf-rg-external.location
}
output "tf-rg-external-name" {
  value = data.resourcegroup.tf-rg-external.name
}
目的:我想使用resourcegroup模块中的资源组在此模块中引用。这样,我在一个地方定义并在root和其他模块中使用它,例如,compute、app service、aks等

main.tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
变量。tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
计算模块

目的:定义计算(VM)的所有属性。其思想是,根模块将使用此模块来启动不同的VM角色

main.tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
变量。tf

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

}
# 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
}
# Declare env variable
variable "vnet_name" {
  type = string
}

variable "subnet_name" {
  type = string
}
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
  .
  .
  .
}
variable "admin_password" {
  type = string
}
variable "admin_username" {
  type = string
}
variable "boot_diagnostics" {
  type = bool
}
variable "boot_diagnostics_sa_type" {
  type = string
}...
地形根模块

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

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?将要改变的是它们的主机名和vm大小

==========

回答后更改

==========

我更新了中的子网、资源组和位置的值 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
资源组网络模块中的Myoutputs.tf文件如下所示:

location                      = module.resourcegroup.tf-rg-external-location
vnet_subnet_id                = module.network.subnet-id
resource_group_name           = module.resourcegroup.tf-rg-external-name
输出网络模块的.tf

output "subnet-id" {
  value = "data.network.tf-sn.id"
}
output "tf-rg-external-location" {
  value = data.resourcegroup.tf-rg-external.location
}
output "tf-rg-external-name" {
  value = data.resourcegroup.tf-rg-external.name
}
输出资源组的.tf

output "subnet-id" {
  value = "data.network.tf-sn.id"
}
output "tf-rg-external-location" {
  value = data.resourcegroup.tf-rg-external.location
}
output "tf-rg-external-name" {
  value = data.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.
那么,我们似乎不应该在根模块中引用它们

另外,它们的变量应该在root modules variables.tf文件中定义,因为我相信您可以覆盖根模块中模块变量的值

如果我表现得很愚蠢,请原谅我。我试图了解它在现实生活中是如何工作的

上次提交和公开回购后,错误如下所示

Error: Reference to undeclared module

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

No module call named "resourcegroup" is declared in the root module.


Error: Reference to undeclared module

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

No module call named "network" is declared in the root module.


Error: Reference to undeclared module

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

No module call named "resourcegroup" is declared in the root module.


Error: Reference to undeclared module

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

No module call named "resourcegroup" is declared in sql_vm.


Error: Reference to undeclared module

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

No module call named "network" is declared in sql_vm.


Error: Reference to undeclared module

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

No module call named "resourcegroup" is declared in sql_vm.


Error: Reference to undeclared module

  on modules/network/main.tf line 5, in data "azurerm_virtual_network" "tf-vn":
   5:   resource_group_name = module.resource_groups.external_rg_name

No module call named "resource_groups" is declared in test2.


Error: Reference to undeclared resource

  on modules/resourcegroup/outputs.tf line 2, in output "tf-rg-external-location":
   2:   value = data.resourcegroup.tf-rg-external.location

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


Error: Reference to undeclared resource

  on modules/resourcegroup/outputs.tf line 5, in output "tf-rg-external-name":
   5:   value = data.resourcegroup.tf-rg-external.name

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

检查您正在处理的回购协议()。这对我现在来说是有意义的

问题是,您将如何使用公共模块的想法与自己创建的模块混为一谈

事实上,您不需要设置任何模块来引用其他公共terraform注册表模块

将子模块(模块/计算、模块/网络、模块/资源组)中的所有代码移动到顶部文件夹(
/main.tf

例如(代码未经验证,仅供参考)


先生,我已经用新的错误更新了我的问题,敬请指教。我添加了答案,让我知道它是否能解决您的问题。先生。请协助。我的整个回购协议现在都公开了。现在有道理了。您根本不需要创建子模块。一切都应该在最上面。因为您尝试引用公共terraform注册表模块。我给你一些代码,看看我的更新。非常感谢!我现在明白我在做什么了。还有很多东西要学!我认为你需要仔细阅读答案,它说你需要定义输出,以便你可以参考你需要的资源。事实上,我这样做了,然后只运行了地形图,我没有在我的问题帖子中粘贴output.tf。我将修改我的问题,以便你们能看到输出。如果没有定义outputs.tf,它本身会报告一个不同的错误。也许现在它有意义了?我需要一些帮助,因为我无法根据BMW先生提出的解决方案使其工作。