为什么以下政策没有效果?我希望它能够阻止创建任何不以 rg-* 开头的资源组。但是,可以使用资源组名称的任何模式。
{
"properties": {
"policyType": "Custom",
"mode": "All",
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Resources/resourceGroups"
},
{
"field": "name",
"notLike": "rg-*"
}
]
},
"then": {
"effect": "Deny"
}
}
},
"id": "/subscriptions/.../providers/Microsoft.Authorization/policyDefinitions/policy-naming-convention-rg",
"type": "Microsoft.Authorization/policyDefinitions",
"name": "policy-naming-convention-rg"
}
它已正确分配,而且我已经等待了足够长的时间,因此它有时间生效。可用于创建资源组的 ARM 模板如下所示:
所以类型应该匹配,字段也应该匹配。我很困惑。有什么想法吗?
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
"rgName": {
"type": "string"
},
"rgLocation": {
"type": "string"
},
"tags": {
"type": "object",
"defaultValue": {}
}
},
"variables": {},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2018-05-01",
"location": "[parameters('rgLocation')]",
"name": "[parameters('rgName')]",
"properties": {},
"tags": "[parameters('tags')]"
}
],
"outputs": {}
}
最佳答案
请参阅this SO question ,我认为这可能会有所帮助。基本上,定义策略时需要使用 Microsoft.Resources/subscriptions/resourceGroups
而不是 Microsoft.Resources/resourceGroups
。
这不完全是您的用例,但 Microsoft Azure 提供 an example与使用上述命名约定的资源组相关的 Azure 策略。
关于对资源组强制执行命名约定的 Azure 策略没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70116736/