使用 ARM 将 Azure 策略应用于管理组

问题描述

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

问题:将此模板部署到管理组会导致“'该位置不需要模板函数‘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']]"