我在 Variables.tf 文件中有以下变量:
variable tenants {
description = "Map of project names to configuration."
type = list(object({
name = string
dname = string
desc = string
site = list(string)
}))
default = [{
name = "Tenant-1",
dname = "Tenant-1",
desc = "Test Tenant 1",
site = ["site1", "site2"]
},
{
name = "Tenant-2",
dname = "Tenant-2",
desc = "Test Tenant 2",
site = ["site1"]
}]
}
Run Code Online (Sandbox Code Playgroud)
在我的 main.tf 文件中,我想循环遍历这个列表。我在 main.tf 文件中有以下代码:
resource "mso_tenant" "restenant" {
for_each = {for i, v in var.tenants: i => v}
name = each.value.name
display_name = each.value.dname
description = each.value.desc
site_associations {
site_id = each.value.site
}
}
Run Code Online (Sandbox Code Playgroud)
因此,最终结果应该是使用变量文件中指定的属性创建 2 个租户。因此,一旦创建,tenant1 将有 2 个 site_associations,tenant2 将有 1 个关联。
结果应该是:
name = "Tenant-1"
display_name = "Tenant-1"
description = "Test Tenant 1"
site_associations {
site_id = site1
site_id = site2
}
Run Code Online (Sandbox Code Playgroud)
和
name = "Tenant-2"
display_name = "Tenant-2"
description = "Test Tenant 2"
site_associations {
site_id = site1
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下方法:
resource "mso_tenant" "restenant" {
for_each = {for i, v in var.tenants: i => v}
name = each.value.name
display_name = each.value.dname
description = each.value.desc
site_associations {
site_id = each.value.site
}
}
Run Code Online (Sandbox Code Playgroud)
这适用于name
,dname
但desc
它不会迭代站点变量(这是一个列表)。这会导致错误消息:
each.value.site 是具有 1 个元素的字符串列表,属性“site_id”的值不合适:需要字符串。
尝试解决如下:
resource "mso_tenant" "restenant" {
for_each = {for i, v in var.tenants: i => v}
name = each.value.name
display_name = each.value.dname
description = each.value.desc
site_associations {
site_id = [for site in each.value.site: site]
}
}
Run Code Online (Sandbox Code Playgroud)
但这也给出了:
each.value.site 是包含 2 个元素的字符串列表,属性“site_id”的值不合适:需要字符串。
小智 10
您正在寻找的是动态块:https://www.terraform.io/docs/configuration/expressions.html#dynamic-blocks
原来的:
site_associations {
site_id = each.value.site
}
Run Code Online (Sandbox Code Playgroud)
动态的:
dynamic "site_associations"{
for_each = each.value.site
content {
site_id = site_associations.value
}
}
Run Code Online (Sandbox Code Playgroud)
该dynamic
块允许您for_each
在each.value.site
.
注意:动态块内的值是使用块名称引用的site_associations.value
整个main.tf:
variable tenants {
description = "Map of project names to configuration."
type = list(object({
name = string
dname = string
desc = string
site = list(string)
}))
default = [{
name = "Tenant-1",
dname = "Tenant-1",
desc = "Test Tenant 1",
site = ["site1", "site2"]
},
{
name = "Tenant-2",
dname = "Tenant-2",
desc = "Test Tenant 2",
site = ["site1"]
}]
}
resource "mso_tenant" "restenant" {
for_each = {for i, v in var.tenants: i => v}
name = each.value.name
display_name = each.value.dname
description = each.value.desc
dynamic "site_associations"{
for_each = each.value.site
content {
site_id = site_associations.value
}
}
}
Run Code Online (Sandbox Code Playgroud)
计划输出:
# mso_tenant.restenant["0"] will be created
+ resource "mso_tenant" "restenant" {
+ description = "Test Tenant 1"
+ display_name = "Tenant-1"
+ id = (known after apply)
+ name = "Tenant-1"
+ site_associations {
+ aws_access_key_id = (known after apply)
+ aws_account_id = (known after apply)
+ aws_secret_key = (known after apply)
+ azure_access_type = (known after apply)
+ azure_active_directory_id = (known after apply)
+ azure_application_id = (known after apply)
+ azure_client_secret = (known after apply)
+ azure_subscription_id = (known after apply)
+ is_aws_account_trusted = (known after apply)
+ site_id = "site1"
+ vendor = (known after apply)
}
+ site_associations {
+ aws_access_key_id = (known after apply)
+ aws_account_id = (known after apply)
+ aws_secret_key = (known after apply)
+ azure_access_type = (known after apply)
+ azure_active_directory_id = (known after apply)
+ azure_application_id = (known after apply)
+ azure_client_secret = (known after apply)
+ azure_subscription_id = (known after apply)
+ is_aws_account_trusted = (known after apply)
+ site_id = "site2"
+ vendor = (known after apply)
}
+ user_associations {
+ user_id = (known after apply)
}
}
# mso_tenant.restenant["1"] will be created
+ resource "mso_tenant" "restenant" {
+ description = "Test Tenant 2"
+ display_name = "Tenant-2"
+ id = (known after apply)
+ name = "Tenant-2"
+ site_associations {
+ aws_access_key_id = (known after apply)
+ aws_account_id = (known after apply)
+ aws_secret_key = (known after apply)
+ azure_access_type = (known after apply)
+ azure_active_directory_id = (known after apply)
+ azure_application_id = (known after apply)
+ azure_client_secret = (known after apply)
+ azure_subscription_id = (known after apply)
+ is_aws_account_trusted = (known after apply)
+ site_id = "site1"
+ vendor = (known after apply)
}
+ user_associations {
+ user_id = (known after apply)
}
}
Plan: 2 to add, 0 to change, 0 to destroy.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37981 次 |
最近记录: |