Create an azure policy to apply multiple tags on multiple azure resource types, where tagname and tagvalues will be input of type an array of objects, If tags doesn't exists on the resource

2023-05-16T08:34:10.01+00:00

I am trying to create a policy definition for adding multiple tags, where tag name and tag values will be input of type an array of objects.
if any tags in the array are missing for the resource, those tags must me added on already existing resources.

Azure SQL Database
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,036 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,018 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SwathiDhanwada-MSFT 18,996 Reputation points Moderator
    2023-05-23T07:03:58.3533333+00:00

    @Prarthan Jain (L&T INFOTECH LIMITED) Welcome to Microsoft Q & A Community Forum. Here is a sample policy for your reference. This policy checks whether tags which are part of arrays exists or not. If it does not exist, it automatically adds the tag name and tag value to the resource. If the tag exists but value is not expected, it shows as non-compliant and you can remediate by creating remediation task.

    {
    	"mode": "All",
    	"policyRule": {
    		"if": {
    			"allOf": [
    				{
    					"field": "type",
    					"in": [
    						"Microsoft.Compute/virtualMachines",
    						"Microsoft.Storage/storageAccounts",
    						"Microsoft.Network/networkInterfaces"
    					]
    				},
    				{
    					"anyOf": [
    						{
    							"not": {
    								"field": "[concat('tags[', parameters('tags')[0].tagName, ']')]",
    								"exists": "true"
    							}
    						},
    						{
    							"not": {
    								"field": "[concat('tags[', parameters('tags')[1].tagName, ']')]",
    								"exists": "true"
    							}
    						}
    					]
    				}
    			]
    		},
    		"then": {
    			"effect": "modify",
    			"details": {
    				"roleDefinitionIds": [
    					"/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
    				],
    				"operations": [
    					{
    						"operation": "addOrReplace",
    						"field": "[concat('tags[', parameters('tags')[0].tagName, ']')]",
    						"value": "[parameters('tags')[0].tagValue]"
    					},
    					{
    						"operation": "addOrReplace",
    						"field": "[concat('tags[', parameters('tags')[1].tagName, ']')]",
    						"value": "[parameters('tags')[1].tagValue]"
    					}
    				]
    			}
    		}
    	},
    	"parameters": {
    		"tags": {
    			"type": "Array",
    			"metadata": {
    				"displayName": "tags",
    				"description": "The tags to apply to the resources."
    			},
    			"defaultValue": [
    				{
    					"tagName": "Environment",
    					"tagValue": "Production"
    				},
    				{
    					"tagName": "Department",
    					"tagValue": "IT"
    				}
    			]
    		}
    	}
    }
    
    
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.