Forcing naming convention using Policy

Ashar Siddiqui 1 Reputation point
2020-12-06T20:12:27.933+00:00

I would like to know if I can define a policy where I can add a prefix followed by '-' and then variable length of numbers or characters.
I cannot find a logical operator to define variable length in Azure policy as whatever I try is not working.

"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"match": "Microsoft.Compute/virtualMachines"
},
{
"not": {
"field": "name",
"matchInsensitively": "ash361-.....`"

ash361-..... works if I name VM as ash361-abcde or ASH361-12345
but what I want is that prefix is required but then add whatever number of characters for example 'ash361-uciptvm67' or 'ash361-cisIPT4367'

I tried 'ash361-.*' but that did not work.

is there an operator for variable length?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,130 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
793 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SwathiDhanwada-MSFT 17,401 Reputation points
    2020-12-09T11:47:49.833+00:00

    @Ashar Siddiqui Welcome to Microsoft Q & A Community Forum. For your requirement , you can use below sample snippet to verify the prefix of the VM name and also vm name can be any number of characters.

    {  
      "mode": "All",  
      "policyRule": {  
        "if": {  
          "allOf": [  
            {  
              "not": {  
                "value": "[substring(field('name'), 0, 3)]",  
                "equals": "swa"  
              }  
            },  
            {  
              "field": "type",  
              "equals": "Microsoft.Compute/virtualMachines"  
            }  
          ]  
        },  
        "then": {  
          "effect": "deny"  
        }  
      },  
      "parameters": {}  
    }  
    

    Also, to check length of the VM name , you can use this value operator "[length(field('name'))]" in your policy definition.

    1 person found this answer helpful.
    0 comments No comments