CloudAcademy – Getting Started With Azure Cross-Platform CLI Automation [FCO]
In this demonstration, we will take a look at the JSON output support in the Azure Cross-Platform (xPlat) CLI Tool. By emitting JSON from the Azure xPlat CLI Tool, we can use other JSON parsing tools, such as jsawk or jq, to filter, modify, and sort the data, reducing the need to perform onerous text parsing, and improving the output format of the data.
One of the great things about the Azure cross-platform CLI tool is that we can emit JSON from many of the commands that are available inside of it.
So the first thing we’re going to do is call “Azure config help,” and we’re actually going to change the mode from Azure service management, which we’re currently in, over to the Azure resource manager mode. So now we’ve changed contexts into ARM successfully. Any commands that we issue from now on will be executed against the resource manager API instead of the classic service management API.
Now, this command, or this parameter, rather, works on many other Azure commands, as well. So one of the key concepts in Azure resource manager is called a resource group, which contains individual resources. So if we call “Azure group list,” we’ll get a list of resource groups inside of our subscription. If we append the “–JSON” parameter onto that, you’ll actually see that we get JSON output for each of those resource groups. So the name, location, and provisioning state, as well as the tags, will be emitted as JSON, which is much easier to parse using other command line tools like JQ or the JS Awk utility that parse JSON output instead of tabular output.
So let’s go ahead and just do something really simple, like “return this.name.” So the JS Awk utility is going to look at the array of JSON objects, and for each object, it’s simply going to return the name property. So let’s see what the output of that looks like. So as you can see, the JS Awk utility has returned just the name property from each object, each resource group, in this case, and it’s actually built a JSON array containing strings that represent the names of each resource group.
You can also modify objects in the pipeline using JSON, the JS Awk utility, so we can actually add another property to each of the objects. So, for example, we could call “this.company,” and set that equal to Cloud Academy. So what JS Awk is going to do is take each of the objects in this JSON pipeline, and it’s going to create a new property called “this.company.” So if we run that command, we’ll get very similar JSON output to what we had before, but if we look through the rather complicated output here, you’ll see that we have a property called “company,” and each of these resource groups in this array of objects has a new property called “company” now.
One other thing that we can do with JS Awk is to run an after script. So basically, after each object has been processed in the pipeline…so let’s go back to our earlier example of returning just the resource group names. Once we get this array of strings back, we can run what’s called an after script, and we can “return this.join,” and then we’ll basically join all these strings on a new line character.
So now what we’ve done is we’ve taken the JSON output for these resource groups, we’ve parsed it with the JS Awk utility to just return the name property, and then finally, we took that array of strings and we joined it on a new line character. So now we have just the resource group names, and we can pipe these into other Unix commands to manage them appropriately.