【问题标题】:Apply an Azure Policy to a management group using ARM使用 ARM 将 Azure 策略应用到管理组
【发布时间】:2021-05-13 10:37:50
【问题描述】:

目标:将 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()”,但指南建议在嵌套模板和“索引”资源上使用它应该可以工作。

  • 我相当确定管道是正确的,因为我已经部署了多个审核策略
  • 从门户中的试验来看,这应该是可能的
  • 周围有大量的阅读材料,但我没有阅读(或至少理解)似乎对此有所帮助

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

感谢您的帮助!

【问题讨论】:

    标签: azure-resource-manager azure-policy


    【解决方案1】:

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

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

    【讨论】:

    • 啊哈!谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2020-08-31
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多