来自远程 terraform 模块的输出变量
Output variables from remote terraform module
对于我使用远程模块(git 模块)的项目,这些模块在 terraformMain.tf 文件中调用和执行。
例如,我使用了一个 Azure 资源组模块,这个模块在 terraformMain.tf 中通过“count = length (var.resourcegroups)”循环。我现在遇到的问题是我想在下一个模块(创建 VNET)中使用两个创建的资源组之一,但我一直遇到以下错误:
Error: Unsupported attribute
on outputs.tf line 2, in output "RG": 2: value =
[module.resourceGroups.resource_group_name]
This value does not have any attributes.
Unsupported attribute
on terraformMain.tf line 33, in module "vnet": 33:
resourcegroup_name = module.resourceGroups.resource_group_name[0]
This value does not have any attributes.
Azure 资源组模块代码如下所示:
main.tf
resource "azurerm_resource_group" "RG" {
name = var.resource_group_name
location = var.location
}
variables.tf
variable "location" {
type = string
}
variable "resource_group_name" {
type = string
}
outputs.tf
output "resource_group_names" {
value = concat(azurerm_resource_group.RG.*.name, [""])[0]
}
terraformMain.tf 的代码如下所示:
terraformMain.tf
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.45.1"
}
}
backend "azurerm" {
resource_group_name = "__terraformresourcegroup__"
storage_account_name = "__terraformstorageaccount__"
container_name = "__terraformcontainer__"
key = "__terraformkey__"
}
}
provider "azurerm" {
features {}
}
module "resourceGroups" {
count = length(var.resourcegroups)
source = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/ResourceGroup"
location = var.location
resource_group_name = var.resourcegroups[count.index]
}
module "vnet" {
source = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/VirtualNetwork"
resourcegroup_name = module.resourceGroups.resource_group_name[0]
location = var.location
vnet_name = var.vnet_name
count = length(var.subnet_names)
vnet_cidr = var.vnet_cidr[count.index]
subnet_cidr = var.subnet_cidr[count.index]
subnet_name = var.subnet_names[count.index]
}
variables.tf
variable "location" {
default = "westeurope"
}
variable "resourcegroups" {
default = ["rg1", "rg2"]
}
#Azure Vnet / Subnet
variable "vnet_name" {
default = "vnet_1"
}
variable "subnet_names" {
default = ["subnet1", "subnet2"]
}
variable "vnet_cidr" {
default = ["10.116.15.0/24"]
}
variable "subnet_cidr" {
default = ["10.116.15.0/26", "10.116.15.128/27"]
}
outputs.tf
output "RG" {
value = [module.resourceGroups.resource_group_name]
}
感谢任何帮助!
模块中的变量需要有一个type
或一个default
。
例如,这将是一个有效文件:
variable "location" {
type = string
}
variable "resource_group_name" {
type = string
}
您的 resourceGroups
模块设置了 count = length(var.resourcegroups)
,因此 module.resourceGroups
是一个对象列表,因此您需要在访问属性之前请求列表中的特定元素:
module.resourceGroups[0].resource_group_name
或者,如果您的目标是 return 所有资源组名称的列表,您可以使用 the [*]
operator 从每个元素中简明地访问 resource_group_name
参数和 return 结果列表:
resource.resourceGroups[*].resource_group_name
我们应用的解决方案是将计数从 terraformMain.tf 移动到资源模块 main.tf。这允许我们通过模块的 output.tf 将资源组传递给 terraformMain。
资源组模块:
main.tf
resource "azurerm_resource_group" "RG" {
count = length(var.resource_group_name)
name = var.resource_group_name[count.index]
location = var.location
}
outputs.tf
output "resource_group_names" {
value = azurerm_resource_group.RG.*.name
}
terraformMain.tf代码:
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.45.1"
}
}
backend "azurerm" {
resource_group_name = "__terraformresourcegroup__"
storage_account_name = "__terraformstorageaccount__"
container_name = "__terraformcontainer__"
key = "__terraformkey__"
}
}
provider "azurerm" {
features {}
}
module "resourceGroups" {
source = "git::https://*****@dev.azure.com/*****/TerraformAzureModules/_git/ResourceGroup"
location = var.location
resource_group_name = var.resourcegroups
}
module "vnet" {
source = "git::https://******@dev.azure.com/*****/TerraformAzureModules/_git/VirtualNetwork"
resourcegroup_name = module.resourceGroups.resource_group_names[0]
location = var.location
vnet_name = var.vnet_name
vnet_cidr = var.vnet_cidr
subnet_cidr = var.subnet_cidr
subnet_name = var.subnet_names
}
感谢您的贡献
对于我使用远程模块(git 模块)的项目,这些模块在 terraformMain.tf 文件中调用和执行。
例如,我使用了一个 Azure 资源组模块,这个模块在 terraformMain.tf 中通过“count = length (var.resourcegroups)”循环。我现在遇到的问题是我想在下一个模块(创建 VNET)中使用两个创建的资源组之一,但我一直遇到以下错误:
Error: Unsupported attribute
on outputs.tf line 2, in output "RG": 2: value = [module.resourceGroups.resource_group_name]
This value does not have any attributes.
Unsupported attribute
on terraformMain.tf line 33, in module "vnet": 33: resourcegroup_name = module.resourceGroups.resource_group_name[0]
This value does not have any attributes.
Azure 资源组模块代码如下所示:
main.tf
resource "azurerm_resource_group" "RG" {
name = var.resource_group_name
location = var.location
}
variables.tf
variable "location" {
type = string
}
variable "resource_group_name" {
type = string
}
outputs.tf
output "resource_group_names" {
value = concat(azurerm_resource_group.RG.*.name, [""])[0]
}
terraformMain.tf 的代码如下所示:
terraformMain.tf
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.45.1"
}
}
backend "azurerm" {
resource_group_name = "__terraformresourcegroup__"
storage_account_name = "__terraformstorageaccount__"
container_name = "__terraformcontainer__"
key = "__terraformkey__"
}
}
provider "azurerm" {
features {}
}
module "resourceGroups" {
count = length(var.resourcegroups)
source = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/ResourceGroup"
location = var.location
resource_group_name = var.resourcegroups[count.index]
}
module "vnet" {
source = "git::https://*****@dev.azure.com/****/TerraformAzureModules/_git/VirtualNetwork"
resourcegroup_name = module.resourceGroups.resource_group_name[0]
location = var.location
vnet_name = var.vnet_name
count = length(var.subnet_names)
vnet_cidr = var.vnet_cidr[count.index]
subnet_cidr = var.subnet_cidr[count.index]
subnet_name = var.subnet_names[count.index]
}
variables.tf
variable "location" {
default = "westeurope"
}
variable "resourcegroups" {
default = ["rg1", "rg2"]
}
#Azure Vnet / Subnet
variable "vnet_name" {
default = "vnet_1"
}
variable "subnet_names" {
default = ["subnet1", "subnet2"]
}
variable "vnet_cidr" {
default = ["10.116.15.0/24"]
}
variable "subnet_cidr" {
default = ["10.116.15.0/26", "10.116.15.128/27"]
}
outputs.tf
output "RG" {
value = [module.resourceGroups.resource_group_name]
}
感谢任何帮助!
模块中的变量需要有一个type
或一个default
。
例如,这将是一个有效文件:
variable "location" {
type = string
}
variable "resource_group_name" {
type = string
}
您的 resourceGroups
模块设置了 count = length(var.resourcegroups)
,因此 module.resourceGroups
是一个对象列表,因此您需要在访问属性之前请求列表中的特定元素:
module.resourceGroups[0].resource_group_name
或者,如果您的目标是 return 所有资源组名称的列表,您可以使用 the [*]
operator 从每个元素中简明地访问 resource_group_name
参数和 return 结果列表:
resource.resourceGroups[*].resource_group_name
我们应用的解决方案是将计数从 terraformMain.tf 移动到资源模块 main.tf。这允许我们通过模块的 output.tf 将资源组传递给 terraformMain。
资源组模块:
main.tf
resource "azurerm_resource_group" "RG" {
count = length(var.resource_group_name)
name = var.resource_group_name[count.index]
location = var.location
}
outputs.tf
output "resource_group_names" {
value = azurerm_resource_group.RG.*.name
}
terraformMain.tf代码:
terraform {
required_version = ">= 0.13"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.45.1"
}
}
backend "azurerm" {
resource_group_name = "__terraformresourcegroup__"
storage_account_name = "__terraformstorageaccount__"
container_name = "__terraformcontainer__"
key = "__terraformkey__"
}
}
provider "azurerm" {
features {}
}
module "resourceGroups" {
source = "git::https://*****@dev.azure.com/*****/TerraformAzureModules/_git/ResourceGroup"
location = var.location
resource_group_name = var.resourcegroups
}
module "vnet" {
source = "git::https://******@dev.azure.com/*****/TerraformAzureModules/_git/VirtualNetwork"
resourcegroup_name = module.resourceGroups.resource_group_names[0]
location = var.location
vnet_name = var.vnet_name
vnet_cidr = var.vnet_cidr
subnet_cidr = var.subnet_cidr
subnet_name = var.subnet_names
}
感谢您的贡献