Search code examples
azureazure-cli

Can't filter resources by name in Azure CLI


When I execute the following command (as suggested here):

az resource list --query "[?location=='swedencentral']"

I get a list of resource groups, as expected. One of them is named se-tyr-business and I see it in the listing clearly containing the following section.

{
"changedTime": "2023-02-21T04:41:33.593799+00:00",
...
"location": "swedencentral",
"name": "se-tyr-business",
...
"type": "Microsoft.Web/sites"
}, ...

So I figured that if I substitute location for name, then the syntax should produce a filtered list by that parameter. However, executing:

az resource list --query "[?name=='se-tyr-business']"

produces an empty list, suggesting that there's no matches. I don't understand what I'm missing. I also tried to get inspiration from other sources suggesting that instead of the condition in the brackets, I may simply pull out a parameter's values.

az resource list --query location --output table
az resource list --query name --output table

However, that produced no content at all (no dummy object, no empty array, nothing, nada, ziltch)! So I'm confused (and surely mistaken) as to how to continue. The official docs on listing is rahter brief and I find it hard to locate informative examples in blogs. Also, a lot of examples seem to target Azure Powershell, which isn't my interest.


Solution

  • I have reproduced in my environment and got expected results as below and followed Microsoft-Document:

    az resource list --query "[?location=='swedencentral']"
    

    Output:

    enter image description here

    Then used:

     az resource list --query "[?name=='CRLB-IP']"
     
    

    Output:

    enter image description here

    enter image description here

    To get the names in particular Loaction:

    az resource list --query "[?location=='swedencentral'].{Name:name}"
    

    Output:

    enter image description here

    az resource list --query "[?location=='swedencentral'].name"
    

    Output:

    enter image description here

    You can use && operator to get particular location and name:

    az resource list --query "[?location=='swedencentral'] && [?name=='CRLB-IP']"
    

    Output:

    enter image description here