对资源组强制执行命名约定的 Azure 策略没有效果

标签 azure azure-policy

为什么以下政策没有效果?我希望它能够阻止创建任何不以 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/

相关文章:

.net - 触发器并发运行时配置一旦指定就无法删除

c# - Swashbuckle - 使用自定义 JSON 模式进行操作

json - 如何检查任何必需的标签是否为空 Azure Policy

azure - 如何在 Azure 策略定义中组合多个语句?

azure - 使用 Azure PowerSHell 部署 Azure Initiative(集定义)

c# - 使用 PDFSharp 时访问 Azure Web App 中的字体时出错

azure - 使用 Python 将多个 PDF 合并为一个(从 Azure Blob 存储下载)的最佳方法是什么?

Azure BICEP 部署(如果尚不存在)

azure - '在类型 'type' 的对象上找不到成员 'PolicyParameter'

json - 如何在 Azure 策略中强制实现标签值模式?