gpt4 book ai didi

azure - 附加标签策略不适用于某些资源

转载 作者:行者123 更新时间:2023-12-02 06:44:02 26 4
gpt4 key购买 nike

我正在创建一项 Azure 策略,用于将标签附加到新创建的资源,它适用于大多数组件,但我发现此策略不适用于某些组件,例如:逻辑应用。

{
"mode": "All",
"parameters": {
"Environment": {
"type": "String",
"metadata": {
"displayName": "Environment"
},
"defaultValue": "dev"
},
"Owner": {
"type": "String",
"metadata": {
"displayName": "Owner"
},
"defaultValue": "Admin"
},
"CostCenter": {
"type": "String",
"metadata": {
"displayName": "CostCenter"
},
"defaultValue": "NA"
}
},
"policyRule": {
"if": {
"field": "tags",
"exists": "false"
},
"then": {
"effect": "append",
"details": [
{
"field": "tags",
"value": {
"Environment": "[parameters('Environment')]",
"Owner": "[parameters('Owner')]",
"CostCenter": "[parameters('CostCenter')]"
}
}
]
}
}
}

我添加了类似的策略来将标签应用于资源组,但根本不起作用,我不知道发生了什么。

{
"mode": "All",
"parameters": {
"Environment": {
"type": "String",
"metadata": {
"displayName": "Environment"
},
"defaultValue": "dev"
},
"Owner": {
"type": "String",
"metadata": {
"displayName": "Owner"
},
"defaultValue": "admin"
},
"CostCenter": {
"type": "String",
"metadata": {
"displayName": "CostCenter"
},
"defaultValue": "NA"
}
}
"policyRule": {
"if": {
"allOf": [
{
"field": "tags",
"exists": "false"
},
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
}
]
},
"then": {
"effect": "append",
"details": [
{
"field": "tags",
"value": {
"Environment": "[parameters('Environment')]",
"Owner": "[parameters('Owner')]",
"CostCenter": "[parameters('CostCenter')]"
}
}
]
}
}
}

最佳答案

我自己弄清楚,我的策略中的 "exists": "false" 条件仅在“tags”属性缺失或为空时才会触发,因此具有 "tags": {} 将绕过我的政策,即使它没有任何标签。

另外,简单的对标签进行检查是不合理的,应该通过标签名称逐个检查,如果不满足,则采取追加操作。

而且我还发现以下声明不适用于资源组,可能是因为这是不规则的做法。

{
"then": {
"effect": "append",
"details": [
{
"field": "tags",
"value": {
"Environment": "[parameters('Environment')]",
"Owner": "[parameters('Owner')]",
"CostCenter": "[parameters('CostCenter')]"
}
}
]
}
}

建议改用下面的语句

{
"then": {
"effect": "append",
"details": [
{
"field": "tags['Environment']",
"value": "[parameters('Environment')]"
},
{
"field": "tags['Owner']",
"value": "[parameters('Owner')]"
},
{
"field": "tags['CostCenter']",
"value": "[parameters('CostCenter')]"
}
]
}
}

关于azure - 附加标签策略不适用于某些资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074872/

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