你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本文介绍如何在部署期间使用 Azure 资源管理器模板(ARM 模板)标记资源、资源组和订阅。 有关标记建议和限制,请参阅 使用标记来组织 Azure 资源和管理层次结构。
注释
在通过 ARM 模板或 Bicep 文件应用标签时,这些标签将覆盖任何现有标签。
应用值
以下示例使用三个标记部署存储帐户。 其中两个标记(Dept 和 Environment)设置为文本值。 一个标记 (LastDeployed) 设置为默认为当前日期的参数。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"utcShort": {
"type": "string",
"defaultValue": "[utcNow('d')]"
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"tags": {
"Dept": "Finance",
"Environment": "Production",
"LastDeployed": "[parameters('utcShort')]"
},
"properties": {}
}
]
}
应用对象
可以定义一个对象参数,该参数存储多个标记并将该对象应用于标记元素。 此方法比前面的示例更具灵活性,因为对象可以具有不同的属性。 对象中的每个属性都将成为资源的单独标记。 以下示例具有一个名为 tagValues 应用于标记元素的参数。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
},
"tagValues": {
"type": "object",
"defaultValue": {
"Dept": "Finance",
"Environment": "Production"
}
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"tags": "[parameters('tagValues')]",
"properties": {}
}
]
}
应用 JSON 字符串
若要将许多值存储在单个标记中,请应用表示值的 JSON 字符串。 整个 JSON 字符串存储为一个不能超过 256 个字符的标记。 以下示例具有一个名为包含 JSON 字符串中的多个值的标记 CostCenter :
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"tags": {
"CostCenter": "{\"Dept\":\"Finance\",\"Environment\":\"Production\"}"
},
"properties": {}
}
]
}
从资源组应用标记
若要将资源组中的标记应用于资源,请使用 resourceGroup() 函数。 获取标记值时,请使用 tags[tag-name] 语法而不是 tags.tag-name 语法,因为某些字符在点表示法中未正确分析。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]"
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2021-04-01",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"tags": {
"Dept": "[resourceGroup().tags['Dept']]",
"Environment": "[resourceGroup().tags['Environment']]"
},
"properties": {}
}
]
}
将标记应用于资源组或订阅
可以通过部署资源类型 Microsoft.Resources/tags 来将标记添加到资源组或订阅。 可以将标记应用于要部署的目标资源组或订阅。 每次部署模板时,都会替换以前的任何标记。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"tagName": {
"type": "string",
"defaultValue": "TeamName"
},
"tagValue": {
"type": "string",
"defaultValue": "AppTeam1"
}
},
"resources": [
{
"type": "Microsoft.Resources/tags",
"name": "default",
"apiVersion": "2021-04-01",
"properties": {
"tags": {
"[parameters('tagName')]": "[parameters('tagValue')]"
}
}
}
]
}
若要将标记应用到资源组,请使用 Azure PowerShell 或 Azure CLI。 部署到要标记的资源组。
New-AzResourceGroupDeployment -ResourceGroupName exampleGroup -TemplateFile https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
az deployment group create --resource-group exampleGroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
若要将标记应用到订阅,请使用 PowerShell 或 Azure CLI。 部署到要标记的订阅。
New-AzSubscriptionDeployment -name tagresourcegroup -Location westus2 -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
az deployment sub create --name tagresourcegroup --location westus2 --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
有关订阅部署的详细信息,请参阅 在订阅级别创建资源组和资源。
以下模板将对象中的标记添加到资源组或订阅。
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"tags": {
"type": "object",
"defaultValue": {
"TeamName": "AppTeam1",
"Dept": "Finance",
"Environment": "Production"
}
}
},
"resources": [
{
"type": "Microsoft.Resources/tags",
"apiVersion": "2021-04-01",
"name": "default",
"properties": {
"tags": "[parameters('tags')]"
}
}
]
}
后续步骤
- 并非所有资源类型都支持标记。 若要确定是否可以将标记应用于资源类型,请参阅 Azure 资源的标记支持。
- 有关如何实施标记策略的建议,请参阅 资源命名和标记决策指南。
- 有关标记建议和限制,请参阅 使用标记来组织 Azure 资源和管理层次结构。