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

Azure Policy配置:校验资源标签键的大小写敏感性

Azure Policy 标签键严格大小写校验问题

我正在配置Azure Policy以校验新旧资源上是否存在指定标签。仅校验标签存在的规则已轻松实现:

policy_rule = <<RULE
{
  "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourceGroups"
          },
          {
            "field": "[concat('tags[', parameters('TagName'), ']')]",
            "exists": "false"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
}
RULE

对应的参数配置如下:

parameters = <<PARAMETERS
{
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        }
      },
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "The effect determines what happens when the policy rule is evaluated to match"
        }
      }
}
PARAMETERS

但现在我需要让该策略规则同时校验tagName参数的大小写。例如:预期标签键为RigorousMakeMeHappy,则资源必须配置该标签键,而不能是rigorousmakemehappy、rigorousMakeMeHappy或RIGOROUSMAKEMEHAPPY等其他大小写形式。

我已尝试两天但未成功,曾尝试以下规则:

policy_rule = <<RULE
{
  "if": {
        "anyOf": [
          {
          "field": "tags",
          "match": "[parameters('tagName')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
}
RULE

我参考过相关官方文档和社区内容,但仍未解决问题。


补充说明

我需要明确:仅要求标签键严格匹配指定大小写。

使用以下规则时:

policy_rule = <<RULE
{
  "if": {
        "allOf": [
          {
          "not": {
             "field": "[concat('tags[', parameters('tagName'), ']')]",
            "match": "[parameters('tagName')]"
           }
          },
           {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
}

无法达到预期效果:

  • 创建标签键为RigorousMakeMeHappy、值为qsdsqfsdf的VM,校验不通过,因为规则匹配的是键值对的值而非键。
  • 修改标签为键RigorousMakeMeHappy、值RigorousMakeMeHappy的VM,校验通过,同样是因为规则匹配的是值。

感谢您的宝贵帮助!


内容的提问来源于stack exchange,提问作者Greg'ory

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 02:07:08