Contact

Subscribe via Email

Subscribe via RSS/JSON

Categories

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan

Elsewhere

Az CLI find location shortcode

I wanted to use the Az CLI command to find the shortcode of a location. You know: “UAE North” == “uaenorth”

This command gives you a list of locations and their shortcode and other details:

This is fine and I could scroll through the output to see the shortcode I was interested in, but I thought it would be fun if I can pipe it through jq to get exactly what I want.

That didn’t work though as I kept getting the following:

Turns out this is because jq cannot parse the input correctly. This stumped me for a while until I realized that the Az commands have an “–output json” switch to output in JSON. So while the default output looks like it’s JSON, it’s actually formatted and not really JSON.

Here’s a typical output entry for a location:

What I get is an array of JSON entries like the above.

If I want a list of locations and their shortcodes I can do something like the following:

What I am doing here is piping this to jq. And I tell jq to expect an array and iterate through it (that’s what the .[] does). Then I tell jq to select just the “displayName” and “name” elements from each JSON entry in this array, call these “displayName” and “name”, and output this as JSON (that’s what the {} around part of the commands do). The output looks like this:

If I don’t want an output as JSON I can skip the {} and do something like this:

This just returns a list of locations and shortcodes and isn’t very pretty:

Now, back to my original problem. I am only interested in the shortcode of the location I am interested in, so I don’t really need a list. Instead I can do something like this:

I use the select operator here to find the entry whose “displayName” matches what I am interested in. The result is a single shortcode.

If I want the result to be JSON I can enclose it around {} like I did earlier and also add a label. 

The result looks like this:

To make it easier for the future, I created a Bash function out of this:

It’s similar to the above just that I have to pass the argument I give the function over to jq. For this I have to use the --args switch which lets me define a new internal variable called “displayName” which has the value of the argument I give the function ($1 in this case, in quotes so I capture strings with spaces). When I use this variable as $displayName in jq later, I should skip the double quotes (figured this out via trial and error).

That’s all, now I can simply do az_location "UAE North" and get my answer!

Az CLI and multiple accounts

I wanted to use the Az CLI commands with two separate accounts – my work and personal. I thought that if I opened two separate terminal windows that’d do the trick but turns out it doesn’t. Both terminal windows share the same login info etc.

Then I came across this issue. Turns out there’s an environment variable wherein you can set the location of your Azure config. I use macOS and in my case the Az CLI commands create a folder called “.Azure” in my home directory and use that to store info. So I created a new folder called “.Azure-personal” and in the terminal window where I want to login with my personal account I did the following:

After that I used the “az login” etc. commands as usual and was able to have two separate instances running in parallel.

To make it easy I added two functions to my bash profile:

Now I can invoke the appropriate function and it will log me in correctly.

Azure VM stuck on starting/ running/ shutting down

So I was creating a new VM from an image in Azure today. The portal showed it as “Running” but I couldn’t connect to it. Took a look at the Boot Diagnostics and it’s stuck on boot up:

Stopping the VM from the portal or PowerShell made no difference. Finally I had to log a case with Microsoft. 

The engineer from Microsoft didn’t seem to know of any reset option either … he suggested I resize the VM, which I did and that shutdown the VM. (Or maybe it was just a matter of time. It took an hour+ from the time I encountered this issue to when I did the resize. First I had to log a case via the portal, then a Critical Situation Manager contacted me to say she will arrange for an engineer; then the engineer called me and he wanted a screen sharing and Teams call, and I was like dude why don’t you just shutdown or reset the VM from your console. He didn’t want to do that and wanted to document the whole thing so that took some time during which he suggested I do a resize … and some 10 mins after that the VM stopped. So maybe resize helps, or maybe it just needs an hour for stuck VMs to be reset automatically).

Thanks to Microsoft for calling and coordinating promptly, but I do wish sometimes people would not be so tied up in following the “script” and just listen to what the customer is saying (dude, reset the VM) instead of insisting on a screen share and call etc. Would save everyone time. :)

Add multiple Azure VMs to a log analytics workspace for monitoring

I wanted to add a bunch of VMs in a subscription to a log analytics workspace in Azure so I can add it to update management etc. Didn’t find any command when I Googled for this, so here’s a blog post in case anyone else searches for it.

There isn’t anything fancy in what I am doing. The way to do it for a single VM is in this official doc. I just loop around it for all VMs.

Get the workspace ID and key from Log Analytics workspace > [your workspace] > Overview > “1. Connect a data source” > “Windows, Linux, and other sources”

ARM deployment – Accepting legal terms

I haven’t blogged for a while, I know. Except for a few movie posts it’s been pretty silent here. Mostly coz I have been too busy with work and never got a chance to blog. Plus now I use a note taking app (Bear) on my Mac to keep notes, so there’s less requirement for a blog to keep my notes.

I’ve been playing with ARM templates recently and came across the following when I was trying to deploy a Citrix NetScaler (or ADC as they call them now) today:

Azure Error: MarketplacePurchaseEligibilityFailed
Message: Marketplace purchase eligibilty check returned errors. See inner errors for details.
Exception Details:
Error Code: BadRequest
Message: Offer with PublisherId: citrix, OfferId: netscalervpx-121 cannot be purchased due to validation errors. See details for more information.[{“Legal terms have not been accepted for this item on this subscription: ‘xxxx’. To accept legal terms using PowerShell, please use Get-AzureRmMarketplaceTerms and Set-AzureRmMarketplaceTerms API(https://go.microsoft.com/fwlink/?linkid=862451) or deploy via the Azure portal to accept the terms”:”StoreApi”}]

To work around this login to the portal, click to create a new resource, search for “Citrix ADC”, select the one you are interested in deploying (from the drop down), then select “Get started”.

Then go ahead and enable it for all the subscriptions you are interested in. That’s all.