Skip to content

Commit f8869ad

Browse files
authored
Merge pull request #99811 from tfitzmac/1226tags
update tag examples for CLI
2 parents 3df2073 + 05502fb commit f8869ad

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

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

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Tag resources for logical organization
33
description: Shows how to apply tags to organize Azure resources for billing and managing.
44
ms.topic: conceptual
5-
ms.date: 12/05/2019
5+
ms.date: 12/27/2019
66
---
77
# Use tags to organize your Azure resources
88

@@ -169,7 +169,7 @@ Set-AzResourceGroup -Tag @{} -Name examplegroup
169169

170170
To see the existing tags for a *resource group*, use:
171171

172-
```azurecli
172+
```azurecli-interactive
173173
az group show -n examplegroup --query tags
174174
```
175175

@@ -184,82 +184,95 @@ That script returns the following format:
184184

185185
Or, to see the existing tags for a *resource that has a specified name, type, and resource group*, use:
186186

187-
```azurecli
187+
```azurecli-interactive
188188
az resource show -n examplevnet -g examplegroup --resource-type "Microsoft.Network/virtualNetworks" --query tags
189189
```
190190

191191
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:
192192

193-
```azurecli
193+
```azurecli-interactive
194194
az resource show --id <resource-id> --query tags
195195
```
196196

197197
To get resource groups that have a specific tag, use `az group list`:
198198

199-
```azurecli
199+
```azurecli-interactive
200200
az group list --tag Dept=IT
201201
```
202202

203203
To get all the resources that have a particular tag and value, use `az resource list`:
204204

205-
```azurecli
205+
```azurecli-interactive
206206
az resource list --tag Dept=Finance
207207
```
208208

209-
Every time you apply tags to a resource or a resource group, you overwrite the existing tags on that resource or resource group. Therefore, you must use a different approach based on whether the resource or resource group has existing tags.
209+
When adding tags to a resource group or resource, you can either overwrite the existing tags or append new tags to existing tags.
210210

211-
To add tags to a *resource group without existing tags*, use:
211+
To overwrite the existing tags on a resource group, use:
212212

213-
```azurecli
214-
az group update -n examplegroup --set tags.Environment=Test tags.Dept=IT
213+
```azurecli-interactive
214+
az group update -n examplegroup --tags 'Environment=Test' 'Dept=IT'
215215
```
216216

217-
To add tags to a *resource without existing tags*, use:
217+
To append a tag to the existing tags on a resource group, use:
218+
219+
```azurecli-interactive
220+
az group update -n examplegroup --set tags.'Status'='Approved'
221+
```
222+
223+
To overwrite the tags on a resource, use:
218224

219-
```azurecli
220-
az resource tag --tags Dept=IT Environment=Test -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
225+
```azurecli-interactive
226+
az resource tag --tags 'Dept=IT' 'Environment=Test' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
221227
```
222228

223-
To add tags to a resource that already has tags, retrieve the existing tags, reformat that value, and reapply the existing and new tags:
229+
To append a tag to the existing tags on a resource, use:
224230

225-
```azurecli
226-
jsonrtag=$(az resource show -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks" --query tags -o json)
227-
rt=$(echo $jsonrtag | tr -d '"{},' | sed 's/: /=/g')
228-
az resource tag --tags $rt Project=Redesign -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
231+
```azurecli-interactive
232+
az resource update --set tags.'Status'='Approved' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
229233
```
230234

231235
To apply all tags from a resource group to its resources, and *not keep existing tags on the resources*, use the following script:
232236

233-
```azurecli
234-
groups=$(az group list --query [].name --output tsv)
235-
for rg in $groups
237+
```azurecli-interactive
238+
jsontags=$(az group show --name examplegroup --query tags -o json)
239+
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')
240+
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
241+
for id in $resourceids
236242
do
237-
jsontag=$(az group show -n $rg --query tags -o json)
238-
t=$(echo $jsontag | tr -d '"{},' | sed 's/: /=/g')
239-
r=$(az resource list -g $rg --query [].id --output tsv)
240-
for resid in $r
241-
do
242-
az resource tag --tags $t --id $resid
243-
done
243+
az resource tag --tags $tags --id $id
244244
done
245245
```
246246

247247
To apply all tags from a resource group to its resources, and *keep existing tags on resources*, use the following script:
248248

249-
```azurecli
250-
groups=$(az group list --query [].name --output tsv)
251-
for rg in $groups
249+
```azurecli-interactive
250+
jsontags=$(az group show --name examplegroup --query tags -o json)
251+
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')
252+
253+
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
254+
for id in $resourceids
255+
do
256+
resourcejsontags=$(az resource show --id $id --query tags -o json)
257+
resourcetags=$(echo $resourcejsontags | tr -d '"{},' | sed 's/: /=/g')
258+
az resource tag --tags $tags$resourcetags --id $id
259+
done
260+
```
261+
262+
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.
263+
264+
```azurecli-interactive
265+
jsontags=$(az group show --name examplegroup --query tags -o json)
266+
tags=$(echo $jsontags | tr -d '{}"' | sed 's/: /=/g' | sed "s/\"/'/g" | sed 's/, /,/g' | sed 's/ *$//g' | sed 's/^ *//g')
267+
origIFS=$IFS
268+
IFS=','
269+
read -a tagarr <<< "$tags"
270+
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
271+
for id in $resourceids
252272
do
253-
jsontag=$(az group show -n $rg --query tags -o json)
254-
t=$(echo $jsontag | tr -d '"{},' | sed 's/: /=/g')
255-
r=$(az resource list -g $rg --query [].id --output tsv)
256-
for resid in $r
257-
do
258-
jsonrtag=$(az resource show --id $resid --query tags -o json)
259-
rt=$(echo $jsonrtag | tr -d '"{},' | sed 's/: /=/g')
260-
az resource tag --tags $t$rt --id $resid
261-
done
273+
az resource tag --tags "${tagarr[@]}" --id $id
262274
done
275+
IFS=$origIFS
263276
```
264277

265278
## Templates

0 commit comments

Comments
 (0)