如何创建验证资源组名称的 Azure 策略

How can I create an Azure policy that validates Resource Group Names

我正在尝试创建一个我可以在订阅级别分配的 Azure 策略,并控制订阅中资源组的命名。

策略需要针对资源类型或以其他方式限制它们的应用,否则它们将全局应用于所有资源。

我可以使用什么资源类型(或其他方法)来将我的验证仅限于 资源组 名称?

这是我正在尝试的:

$definition = New-AzureRmPolicyDefinition -Name resourceGroupNamePatterns 
   -Description "Restrict resource group names to allowed prefixes only" -Policy '{
    "if": {
        "allOf": [
          {
            "not": {
              "field": "name",
              "like": "Pattern1-*"
            }
          },
          {
            "not": {
              "field": "name",
              "like": "Pattern2-*"
            }
          },
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourcegroups"
          }
        ]
    },
    "then": {
        "effect": "deny"
    }
}'

资源组是Microsoft.Resources/subscriptions/resourcegroups类型。您可以从资源提供者操作中推断出:

Get-AzureRmProviderOperation 'Microsoft.Resources/*'

不确定这个问题是否仍然相关,但在发布时 Azure Policy 不支持对资源组进行评估。

问题中提供的政策定义是正确的。

请尝试更新您的 powershell 版本,并更新策略定义。它将默认为 mode: all,这反过来将启用对资源组的策略评估。

关于策略模式的文档:https://docs.microsoft.com/en-us/azure/azure-policy/policy-definition

模式

模式确定将为策略评估哪些资源类型。支持的模式是:

  • all:评估资源组和所有资源类型
  • indexed: 只评估支持标签和位置的资源类型

我们建议您将模式设置为全部。通过门户创建的所有策略定义都使用 all 模式。如果使用PowerShell或Azure CLI,需要指定mode参数,设置为all。