skip to Main Content

I am trying to create a azure policy that blocks resources from having tag that start’s with "ABC".

POLICY

"parameter" :{
   "tagname":{
      "type":"string",
      "defaultValue":"ABC"
   }
},

"policyRule" :{
    "if" : {
        "allOf" : [
           {
              "field":"type",
              "equals":"Microsoft.Storage/storageAccounts"
           },
           {
               "field":"[concat('tags[', parameter('tagname'), '*', ']')]",
               "exists":true
           }
        ]
    },
    "then":{
        "effect":"deny"
    }
}

Can we use regular expressions like "ABC*" to get tags that starts with ABC in tagname

2

Answers


  1. try this.

    {
      "mode": "All",
      "policyRule": {
        "if": {
          "allOf": [
            {
              "field": "type",
              "equals": "Microsoft.Storage/storageAccounts"
            },
            {
              "not": {
                "field": "[concat('tags[', parameters('tagname'), ']')]",
                "like": "ABC*"
              }
            }
          ]
        },
        "then": {
          "effect": "deny"
        }
      },
      "parameters": {
        "tagname": {
          "type": "String",
          "metadata": {
            "displayName": "Tag Name"
          }
        }
      }
    }
    
    Login or Signup to reply.
  2. I am trying to create a azure policy that blocks resources from having tag that start’s with "ABC".

    Here is the updated policy to block the Tag name start with ABC.

     {
          "mode": "All",
          "policyRule": {
            "if": {
              "allOf": [
                {
                  "field": "tags",
                  "exists": "true"
                },
                {
                  "field": "tags['ABC']",
                  "exists": "true"
                }
              ]
            },
            "then": {
              "effect": "deny"
            }
          },
          "parameters": {}
        }
    

    Policy is denying Resource Group creation if I enter the Tag name "ABC"

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search