Command Line Wizardry with Azure CLI

The azure cross-platform command line tool is a pretty sweet tool to manage your azure resources and can easily be installable via npm by doing:

npm install azure-cli -g

Of course you can do all sorts of commands such as:

azure account list

info:    Executing command account list
data:    Name                                       Id                                    Tenant Id                             Current
data:    -----------------------------------------  ------------------------------------  ------------------------------------  -------
data:    Strategic Engagements Developers Research  04f7e...                              72f98...                              true   
data:    Microsoft Azure Internal Consumption       8723...                               72f98...                              false  
data:    Microsoft Azure Internal Consumption       d7d3...                               72f98...                              false  
info:    account list command OK

Although the tabular data is great for the eyes, did you know that the azure cli also supports a --json flag, which outputs everything in json? It’s super useful when hooking up the cli to automation:

azure account list --json
[
  {
    "id": "04f7...",
    "name": "Strategic Engagements Developers Research",
    "user": {
      "name": "sedouard@microsoft.com",
      "type": "user"
    },
    "tenantId": "72f7...",
    "isDefault": true,
    "registeredProviders": [],
    "environmentName": "AzureCloud"
  },
  {
    "id": "8723...",
    "name": "Microsoft Azure Internal Consumption",
    "user": {
      "name": "sedouard@microsoft.com",
      "type": "user"
    },
    "tenantId": "72f...",
    "isDefault": false,
    "registeredProviders": [],
    "environmentName": "AzureCloud"
  },
  {
    "id": "d7d3...",
    "name": "Microsoft Azure Internal Consumption",
    "user": {
      "name": "sedouard@microsoft.com",
      "type": "user"
    },
    "tenantId": "727...",
    "isDefault": false,
    "registeredProviders": [],
    "environmentName": "AzureCloud"
  }
]

Parsing the JSON using JQ

Currently I’m doing some bash automation and I’d like to pull some of these values out and set them as environment variables. I don’t really feel like writing a program that’ll unpack the json and using grep with regex matching would probably make me scratch my eyes out.

Turns out there’s a nifty tool out there which is basically available for every platform called jq. You can grab it on linux using yum, or apt, osx via brew and with chocolatey for windows.

You can pipe the JSON output to jq and pull out whatever you want by just specifying a json query string. In the example output above, if I wanted to pull out the tenantId for the 2nd element I would just do:

azure account list --json | jq '.[1].tenantId'
"72f..."

Just like that! In my case I’m doing this to grab some data and export as an environment variable. You can do anything with it really though. Checkout jq’s awesome documentation for more tricks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>