gpt4 book ai didi

azure - Terraform 单变量问题的多个值

转载 作者:行者123 更新时间:2023-12-03 06:10:00 24 4
gpt4 key购买 nike

我正在尝试使用 Terraform 在 AD 中创建 Azure 应用程序。

resource "azuread_application" "my_ad_app" {
display_name = "my_app"

#API Permissions
required_resource_access {
resource_app_id = "0000000-000000000000" # Microsoft Graph

resource_access {
id = "df021288-bdef-9214" # User.Read.All
type = "Role"
}

resource_access {
id = "18a4783c662c884" # User.Read.All
type = "Role"
}

resource_access {
id = "9a5d68c3a30" # Application.Read.All
type = "Role"
}

我将创建更多 Azure AD 应用程序,并分配相同的 API 权限。将分配许多 API 权限,它们对于我将创建的所有 Azure AD 应用程序都是相同的。由于我将为其创建的所有 Azure AD 应用程序的 API 权限都是相同的,因此我想将整个 required_resource_access 分配为变量。

类似这样的事情。

#API Permissions
required_resource_access {
resource_app_id = "00000003-0000-0000-c000-000000000000" # Microsoft Graph

resource_access {
assign the entire part as a variable here? Example: var.ms_graph_api_permission
}

我尝试将以下内容添加为变量,但似乎不起作用。

variable "ms_graph_api_permission" {
type = list(string)
default =

resource_access {
id = "df021288f22de89214"
type = "Role"
}

resource_access {
id = "18a4783e5662c884"
type = "Role"
}

resource_access {
id = "9a5d68ac3a30"
type = "Role"
}
}"

这似乎不起作用。知道如何自动化执行此操作吗?

我尝试使用本地人。

locals {
resource_access = [
{
id = "df021288-bdde89214" # User.Read.All
type = "Role"
},
{
id = "18a4783c-85e5662c884" # User.Read.All
type = "Role"
}
]
}

resource "azuread_application" "my_ad_app" {
display_name = "my_app"

#API Permissions
required_resource_access {
for_each = {
for index, az_app_id in local.resource_access:
az_app_id.id => az_app_id
}

resource_app_id = "000000000000" # Microsoft Graph

resource_access = How should I pass values here using locals??

最佳答案

从这里开始需要注意的一件重要事情是,像 resource_access 这样的嵌套 block 不是您可以在变量中传递的纯数据:它们是提供者在其架构中声明的静态配置结构。因此,您不能“仅仅”通过变量传递一整套 block ,但您可以传递一些用于填充这些 block 的数据,并要求 Terraform 语言动态生成 resource_access block 根据该数据为您提供。本答案的其余部分是如何做到这一点的示例。

一、变量声明:

variable "ms_graph_api_permission" {
type = list(object({
id = string
type = string
}))
}

这声明 var.ms_graph_api_permission 是一个对象列表,其中每个对象必须有两个属性 - idtype - 两者都是字符串。这似乎与您想要通过 resource_access block 提供的数据的形状相匹配。

该类型的值看起来就像您尝试使用本地值一样:

[
{
id = "df021288-bdde89214" # User.Read.All
type = "Role"
},
{
id = "18a4783c-85e5662c884" # User.Read.All
type = "Role"
},
]

在为此变量选择值时,您可以使用上面这样的表达式作为变量的默认值,也可以在调用模块的 module block 中使用。

在资源 block 本身内部,您需要要求 Terraform 使用 resource_access block 为该列表的每个元素生成一个 dynamic block :

resource "azuread_application" "my_ad_app" {
display_name = "my_app"

#API Permissions
required_resource_access {
resource_app_id = "0000000-000000000000" # Microsoft Graph

dynamic "resource_access" {
for_each = var.ms_graph_api_permission
content {
id = resource_access.value.id
type = resource_access.value.type
}
}
}
}

dynamic "resource_access" block 指定基于某些其他集合生成零个或多个 resource_access block 的规则,在本例中为 var.ms_graph_api_permissioncontent block 描述了如何生成每个 block 的主体,其中 resource_access 是引用 var.ms_graph_api_permission 的当前元素的局部符号。

在将配置传递给提供者之前,Terraform Core 会将其动态扩展为一系列单独的 resource_access block ,因此从提供者的角度来看,这相当于您在原始示例中写出了一系列静态 resource_access block 。

关于azure - Terraform 单变量问题的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76921688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com