Search code examples
azure-clijmespath

Concatenating a query expression in Azure CLI


I'm trying to query a list of soft-deleted Azure key vaults, then determine if the name of a specific key vault is listed.

 keyVaultName=whatever
 az keyvault list-deleted --query "[?contains(name, $keyVaultName)]" -o json

That produces the output...

az keyvault list-deleted: error: argument --query: invalid jmespath_type value: '[?contains(name,whatever)]'

So apparently I need to wrap that variable name in single quotes - but how? Is there some sort of string concatenation function?


Solution

  • The contains return true or false, the format is

    boolean contains(array|string $subject, any $search)
    

    You could try this

    kv=nancykeyvault
    az keyvault list-deleted --query "contains([].name,'$kv')"
    

    enter image description here

    Or, just wrap the variable in single quotes like this, it by default outputs as the JSON format.

    az keyvault list-deleted --query "[?contains(name,'$kv')]"
    

    enter image description here