【问题标题】:Azure Policy Deny :if one of the tag not present in the resource group nameAzure Policy Deny:如果资源组名称中不存在标记之一
【发布时间】:2019-01-10 21:23:37
【问题描述】:

我已经创建了一个 Azure 策略,如果用户没有使用键“Env”或“使用”指定标签,我想拒绝创建资源组

但是当我使用 Env 标签创建资源组时它会阻止我,它只允许我同时添加 env 和 use 的标签。

根据我的理解,azure 策略中的“anyof”被用作“OR”,但我的代码表现不一样

{
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Resources/subscriptions/resourceGroups"
      },
      {
        "anyof": [
          {
            "field": "tags.Env",
            "exists": false
          },
          {
            "field": "tags.use",
            "exists": false
          }
        ]
      }
    ]
  },
  "then": {
    "effect": "deny"
  }
}

根据 Chris 的建议,我已经处理了标签名称和值,但它在政策中给了我一个错误,它没有采用“NOT”

{
  "mode": "all",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        },
        {
          "not":{
        {
          "field": "tags.Env",
          "equals" : "Prod"
        },
        {
          "field": "tags.OS",
          "equals" : "windows"
        }
                  }
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  },
  "parameters": {}
}

【问题讨论】:

    标签: azure azure-resource-manager azure-resource-group


    【解决方案1】:

    现在,就像您提到的那样,政策正在评估“tags.Env 不存在或 tags.use 不存在”。如果任一标签不存在,您将被拒绝。

    如果“tags.Env 不存在且 tags.use 不存在”,您想要拒绝。这意味着它们都丢失了,这是您要防止的。

    {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Resources/subscriptions/resourceGroups"
          },
          {
             "field": "tags.Env",
             "exists": false
           },
           {
             "field": "tags.use",
             "exists": false
           }
        ]
      },
      "then": {
        "effect": "deny"
      }
    }
    

    【讨论】:

    • 谢谢克里斯,让我试试这个。你也可以让我知道我是否可以在其中添加标签值吗?或者只是我们必须使用标签名称
    • 嗨,克里斯,我尝试使用标签名称和值创建策略。但我无法做到我错了。你能纠正我吗? "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Resources/subscriptions/resourceGroups" }, { "not":{ { "field": "tags.Env" , "equals" : "Prod" }, { "field": "tags.OS", "equals" : "windows" } } } ] }, "then": { "effect": "deny" } },
    • 如果您希望他们使用 Prod 或 windows 作为这两个值,如果不存在则拒绝,那么它是:{ "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Resources/subscriptions/resourceGroups" }, { "field": "tags.Env", "notEquals": "Prod" }, { "field": "tags.OS", "notEquals": "windows" } ] }, "then": { "effect": "deny" } }
    • 感谢 Chris,目前如果我将 Env 指定为 prod 和 OS Linux。它接受并部署 RG,因为它满足一个条件,所以它不会进入下一个条件。
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2021-11-08
    • 2019-10-30
    • 2019-10-19
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多