Skip to content

Commit 4d833bf

Browse files
committed
update
1 parent f5eb9c9 commit 4d833bf

File tree

1 file changed

+97
-39
lines changed

1 file changed

+97
-39
lines changed

articles/azure-resource-manager/management/tag-resources.md

Lines changed: 97 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ $subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
200200
Get-AzTag -ResourceId "/subscriptions/$subscription"
201201
```
202202

203-
### List resources by tag
203+
### List by tag
204204

205205
To get resources that have a specific tag name and value, use:
206206

@@ -247,75 +247,78 @@ Remove-AzTag -ResourceId "/subscriptions/$subscription"
247247

248248
## Azure CLI
249249

250-
To see the existing tags for a *resource group*, use:
250+
### Apply tags
251251

252-
```azurecli-interactive
253-
az group show -n examplegroup --query tags
254-
```
252+
When adding tags to a resource group or resource, you can either overwrite the existing tags or append new tags to existing tags.
255253

256-
That script returns the following format:
254+
To overwrite the tags on a resource, use:
257255

258-
```json
259-
{
260-
"Dept" : "IT",
261-
"Environment" : "Test"
262-
}
256+
```azurecli-interactive
257+
az resource tag --tags 'Dept=IT' 'Environment=Test' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
263258
```
264259

265-
Or, to see the existing tags for a *resource that has a specified name, type, and resource group*, use:
260+
To append a tag to the existing tags on a resource, use:
266261

267262
```azurecli-interactive
268-
az resource show -n examplevnet -g examplegroup --resource-type "Microsoft.Network/virtualNetworks" --query tags
263+
az resource update --set tags.'Status'='Approved' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
269264
```
270265

271-
When looping through a collection of resources, you might want to show the resource by resource ID. A complete example is shown later in this article. To see the existing tags for a *resource that has a specified resource ID*, use:
266+
To overwrite the existing tags on a resource group, use:
272267

273268
```azurecli-interactive
274-
az resource show --id <resource-id> --query tags
269+
az group update -n examplegroup --tags 'Environment=Test' 'Dept=IT'
275270
```
276271

277-
To get resource groups that have a specific tag, use `az group list`:
272+
To append a tag to the existing tags on a resource group, use:
278273

279274
```azurecli-interactive
280-
az group list --tag Dept=IT
275+
az group update -n examplegroup --set tags.'Status'='Approved'
281276
```
282277

283-
To get all the resources that have a particular tag and value, use `az resource list`:
278+
Currently, Azure CLI doesn't support applying tags to subscriptions.
284279

285-
```azurecli-interactive
286-
az resource list --tag Dept=Finance
287-
```
280+
### Inherit tags
288281

289-
When adding tags to a resource group or resource, you can either overwrite the existing tags or append new tags to existing tags.
282+
To apply tags from a subscription or resource group to the resources, see [Azure Policies - tags](tag-policies.md).
290283

291-
To overwrite the existing tags on a resource group, use:
284+
### List tags
285+
286+
To see the existing tags for a resource, use:
292287

293288
```azurecli-interactive
294-
az group update -n examplegroup --tags 'Environment=Test' 'Dept=IT'
289+
az resource show -n examplevnet -g examplegroup --resource-type "Microsoft.Network/virtualNetworks" --query tags
295290
```
296291

297-
To append a tag to the existing tags on a resource group, use:
292+
To see the existing tags for a resource group, use:
298293

299294
```azurecli-interactive
300-
az group update -n examplegroup --set tags.'Status'='Approved'
295+
az group show -n examplegroup --query tags
301296
```
302297

303-
To overwrite the tags on a resource, use:
298+
That script returns the following format:
304299

