This policy requires a specified tag name and value. You specify the tag name and value to enforce.
If you don't have an Azure subscription, create a free account before you begin.
Sample template
JSON
{
"properties": {
"displayName": "Require tag and its value",
"policyType": "BuiltIn",
"mode": "Indexed",
"description": "Enforces a required tag and its value. Does not apply to resource groups.",
"parameters": {
"tagName": {
"type": "String",
"metadata": {
"description": "Name of the tag, such as costCenter"
}
},
"tagValue": {
"type": "String",
"metadata": {
"description": "Value of the tag, such as headquarter"
}
}
},
"policyRule": {
"if": {
"not": {
"field": "[concat('tags[', parameters('tagName'), ']')]",
"equals": "[parameters('tagValue')]"
}
},
"then": {
"effect": "deny"
}
}
},
"id": "/providers/Microsoft.Authorization/policyDefinitions/1e30110a-5ceb-460c-a204-c1c3969c6d62",
"type": "Microsoft.Authorization/policyDefinitions",
"name": "1e30110a-5ceb-460c-a204-c1c3969c6d62"
}
Deploy with the portal
Deploy with PowerShell
This sample requires Azure PowerShell. Run
Get-Module -ListAvailable Az
to find the version. If you need to install or upgrade, see Install Azure PowerShell module.
Run the Connect-AzAccount cmdlet to connect to Azure.
Azure PowerShell
$definition = New-AzPolicyDefinition -Name "enforce-tag-value" -DisplayName "Enforce tag and its value" -description "Enforces a required tag and its value." -Policy 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/enforce-tag-value/azurepolicy.rules.json' -Parameter 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/enforce-tag-value/azurepolicy.parameters.json' -Mode All
$definition
$assignment = New-AzPolicyAssignment -Name <assignmentname> -Scope <scope> -tagName <tagName> -tagValue <tagValue> -PolicyDefinition $definition
$assignment
Clean up PowerShell deployment
Run the following command to remove the resource group, VM, and all related resources.
Azure PowerShell
Remove-AzResourceGroup -Name myResourceGroup
Deploy with Azure CLI
To run this sample, install the latest version of the Azure CLI. To start, run
az login
to create a connection with Azure.
Samples for the Azure CLI are written for the
bash
shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.
Azure CLI
az policy definition create --name 'enforce-tag-value' --display-name 'Enforce tag and its value' --description 'Enforces a required tag and its value.' --rules 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/enforce-tag-value/azurepolicy.rules.json' --params 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/enforce-tag-value/azurepolicy.parameters.json' --mode All
az policy assignment create --name <assignmentname> --scope <scope> --policy "enforce-tag-value"
Clean up Azure CLI deployment
Run the following command to remove the resource group, VM, and all related resources.
Azure CLI
az group delete --name myResourceGroup --yes
No comments:
Post a Comment