多标签意外行为的 Azure 策略

时间:2021-02-10 06:38:47

标签: azure azure-policy

我正在尝试设置 Azure 策略以在创建时在资源组上强制执行多个标记。但是,该策略以一种奇怪的方式运行,即使只添加了一个标签 (tagName1),也会创建 RG 组。我不太确定出了什么问题,因为根据我的理解,allOf 表现为 AND 运算符,并且只有在添加了所有标签后才应创建 RG 组。

我需要一些有关如何调试此问题的指导。谢谢

我的政策:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "[concat('tags[', parameters('tagName1'), ']')]",
          "exists": "false"
        },
        {
          "field": "[concat('tags[', parameters('tagName2'), ']')]",
          "exists": "false"
        },
        {
          "field": "[concat('tags[', parameters('tagName3'), ']')]",
          "exists": "false"
        },
        {
          "field": "[concat('tags[', parameters('tagName4'), ']')]",
          "exists": "false"
        },
        {
          "field": "[concat('tags[', parameters('tagName5'), ']')]",
          "exists": "false"
        },
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {
    "tagName1": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Name",
        "description": "Name of the tag, such as 'environment'"
      }
    },
    "tagName2": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Name",
        "description": "Name of the tag, such as 'environment'"
      }
    },
    "tagName3": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Name",
        "description": "Name of the tag, such as 'environment'"
      }
    },
    "tagName4": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Name",
        "description": "Name of the tag, such as 'environment'"
      }
    },
    "tagName5": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Name",
        "description": "Name of the tag, such as 'environment'"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

仅当所有标签 (1-5) 都不存在时,当前条件才会拒绝创建/更新请求。

使用 anyOf 而不是 allOf。