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

Mat*_*tze 4 azure azure-policy

我制定了以下非常基本的政策,旨在对新资源组强制执行命名约定。

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/resourceGroups"
        },
        {
          "field": "name",
          "notLike": "rg-*"
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {}
}
Run Code Online (Sandbox Code Playgroud)

该策略是在订阅级别分配的,并且policy enforcement = enabled。没有排除情况,正如您从策略中看到的,效果设置为deny

然而,这个政策根本就没有任何效果。我可以noncompliant随意创建名称为 , ... 的新资源组。另外,我等了30多分钟政策才生效(实际上我等了24小时多)。

有趣的是,以下策略生效(几乎在分配后立即生效),唯一的区别是资源类型的比较。

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Network/virtualNetworks"
        },
        {
          "field": "name",
          "notLike": "vnet-*"
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {}
}
Run Code Online (Sandbox Code Playgroud)

我真的不明白这里出了什么问题。在我还没有遇到过的策略背景下,资源组有什么特别之处吗?

Mat*_*tze 5

我通过查看处理资源组的内置策略来解决这个问题。实际上,a 所比较的字符串是错误的。以下政策有效...

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        },
        {
          "field": "name",
          "notLike": "rg-*"
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {}
}
Run Code Online (Sandbox Code Playgroud)

如果有人正在寻找即用型解决方案,请查看我的azure-naming-convention-initiative,它基本上是执行 Microsoft 命名约定建议的策略集合。