Eva
Eva

Reputation: 593

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)'

Upvotes: 2

Views: 2818

Answers (3)

resse
resse

Reputation: 71

Your GCP filter query can be transformed to this Azure query

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

You can get more examples here: How to query Azure CLI command output using a JMESPath query

Upvotes: 0

Eva
Eva

Reputation: 593

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'

Upvotes: 3

azMantas
azMantas

Reputation: 232

I would recommend using Azure Resource Graph for this. Resource graph allows you to query all your Azure resources (in all subscriptions that you have access to) using the Kusto language either in the CLI or in the portal.

Upvotes: 0

Related Questions