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
To see the existing tags for a *resource group*, use:
171
171
172
-
```azurecli
172
+
```azurecli-interactive
173
173
az group show -n examplegroup --query tags
174
174
```
175
175
@@ -184,82 +184,95 @@ That script returns the following format:
184
184
185
185
Or, to see the existing tags for a *resource that has a specified name, type, and resource group*, use:
186
186
187
-
```azurecli
187
+
```azurecli-interactive
188
188
az resource show -n examplevnet -g examplegroup --resource-type "Microsoft.Network/virtualNetworks" --query tags
189
189
```
190
190
191
191
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:
192
192
193
-
```azurecli
193
+
```azurecli-interactive
194
194
az resource show --id <resource-id> --query tags
195
195
```
196
196
197
197
To get resource groups that have a specific tag, use `az group list`:
198
198
199
-
```azurecli
199
+
```azurecli-interactive
200
200
az group list --tag Dept=IT
201
201
```
202
202
203
203
To get all the resources that have a particular tag and value, use `az resource list`:
204
204
205
-
```azurecli
205
+
```azurecli-interactive
206
206
az resource list --tag Dept=Finance
207
207
```
208
208
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.
210
210
211
-
To add tags to a *resource group without existing tags*, use:
211
+
To overwrite the existing tags on a resource group, use:
212
212
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'
215
215
```
216
216
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:
218
224
219
-
```azurecli
220
-
az resource tag --tags Dept=ITEnvironment=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"
221
227
```
222
228
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:
To apply all tags from a resource group to its resources, and *not keep existing tags on the resources*, use the following script:
232
236
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
236
242
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
244
244
done
245
245
```
246
246
247
247
To apply all tags from a resource group to its resources, and *keep existing tags on resources*, use the following script:
248
248
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
252
272
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')
0 commit comments