【问题标题】:Azure policy to require a certain Tag be created, but not with a default value?Azure 策略要求创建某个标记,但不使用默认值?
【发布时间】:2019-03-03 19:08:48
【问题描述】:

有没有办法创建 Azure 策略,要求在创建资源时在资源上存在标签,但不检查特定值?我见过的所有例子都是“检查标签 X 是否存在,并且值 Y”。

我只想让用户知道“您需要在此资源上放置标签 X”,因为该值是用户定义的,因此我无法强制执行特定值。

例如 - 我在每个资源上都需要“BillingCode”,但只有创建资源的人才知道他们正确的帐单代码,因为每个人或项目都不同。

【问题讨论】:

    标签: azure


    【解决方案1】:

    您可以使用订阅策略来完成此操作。除非满足某些规则,否则它们将阻止部署 Azure 资源。

    以下示例取自here。 您可以通过使用 notMatch 运算符而不是下面的直接匹配来修改此示例。 More operators here.

    {
       "properties": {
          "displayName": "Enforce tag and its value on resource groups",
          "description": "Enforces a required tag and its value on resource groups.",
          "mode": "All",
          "parameters": {
             "tagName": {
                "type": "String",
                "metadata": {
                   "description": "Name of the tag, such as costCenter"
                }
             },
             "tagValue": {
                "type": "String",
                "metadata": {
                   "description": "Value of the tag, such as headquarter"
                }
             }
          },
          "policyRule": {
             "if": {
                "allOf": [
                   {
                      "field": "type",
                      "equals": "Microsoft.Resources/subscriptions/resourceGroups"
                   },
                   {
                      "not": {
                         "field": "[concat('tags[',parameters('tagName'), ']')]",
                         "equals": "[parameters('tagValue')]"
                      }
                   }
                ]
             },
             "then": {
                "effect": "deny"
             }
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      您需要exists operator

      例如:

      {
          "policyRule": {
              "if": {
                  "field": "[concat('tags[',parameters('tagName'), ']')]",
                  "exists": "false"
              },
              "then": {
                  "effect": "deny"
              }
          },
          "parameters": {
              "tagName": {
                  "type": "String",
                  "metadata": {
                      "description": "Name of the tag, such as costCenter"
                  }
              }
          }
      }
      

      【讨论】:

      • 有没有办法要求tagValue,而不指定任何默认值?
      • @stack247 使用等号,例如` { "field": "[concat('tags[',parameters('tagName'), ']')]", "equals": "" }
      • 谢谢。我用tagValue(而不是tagName)尝试了你的方法,但是在Azure门户中创建资源组时,它不遵守规则。从技术上讲,tagValue"",所以验证通过了。
      • @stack247 将此作为一个单独的问题提出,以便您可以包含整个策略规则。我怀疑你只需要在某个地方翻转一个条件(添加not)。如果它仅不适用于资源组,请记住,您需要使用 "mode": "all" 将策略应用于资源组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多