Forum Discussion

Oleg_A's avatar
Oleg_A
Copper Contributor
Oct 10, 2024

Azure Policy require multiple tags with values

I have a policy that requires specific tag with specific values (json below), but I want to require more tags within the same policy also with specific value and not sure how to do it...

 

Is there a way to add more tags with specific values to the same policy?

For example, I want to require two tags:

environment with prod/non-prod and department with Infra/Finance

 

Is it possible?

Thank you!

 

{
  "properties": {
    "displayName": "Require tag environment and its values on resources ",
    "policyType": "Custom",
    "mode": "Indexed",
    "description": "Enforces a required tag environment and its value. Does not apply to resource groups.",
    "metadata": {
      "category": "Tags",
      "createdBy": ""
      "createdOn": ""
      "updatedBy": ""
      "updatedOn": ""
    },
    "version": "1.0.0",
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name1",
          "description": "Name of the tag, such as 'environment'"
        },
        "allowedValues": [
          "environment"
        ]
      },
      "tagValue": {
        "type": "Array",
        "metadata": {
          "displayName": "Tag Value1",
          "description": "Value of the tag, such as 'production'"
        },
        "allowedValues": [
          "prod",
          "non-prod"
        ]
      }
    },
    "policyRule": {
      "if": {
        "not": {
          "field": "[concat('tags[', parameters('tagName'), ']')]",
          "in": "[parameters('tagValue')]"
        }
      },
      "then": {
        "effect": "deny"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  }
}
  • Try this:

     

    {
      "properties": {
        "displayName": "Require tags environment and department with specific values on resources",
        "policyType": "Custom",
        "mode": "Indexed",
        "description": "Enforces required tags environment and department with their values. Does not apply to resource groups.",
        "metadata": {
          "category": "Tags",
          "createdBy": "",
          "createdOn": "",
          "updatedBy": "",
          "updatedOn": ""
        },
        "version": "1.0.0",
        "parameters": {
          "tagName1": {
            "type": "String",
            "metadata": {
              "displayName": "Tag Name1",
              "description": "Name of the tag, such as 'environment'"
            },
            "allowedValues": [
              "environment"
            ]
          },
          "tagValue1": {
            "type": "Array",
            "metadata": {
              "displayName": "Tag Value1",
              "description": "Value of the tag, such as 'prod' or 'non-prod'"
            },
            "allowedValues": [
              "prod",
              "non-prod"
            ]
          },
          "tagName2": {
            "type": "String",
            "metadata": {
              "displayName": "Tag Name2",
              "description": "Name of the tag, such as 'department'"
            },
            "allowedValues": [
              "department"
            ]
          },
          "tagValue2": {
            "type": "Array",
            "metadata": {
              "displayName": "Tag Value2",
              "description": "Value of the tag, such as 'Infra' or 'Finance'"
            },
            "allowedValues": [
              "Infra",
              "Finance"
            ]
          }
        },
        "policyRule": {
          "if": {
            "anyOf": [
              {
                "not": {
                  "field": "[concat('tags[', parameters('tagName1'), ']')]",
                  "in": "[parameters('tagValue1')]"
                }
              },
              {
                "not": {
                  "field": "[concat('tags[', parameters('tagName2'), ']')]",
                  "in": "[parameters('tagValue2')]"
                }
              }
            ]
          },
          "then": {
            "effect": "deny"
          }
        },
        "versions": [
          "1.0.0"
        ]
      }
    }

     

Resources