Azure 自定义标记策略,排除资源类型

Azure Custom Tag Policy, Exclude resource type

我执行了一个 Azure 自定义策略,发现对象不合规,自定义缺少标签,在我的订阅中。

我从这个政策中得到了很多错误,因为它还发现了 oms 代理、扩展等。

这里是json:

    {
  "mode": "All",
  "policyRule": {
    "if": {
      "anyOf": [
        {
          "field": "tags['TAG1']",
          "exists": false
        },
        {
          "field": "tags['TAG2']",
          "exists": false
        }
      ]
    },
    "then": {
      "effect": "audit"
    }
  },
  "parameters": {}
  }

它会搜索所有资源并在它们不带有该标签时对其进行审核。

是否可以针对特定资源类型指定排除项?例如微软。Compute/virtualMachines/extensions等...

谢谢

这样您就可以在 "notEquals" 运算符中提及您不想检查标签的所有资源类型。

{
      "if": {
        "allOf": [
          {
            "field": "type",
            "notEquals": "Microsoft.Security/assessments"
          },
          {
            "field": "type",
            "notEquals": "Microsoft.Compute/VirtualMachines"
          },
          {
            "anyOf": [
              {
                "field": "tags['TAG1']",
                "exists": false
              },
              {
                "field": "tags['TAG2']",
                "exists": false
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "audit"
      }
    }

谢谢,成功了!我正在尝试为如下类型添加其他排除项,但出现错误:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "not": {
            "field": "type",
            "equals": "Microsoft.Security/assessments"
          },
          {
            "field": "type",
            "equals": "Microsoft.Compute/VirtualMachines"
          }
        },
                  {
            "anyOf": [
              {
                "field": "tags['TAG1']",
                "exists": false
              },
              {
                "field": "tags['TAG2']",
                "exists": false
              }
            ]
          }
        ]
      },
    "then": {
      "effect": "audit"
    }
  },
  "parameters": {}
}

是否可以在同一策略中排除更多对象??

使用 "mode": "indexed" 而不是 "mode": "All" 将只匹配支持位置和标签的资源。

来源:https://docs.microsoft.com/en-us/azure/governance/policy/concepts/definition-structure#resource-manager-modes