DEV Community

Andrei Kniazev
Andrei Kniazev

Posted on

🔍 Azure CLI. How to Simplify Output.

If you need to extract specific information from the output of an Azure CLI command you can use the --query that will do it for you.

Let's say you want to save a deployement key of the Azure Static Web App in a Bash variable for later use in your CI/CD pipeline. When you run the command:

az staticwebappsecrets list -g rg-name -n static-web-app-name --query
Enter fullscreen mode Exit fullscreen mode

The output will be in JSON format:

{
  "id": "some id",
  "kind": null,
  "location": "West Europe",
  "name": "secrets",
  "properties": {
    "apiKey": "cool key"
  },
  "resourceGroup": "rg-name",
  "type": "Microsoft.Web/staticSites/secrets"
}
Enter fullscreen mode Exit fullscreen mode

But you only need the apiKey value. That's where the --query parameter comes in play. Simply add it to your command:

az staticwebappsecrets list -g rg-name -n static-web-app-name --query 'properties.apiKey'
Enter fullscreen mode Exit fullscreen mode

Now, you can save only the key in a Bash variable:

DEPLOYMENT_TOKEN=$(az staticwebappsecrets list -g rg-name -n static-web-app-name --query 'properties.apiKey')
Enter fullscreen mode Exit fullscreen mode

To remove the surrounding quotes, use the following step:

DEPLOYMENT_TOKEN=${DEPLOYMENT_TOKEN//\"/}
Enter fullscreen mode Exit fullscreen mode

And here you go! You have extracted the key and it is ready to use in your CI/CD pipeline.

Full documentation is here: https://learn.microsoft.com/en-us/cli/azure/query-azure-cli

Top comments (0)