【发布时间】:2021-11-03 13:49:57
【问题描述】:
我正在使用 Terraform 设置多个标记策略定义。我已经在自定义 json 文件中提到了带有策略定义 id 和参数的标签定义。
tag_definitions.json
[
{
"parameters": {
"tagName": {
"value": "cost_center"
},
"tagValue": {
"value": "${cost_center}"
}
},
"policyDefinitionId": "/providers/Microsoft.Authorization/policyDefinitions/XXXXXXX-XXXXXXX"
}
]
main.tf
resource "azurerm_policy_set_definition" "tag_definition" {
name = "${var.subscription_name}-tag-definition"
display_name = "${var.subscription_name}-tag-definition"
description = "Append the default tags definition"
policy_type = "Custom"
policy_definitions = templatefile("${path.module}/templates/tag_definitions.json",
{ cost_center = var.cost_center })
metadata = <<METADATA
{ "category" : "Tags" }
METADATA
lifecycle { ignore_changes = [metadata] }
}
在上面的 main.tf 文件中,policy_definitions 参数现在已被弃用。而不是我想使用policy_definition_reference 参数。
我已关注azurerm_policy_set_definition 文档。但本文档不包含有关如何使用“policy_definition_reference”块设置多个策略定义的信息。
我想将 policy_definition_reference 块与我的自定义 tag_definitions.json 文件以及参数值一起使用。
基于以下 cmets,我尝试使用以下代码。但它没有按预期工作。
tag_definitions_params.json
[
{
"parameters": {
"tagName": {
"value": "common.cost_center"
},
"tagValue": {
"value": "${cost_center}"
}
}
},
{
"parameters": {
"tagName": {
"value": "common.env_type"
},
"tagValue": {
"value": "${env_type}"
}
}
}
]
main.tf
resource "azurerm_policy_set_definition" "tag_definition" {
name = "${var.subscription_name}-tag-definition"
display_name = "${var.subscription_name}-tag-definition"
description = "Append the default tags definition"
policy_type = "Custom"
#policy_definitions = templatefile("${path.module}/templates/tag_definitions.json",
#{ cost_center = var.cost_center, env_type = var.env_type, owner_accountname = var.owner_accountname, product = var.product })
policy_definition_reference {
policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/XXXXXXXX"
parameter_values = templatefile("${path.module}/templates/tag_definitions_params.json",
{
cost_center = var.cost_center,
env_type = var.env_type
})
}
policy_definition_reference {
policy_definition_id = "/providers/Microsoft.Authorization/policyDefinitions/XXXXXXXXX"
parameter_values = templatefile("${path.module}/templates/tag_definitions_params.json",
{
cost_center = var.cost_center,
env_type = var.env_type
})
}
metadata = <<METADATA
{ "category" : "Tags" }
METADATA
lifecycle { ignore_changes = [metadata] }
}
【问题讨论】:
-
你收到错误了吗?
-
@MoonHorse,我收到此错误
expandingpolicy_definition_reference: unmarshallingparameter_values: json: cannot unmarshal array into Go value of type map[string]*policy.ParameterValuesValue'。
标签: azure terraform-provider-azure azure-policy