305-
```azurecli-interactive
306-
az resource tag --tags 'Dept=IT' 'Environment=Test' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
300+
```json
301+
{
302+
"Dept" : "IT",
303+
"Environment" : "Test"
304+
}
307305
```
308306

309-
To append a tag to the existing tags on a resource, use:
307+
### List by tag
308+
309+
To get all the resources that have a particular tag and value, use `az resource list`:
310310

311311
```azurecli-interactive
312-
az resource update --set tags.'Status'='Approved' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
312+
az resource list --tag Dept=Finance
313313
```
314314

315-
### Inherit tags
315+
To get resource groups that have a specific tag, use `az group list`:
316316

317-
To apply tags from a subscription or resource group to the resources, see [Azure Policies - tags](tag-policies.md).
317+
```azurecli-interactive
318+
az group list --tag Dept=IT
319+
```
318320

321+
### Handling spaces
319322

320323
If your tag names or values include spaces, you must take a couple of extra steps. The following example applies all tags from a resource group to its resources when the tags may contain spaces.
321324

@@ -335,11 +338,11 @@ IFS=$origIFS
335338

336339
## ARM templates
337340

338-
To tag a resource during deployment, add the `tags` element to the resource you're deploying. Provide the tag name and value.
341+
You can tag resources, resource groups, and subscriptions during deployment with an ARM template.
339342

340-
### Apply a literal value to the tag name
343+
### Apply literal values
341344

342-
The following example shows a storage account with two tags (`Dept` and `Environment`) that are set to literal values:
345+
The following example deploys a storage account with two tags (`Dept` and `Environment`) that are set to literal values:
343346

344347
```json
345348
{
@@ -373,9 +376,9 @@ The following example shows a storage account with two tags (`Dept` and `Environ
373376

374377
To set a tag to a datetime value, use the [utcNow function](../templates/template-functions-string.md#utcnow).
375378

376-
### Apply an object to the tag element
379+
### Apply an object
377380

378-
You can define an object parameter that stores several tags, and apply that object to the tag element. Each property in the object becomes a separate tag for the resource. The following example has a parameter named `tagValues` that is applied to the tag element.
381+
You can define an object parameter that stores several tags, and apply that object to the tag element. This approach provides more flexibility than the previous example because the object can have any properties. Each property in the object becomes a separate tag for the resource. The following example has a parameter named `tagValues` that is applied to the tag element.
379382

380383
```json
381384
{
@@ -411,7 +414,7 @@ You can define an object parameter that stores several tags, and apply that obje
411414
}
412415
```
413416

414-
### Apply a JSON string to the tag name
417+
### Apply a JSON string
415418

416419
To store many values in a single tag, apply a JSON string that represents the values. The entire JSON string is stored as one tag that can't exceed 256 characters. The following example has a single tag named `CostCenter` that contains several values from a JSON string:
417420

@@ -478,6 +481,61 @@ To apply tags from a resource group to a resource, use the [resourceGroup](../te
478481
}
479482
```
480483

484+
### Apply tags to resource groups or subscriptions
485+
486+
You can add tags to a resource group or subscription by deploying the **Microsoft.Resources/tags** resource type. The tags are applied to the target resource group or subscription for the deployment. Each time you deploy the template you replace any tags there were previously applied.
487+
488+
```json
489+
{
490+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
491+
"contentVersion": "1.0.0.0",
492+
"parameters": {
493+
"tagName": {
494+
"type": "string",
495+
"defaultValue": "TeamName"
496+
},
497+
"tagValue": {
498+
"type": "string",
499+
"defaultValue": "AppTeam1"
500+
}
501+
},
502+
"variables": {},
503+
"resources": [
504+
{
505+
"type": "Microsoft.Resources/tags",
506+
"name": "default",
507+
"apiVersion": "2019-10-01",
508+
"dependsOn": [],
509+
"properties": {
510+
"tags": {
511+
"[parameters('tagName')]": "[parameters('tagValue')]"
512+
}
513+
}
514+
}
515+
]
516+
}
517+
```
518+
519+
To apply the tags to a resource group, use either PowerShell or Azure CLI. Deploy to the resource group that you want to tag.
520+
521+
```azurepowershell-interactive
522+
New-AzResourceGroupDeployment -ResourceGroupName exampleGroup -TemplateFile https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
523+
```
524+
525+
```azurecli-interactive
526+
az deployment group create --resource-group exampleGroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
527+
```
528+
529+
To apply the tags to a subscription, use either PowerShell or Azure CLI. Deploy to the subscription that you want to tag.
530+
531+
```azurepowershell-interactive
532+
New-AzSubscriptionDeployment -name tagresourcegroup -Location westus2 -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
533+
```
534+
535+
```azurecli-interactive
536+
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
537+
```
538+
481539
## Portal
482540

483541
[!INCLUDE [resource-manager-tag-resource](../../../includes/resource-manager-tag-resources.md)]

0 commit comments

Comments
 (0)