Search code examples
azureazure-cliazure-vmazure-cli2

Azure cli to filter Instances using tags


I am trying to filter instances based on tags, but it is giving me all the instances present in the resource group. I need to list Instances which has a specific tag. I am using the below command to list instances that have wknhscale == 'active' tag, is there an issue with the command? Also is there any other efficient to achieve this?

az vm list --query '[?tags.wknhscale == 'active'].{Name:name, RG:resourceGroup}' -o table

I am looking for a simple query to fetch Instances with tags, like in gcp

gcloud compute instances list --project test --filter='labels.wknhscale:active AND name ~ .*wkn*' --sort-by=creationTimestamp --format='value(name,zone)'


Solution

  • I was able to query using az graph query. Thanks, @azMantas

    az graph query -q "Resources |  where type =~ 'Microsoft.Compute/virtualMachines' | where tags['wknhscale']=='active' |  where name startswith 'workernode' | project name | order by name asc" | jq '.data[].name'