Search code examples
azureazure-virtual-machineazure-cli

Assigning multiple tags to a resource via Azure CLI results in one big tag when using a variable


When creating VM via az cli (bash) with a list of tags.

First set this variable:
tags='env=qa servertype=frontend Distinct=qa-frontend25 CI=Yes DataDog:True'

However when running the below command in bash

az vm create ... --tags "${tags}"

It creates one long tag that has key env and value qa servertype=frontend Distinct=qa-frontend25 CI=Yes DataDog=True

From Azure CLI documentation

--tags
     Space-separated tags in 'key[=value]' format. 

What am missing here?


Solution

  • If you want to create multi-tag to one VM, you should add the parameter --tags like this:

    --tags 'tag1=test1' 'tag2=test2'
    

    The result like this:

    enter image description here

    It also shows in the document az vm create that you provide. There is a misunderstanding with you.

    --tags Space-separated tags in 'key[=value]' format. Use "" to clear existing tags.

    Update

    Yeah, you can set multi-tag in a variable and take it in the command like this:

    tags="tag1=test1 tag2=test2"
    
    az vm create -g resourceGroupName -n vmName --image image --tags $tags
    

    It will work well and answer for @Johan, if you remove quotes it also works. It's a great question. Actually, the variable works in the CLI command like this:

    --tags $tags -> --tags tag1=test1 tag2=test2
    

    Just like using echo $tags in the bash.