Azure策略为单个标签名称添加标签值

时间:2019-06-17 10:15:15

标签: json azure tags resources policy

需要在Azure中分配策略,在该策略中仅使用指定的标记名称和值来部署资源。 我们需要添加一个Tag名称,根据情况,该名称可以有多个Tag值。

例如

标记名称:环境

标签值:产品,非产品,UAT,测试

因此,这里所有值的标签名称都是相同的,并且值将根据不同情况而相应地更改。相同的代码可能是什么?

3 个答案:

答案 0 :(得分:0)

您可以使用Azure Policy Framework,并且有一个针对您的要求的内置定义-“强制存在标签。不适用于资源组。”

从门户创建分配的步骤已指定here。 这将不允许创建没有标签的资源。 您可以从Azure门户中查看更多内置定义的信息

答案 1 :(得分:0)

我认为您将必须使用Array参数,并为每个标签都有一个策略定义。我试图动态创建一个可用于不同标签的标签,然后再创建一个用于指定多个标签值的选项,但无法使其工作。 https://docs.microsoft.com/en-us/azure/governance/policy/how-to/author-policies-for-arrays

答案 2 :(得分:0)

我会给你我的代码,和你的情况有点类似..在我的情况下,这段代码工作正常:

{
"properties": {
  "displayName": "Enforce Resource Group Tag Values",
  "policyType": "Custom",
  "mode": "All",
  "parameters": {
    "tagEnv": {
      "type": "String",
      "metadata": {
        "displayName": "Tag Environment",
        "description": "Name of the tag, such as 'environment'"
      }
    },
    "allowedEnv": {
      "type": "Array",
      "metadata": {
        "displayName": "Allowed environments",
        "description": "The list of value that can be specified when deploying resources",
        "strongType": "environment"
      },
      "allowedValues": [
        "common",
        "dev",
        "test",
        "prod",
        "research"
      ],
      "defaultValue": [
        "common"
      ]
    }
  },
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Resources/subscriptions/resourceGroups"
        },
        {
          "anyOf": [
            {
                "field": "[concat('tags[', parameters('tagEnv'), ']')]",
                "exists": "false"
            },
            {
                "not": {
                    "field": "[concat('tags[', parameters('tagEnv'), ']')]",
                    "in": "[parameters('allowedEnv')]"
                }
            }
          ]
        }
      ]
    },
    "then": {
      "effect": "deny"
    }
  }
}}
相关问题