You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建基于标签名数组的Azure Policy(无需单标签参数)

可行,具体实现方案如下

你完全可以参照“Allowed locations”的数组参数模式,用数组型参数tagNames实现需求——检查资源是否包含数组中所有指定标签名,缺失任意一个就触发审计,无需为每个标签单独创建参数。

核心实现逻辑

  • 定义数组类型的tagNames参数,用来指定必须存在的标签名集合
  • 借助Azure Policy的copy功能遍历数组,为每个标签生成“标签是否存在”的检查规则
  • 通过not+allOf组合判断:只要资源缺失数组中任意一个标签,就触发审计

完整Policy示例代码

{
  "mode": "Indexed",
  "parameters": {
    "tagNames": {
      "type": "Array",
      "metadata": {
        "displayName": "Required Tag Names",
        "description": "List of tag names that must be present on the resource"
      }
    }
  },
  "policyRule": {
    "if": {
      "not": {
        "allOf": "[variables('requiredTags').tags]"
      }
    },
    "then": {
      "effect": "Audit",
      "details": {
        "message": "Resource is missing required tags: [parameters('tagNames')]"
      }
    },
    "variables": {
      "requiredTags": {
        "copy": [
          {
            "name": "tags",
            "count": "[length(parameters('tagNames'))]",
            "input": {
              "field": "[concat('tags[', parameters('tagNames')[copyIndex('tags')], ']')]",
              "exists": "true"
            }
          }
        ]
      }
    }
  }
}

关键细节说明

  • 数组遍历:通过copy循环遍历tagNames数组,为每个标签生成独立的存在性检查条件,避免重复代码
  • 条件判断:not allOf确保只要有一个标签缺失,就满足审计触发条件
  • 参数复用:单个数组参数即可支持任意数量的标签检查,无需额外添加参数

这种实现完全贴合你的需求,和“Allowed locations”的设计思路一致,用数组参数批量管理需要检查的标签名。

内容的提问来源于stack exchange,提问作者dopbolo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 07:43:26