You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
272
267
273
268
```azurecli-interactive
274
-
az resource show --id <resource-id> --query tags
269
+
az group update -n examplegroup --tags 'Environment=Test' 'Dept=IT'
275
270
```
276
271
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:
278
273
279
274
```azurecli-interactive
280
-
az group list --tag Dept=IT
275
+
az group update -n examplegroup --set tags.'Status'='Approved'
281
276
```
282
277
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.
284
279
285
-
```azurecli-interactive
286
-
az resource list --tag Dept=Finance
287
-
```
280
+
### Inherit tags
288
281
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).
290
283
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:
292
287
293
288
```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
295
290
```
296
291
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:
298
293
299
294
```azurecli-interactive
300
-
az group update -n examplegroup --set tags.'Status'='Approved'
295
+
az group show -n examplegroup --query tags
301
296
```
302
297
303
-
To overwrite the tags on a resource, use:
298
+
That script returns the following format:
304
299
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
+
}
307
305
```
308
306
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`:
To get resource groups that have a specific tag, use `az group list`:
316
316
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
+
```
318
320
321
+
### Handling spaces
319
322
320
323
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.
321
324
@@ -335,11 +338,11 @@ IFS=$origIFS
335
338
336
339
## ARM templates
337
340
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.
339
342
340
-
### Apply a literal value to the tag name
343
+
### Apply literal values
341
344
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:
343
346
344
347
```json
345
348
{
@@ -373,9 +376,9 @@ The following example shows a storage account with two tags (`Dept` and `Environ
373
376
374
377
To set a tag to a datetime value, use the [utcNow function](../templates/template-functions-string.md#utcnow).
375
378
376
-
### Apply an object to the tag element
379
+
### Apply an object
377
380
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.
379
382
380
383
```json
381
384
{
@@ -411,7 +414,7 @@ You can define an object parameter that stores several tags, and apply that obje
411
414
}
412
415
```
413
416
414
-
### Apply a JSON string to the tag name
417
+
### Apply a JSON string
415
418
416
419
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:
417
420
@@ -478,6 +481,61 @@ To apply tags from a resource group to a resource, use the [resourceGroup](../te
478
481
}
479
482
```
480
483
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.
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.
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
0 commit comments