azure search through images with JMESPath

39 Views Asked by At

I'm looking for a way to search through installable virtual machine images for image size.

The az vm image show has the following data

{
  "architecture": "x64",
  "automaticOsUpgradeProperties": {
    "automaticOsUpgradeSupported": false
  },
  "dataDiskImages": [],
  "disallowed": {
    "vmDiskType": "Unmanaged"
  },
  "extendedLocation": null,
  "features": [
    {
      "name": "SecurityType",
      "value": "TrustedLaunchSupported"
    },
    {
      "name": "IsAcceleratedNetworkSupported",
      "value": "True"
    },
    {
      "name": "DiskControllerTypes",
      "value": "SCSI, NVMe"
    },
    {
      "name": "IsHibernateSupported",
      "value": "True"
    }
  ],
  "hyperVGeneration": "V2",
  "id": "/Subscriptions/c1198d2b-175a-4ed3-acf0-7715e4cd23ac/Providers/Microsoft.Compute/Locations/westus/Publishers/Canonical/ArtifactTypes/VMImage/Offers/0001-com-ubuntu-server-noble-daily/Skus/24_04-daily-lts-gen2/Versions/24.04.202312150",
  "imageDeprecationStatus": {
    "alternativeOption": null,
    "imageState": "ScheduledForDeprecation",
    "scheduledDeprecationTime": "2024-03-22T00:00:00+00:00"
  },
  "location": "westus",
  "name": "24.04.202312150",
  "osDiskImage": {
    "operatingSystem": "Linux",
    "sizeInGb": 30
  },
  "plan": null,
  "tags": null
}

What I'm trying to do is

az vm image show \
  --urn Canonical:0001-com-ubuntu-server-noble-daily:24_04-daily-lts-gen2:24.04.202312150 \
  --query 'osDiskImage[?sizeInGb <= "40"]' \
  -o json

and from there to list all Canonical and other linux distros with osDiskSize < 20.

It's probably between JMESPath lists and structs or similar. I'm using bash/zsh.

1

There are 1 best solutions below

4
simon-pearson On

I think what you're trying to do is fundamentally flawed: az vm image show will get the details for a single VM image, so it will always return a JSON object (aka a dictionary, see How to query Azure CLI command output using a JMESPath query), and never an array. JMESPath can only natively filter arrays, not objects, so you cannot filter the results returned from az vm image show with a JMESPath query.

I think your bet is to use a tool like jq in a small post-processing script to get the value of the osDiskImage.sizeInGb property.

Edit: based on β.εηοιτ.βε's excellent comment you can in fact do this natively through JMESPath using the to_array function.

If we issue the following command to Azure CLI:

az vm image show \
  --urn Canonical:0001-com-ubuntu-server-noble-daily:24_04-daily-lts-gen2:24.04.202312150 \
  --query 'to_array(@)[?osDiskImage.sizeInGb <= `40`] | [0]' \
  -o json

This returns the JSON object of the O/S image if the image size in GB is <= 40 GB, otherwise it returns nothing.

Here's a link to a JMESPath playground demoing this.