使用 ARM 将 Azure Policy 应用到管理组

Apply an Azure Policy to a management group using ARM

目标:将 Azure Policy 部署到管理组,以便在其职权范围内的资源中缺少某些标记时,应用资源组中的指定标记

问题:将此模板部署到管理组会导致“'模板函数 'RESOURCEGROUP' 不应在此位置。"

有一个相当简单的结构类似于:

<Management Group> - <Subscription 1> - <Resource Group 1> - <Resource A>
                                      - <Resource Group 2> - <Resource B>
                   - <Subscription 2> - <Resource Group 3> - <Resource C>
                                                           - <Resource D>

有一个使用嵌套策略定义的相当简单的模板:

......
"resources": [
    {
      "type": "Microsoft.Authorization/policyDefinitions",
      "apiVersion": "2019-09-01",
      "name": ".",
      "properties": {
        "policyType": "Custom",
        "mode": "Indexed",
        "displayName": ".",
        "description": ".",
        "metadata": {
          "category": "Tags"
        },
        "policyRule": {
          "if": {
            "anyOf": [
              {
                "field": "tags['costCenter']",
                "exists": "false"
              },
              {
                "field": "tags['CostCenter']",
                "notin": "[parameters('allowedCostCenter')]"
              }
            ]
          },
          "then": {
            "effect": "modify",
            "details": {
              "roleDefinitionIds": [
                "/providers/Microsoft.Authorization/roleDefinitions/4a9ae827-6dc8-4573-8ac7-8239d42aa03f"
              ],
              "operations": [
                {
                  "operation": "add",
                  "field": "tags['CostCenter']",
                  "value": "[resourcegroup().tags['CostCenter']]"
                }
              ]
            }
          }
        }
      }
    }
  ]

我知道您不能对不在资源组内的项目使用“resourcegroup()”,但指南建议在嵌套模板和“索引”资源上使用它应该有效。

我想要实现的目标可行吗?如果是这样,你能看出我做错了什么吗?

感谢您的帮助!

如果您希望 resourcegroup() 函数作为 Azure Policy 的一部分而不是 MG 范围 ARM 模板执行,则需要添加转义符:

"value": "[[resourcegroup().tags['CostCenter']]"