Terraform 通过地形0.12中的一个对象,每个
我想用下一种方式调用terraform模块:Terraform 通过地形0.12中的一个对象,每个,terraform,terraform-template-file,terraform-modules,Terraform,Terraform Template File,Terraform Modules,我想用下一种方式调用terraform模块: module "database_role" { source = "modules/roles" project_id = "testid" role_name = "testrole" actions = { action: ["ENABLE_PROFILER", "DROP_DATABASE"] dat
module "database_role" {
source = "modules/roles"
project_id = "testid"
role_name = "testrole"
actions = {
action: ["ENABLE_PROFILER", "DROP_DATABASE"]
database_name: "test_db"
}
我创建的角色模块定义是:
resource "mongodbatlas_custom_db_role" "custom_role" {
project_id = var.project_id
role_name = var.role_name
dynamic "actions" {
for_each = [for item in [var.actions] : item]
content {
actions {
action = lookup(actions.value, "action")
resources {
cluster = "false"
database_name = lookup(actions.value, "database_name")
}
}
}
}
}
因此,我希望看到正确生成的操作:
actions {
action = "ENABLE_PROFILER"
resources {
cluster = "false"
database_name = "test_db"
}
}
actions {
action = "DROP_DATABASE"
resources {
cluster = "false"
database_name = "test_db"
}
}
我得到一个错误:给定的值不适合子模块变量“actions”。
我在模块动态资源中做错了什么?谢谢此功能可在此处使用
我有以下操作
局部变量::
locals {
actions = {
action = ["ENABLE_PROFILER", "DROP_DATABASE"]
database_name = "test_db"
}
}
现在,我将根据local.actions[“action”]
的大小进行展平,以获得所需的结果。一旦得到展开的列表,我就会在列表中循环创建动态块
resource "mongodbatlas_custom_db_role" "custom_role" {
project_id = "xxx-xxx"
role_name = "yyy-yyy"
dynamic "actions" {
for_each = flatten([
for item in range(length(local.actions["action"])): {
act = local.actions["action"][item]
db_name = local.actions.database_name
}
])
content {
action = actions.value.act
resources {
cluster = "false"
database_name = actions.value.db_name
}
}
}
}
这将产生我想要的所需数量的块::
Harshas-MBP:mongo harshavmb$ terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# mongodbatlas_custom_db_role.custom_role will be created
+ resource "mongodbatlas_custom_db_role" "custom_role" {
+ id = (known after apply)
+ project_id = "xxx-xxx"
+ role_name = "yyy-yyy"
+ actions {
+ action = "ENABLE_PROFILER"
+ resources {
+ cluster = false
+ database_name = "test_db"
}
}
+ actions {
+ action = "DROP_DATABASE"
+ resources {
+ cluster = false
+ database_name = "test_db"
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.
这也可以应用于创建资源。使用嵌套或复杂循环的关键思想是将它们展平为资源或块的列表,我们创建并使用上面的示例中所示的唯一索引遍历展平的列表。谢谢,如果变量为空或未设置,如何跳过动态操作?Thanksput条件运算符仅在长度>0时循环<代码>长度(local.actions[“action”])>0?对于范围内的项(长度(local.actions[“action”]):{act=local.actions[“action”][item]db_name=local.actions.database_name}:{}类似于此。语法可能不正确,但我想你可以试试