Moo*_*rse 1 azure azure-resource-manager azure-policy
我的资源组有一个environment标签,其中只允许特定值:"dev,test,prod"。我想使用 Azure 策略强制执行此操作,该策略将拒绝所有标记中没有此"dev,test,prod"值之一的资源组创建environment。我的策略代码如下:
{
"properties": {
"displayName": "Allowed tag values for Resource Groups",
"description": "This policy enables you to restrict the tag values for Resource Groups.",
"policyType": "Custom",
"mode": "Indexed",
"metadata": {
"version": "1.0.0",
"category": "Tags"
},
"parameters": {
"allowedTagValues": {
"type": "array",
"metadata": {
"description": "The list of tag values that can be specified when deploying resource groups",
"displayName": "Allowed tag values"
},
"defaultValue": [
"dev","test","prod"
]
}
},
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"field": "tags[environment]",
"notIn": "[parameters('allowedTagValues')]"
}
]
},
"then": {
"effect": "deny"
}
}
},
"id": "/providers/Microsoft.Authorization/policyDefinitions/xxxxxxx-xxxxxxx-xxxxxxxxxx-xxxxxxx",
"name": "xxxxxxx-xxxxxxx-xxxxxxxxxx-xxxxxxx"
}
Run Code Online (Sandbox Code Playgroud)
这根本没有任何效果。我也尝试过这个:
{
"not": {
"field": "tags[environment]",
"in": "[parameters('allowedTagValues')]"
}
}
Run Code Online (Sandbox Code Playgroud)
这也不起作用。
有什么建议吗?
您需要将标记值"dev","test","prod"作为参数的允许值传递listofallowedTags,如下所示。
根据您的要求,我们创建了以下策略定义。我们已经在本地环境中对此进行了测试,运行良好。
{
"mode": "All",
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/subscriptions/resourceGroups"
},
{
"not": {
"field": "[concat('tags[', parameters('tagName'), ']')]",
"in": "[parameters('listofallowedtagValues')]"
}
}
]
},
"then": {
"effect": "[parameters('effect')]"
}
},
"parameters": {
"effect": {
"type": "String",
"metadata": {
"displayName": "Effect",
"description": "Enable or disable the execution of the audit policy"
},
"allowedValues": [
"Audit",
"Deny",
"Disabled"
],
"defaultValue": "Deny"
},
"tagName": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": "Name of the tag, such as 'environment'"
},
"defaultValue": "environment"
},
"listofallowedtagValues": {
"type": "Array",
"metadata": {
"displayName": "Tag Values",
"description": "Value of the tag, such as 'production'"
},
"allowedValues": [
"dev",
"test",
"prod"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:如下图所示,自定义策略已分配给订阅。
以下是一些示例输出供参考:
listofallowedtagValues参数 & 中定义的 3 个值不同的值,但在部署资源组时,由于不满足策略要求而失败。test资源组部署成功,因为它满足策略要求。| 归档时间: |
|
| 查看次数: |
5414 次 |
| 最近记录: |