Search notes:
az option: --query
If not configured otherwise, az
commands return a JSON object or an array of JSON objects:
$ az vm image list
[
{
"architecture": "x64",
"offer": "CentOS",
"publisher": "OpenLogic",
"sku": "8_5-gen2",
"urn": "OpenLogic:CentOS:8_5-gen2:latest",
"urnAlias": "CentOS85Gen2",
"version": "latest"
},
{
"architecture": "x64",
"offer": "debian-11",
"publisher": "Debian",
"sku": "11-backports-gen2",
"urn": "Debian:debian-11:11-backports-gen2:latest",
"urnAlias": "Debian11",
"version": "latest"
},
…
]
Using the command's --query
option, it's possible to modify and/or filter the returned data.
In the following example, we want to select the returned values in the offer
key only:
$ az vm image list --query [].offer
[
"CentOS",
"debian-11",
"flatcar-container-linux-free",
"openSUSE-leap-15-4",
"RHEL",
"sles-15-sp3",
"0001-com-ubuntu-server-jammy",
"WindowsServer",
"WindowsServer",
…
]
Selecting multiple keys:
$ az vm image list --query '[][ offer, publisher ]'
[
[
"CentOS",
"OpenLogic"
],
[
"debian-11",
"Debian"
],
…
]
Using the -o
(or --output
) option to format the result more nicely:
$ az vm image list --query '[][ offer, publisher ]' -o table
Column1 Column2
---------------------------- ----------------------
CentOS OpenLogic
debian-11 Debian
flatcar-container-linux-free kinvolk
…
Rename the column headings. Note the dot after []
:
$ az vm image list --query '[].{ Offer: offer, Publisher: publisher }' -o table
Offer Publisher
---------------------------- ----------------------
CentOS OpenLogic
debian-11 Debian
flatcar-container-linux-free kinvolk
Filter the result set. Note that the filtered value (here: WindowsServer
) must be placed in single quotes:
$ az vm image list --query "[?offer != 'WindowsServer' ].offer" -o table
Result
----------------------------
CentOS
debian-11
flatcar-container-linux-free
openSUSE-leap-15-4
RHEL
sles-15-sp3
0001-com-ubuntu-server-jammy