There are many possibilities why it is good to tag devices within Azure. You can use the tags for cost analysis, grouping of resources, adding some important information etc. This article gives some hint how you can create an automatic tagging feature to your Azure subscription if you want to tag resources based on their name.
The solution uses Azure policy to achieve this option.
In Azure it is possible to create different policies for different purposes. This is default in all cases. There are huge amount of built-in policy rules which can be either customized or can be used as it is.
In this example I will show you how you can create a custom rule to filter out resources based on their name and tag these resources with a specific tag.
{
"mode": "Indexed",
"policyRule": {
"if": {
"allOf": [
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"exists": "false"
},
{
"field": "name",
"contains":"[parameters('searchValue')]"
}
]
},
"then": {
"effect": "append",
"details": [
{
"field": "[concat('tags[', parameters('tagName'), ']')]",
"value": "[parameters('tagValue')]"
}
]
}
},
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"displayName": "Tag Name",
"description": "Configures the tag name for related resources"
}
},
"tagValue": {
"type": "String",
"metadata": {
"displayName": "Tag Value",
"description": "Configuring the tag value for resources"
}
},
"searchValue": {
"type": "String",
"metadata": {
"displayName": "Search string of resource",
"description": "The policy will find resources which name contains the content of this field"
}
}
}
}