JMESPATHのクエリー

Azure CLIでshow / list--queryオプションが使えるのだけど、ネストが深い場合などになかなか目的のJMESPATHクエリーが書けなくて、どうしたものか思案していたのだけど、まあ結局jqを使ってやるのが一番楽だなぁ、と。jqがインストールされてなければ、macOSならbrewで。

$ brew install jq

VMが存在するリソースグループに対してlistを実行してjqで整形する。

$ az vm list -g mygroup -o json | jq '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|.[]'
""
"[]"
"[].diagnosticsProfile"
"[].diagnosticsProfile.bootDiagnostics"
"[].diagnosticsProfile.bootDiagnostics.enabled"
"[].diagnosticsProfile.bootDiagnostics.storageUri"
"[].hardwareProfile"
"[].hardwareProfile.vmSize"
"[].id"
"[].location"
"[].name"
"[].networkProfile"
"[].networkProfile.networkInterfaces"
"[].networkProfile.networkInterfaces[]"
"[].networkProfile.networkInterfaces[].id"
"[].networkProfile.networkInterfaces[].resourceGroup"
"[].osProfile"
"[].osProfile.adminUsername"
"[].osProfile.computerName"
"[].osProfile.linuxConfiguration"
"[].osProfile.linuxConfiguration.disablePasswordAuthentication"
"[].osProfile.linuxConfiguration.ssh"
"[].osProfile.linuxConfiguration.ssh.publicKeys"
"[].osProfile.linuxConfiguration.ssh.publicKeys[]"
"[].osProfile.linuxConfiguration.ssh.publicKeys[].keyData"
"[].osProfile.linuxConfiguration.ssh.publicKeys[].path"
"[].osProfile.secrets"
"[].provisioningState"
"[].resourceGroup"
"[].resources"
"[].resources[]"
"[].resources[].id"
"[].resources[].resourceGroup"
"[].storageProfile"
"[].storageProfile.dataDisks"
"[].storageProfile.dataDisks[]"
"[].storageProfile.dataDisks[].caching"
"[].storageProfile.dataDisks[].createOption"
"[].storageProfile.dataDisks[].lun"
"[].storageProfile.dataDisks[].managedDisk"
"[].storageProfile.dataDisks[].managedDisk.id"
"[].storageProfile.dataDisks[].managedDisk.resourceGroup"
"[].storageProfile.dataDisks[].name"
"[].storageProfile.imageReference"
"[].storageProfile.imageReference.offer"
"[].storageProfile.imageReference.publisher"
"[].storageProfile.imageReference.sku"
"[].storageProfile.imageReference.version"
"[].storageProfile.osDisk"
"[].storageProfile.osDisk.caching"
"[].storageProfile.osDisk.createOption"
"[].storageProfile.osDisk.managedDisk"
"[].storageProfile.osDisk.managedDisk.id"
"[].storageProfile.osDisk.managedDisk.resourceGroup"
"[].storageProfile.osDisk.name"
"[].storageProfile.osDisk.osType"
"[].type"
"[].vmId"

osDiskのnameは以下のツリーなので

"[].storageProfile.osDisk.name"

クエリーは以下のようになる。

$ az vm list -g mygroup -o tsv --query "[].storageProfile.osDisk.name"
myvm_OsDisk_1_6a9d704eec1d4aabbf8fcbac0b8664c4

もうちょっとややこしい例。AKSの名前から、ノードリソースグループ、つまりk8sのminionのノードのリソースグループにぶら下がるVMのインスタンスサイズ一覧を得たい。

$ az aks list -o json | jq '[path(..)|map(if type=="number" then "[]" else tostring end)|join(".")|split(".[]")|join("[]")]|unique|.[]'
""
"[]"
"[].agentPoolProfiles"
"[].agentPoolProfiles[]"
"[].agentPoolProfiles[].count"
"[].agentPoolProfiles[].maxPods"
"[].agentPoolProfiles[].name"
"[].agentPoolProfiles[].osDiskSizeGb"
"[].agentPoolProfiles[].osType"
"[].agentPoolProfiles[].storageProfile"
"[].agentPoolProfiles[].vmSize"
"[].dnsPrefix"
"[].enableRbac"
"[].fqdn"
"[].id"
"[].kubernetesVersion"
"[].linuxProfile"
"[].linuxProfile.adminUsername"
"[].linuxProfile.ssh"
"[].linuxProfile.ssh.publicKeys"
"[].linuxProfile.ssh.publicKeys[]"
"[].linuxProfile.ssh.publicKeys[].keyData"
"[].location"
"[].name"
"[].networkProfile"
"[].networkProfile.dnsServiceIp"
"[].networkProfile.dockerBridgeCidr"
"[].networkProfile.networkPlugin"
"[].networkProfile.podCidr"
"[].networkProfile.serviceCidr"
"[].nodeResourceGroup"
"[].provisioningState"
"[].resourceGroup"
"[].servicePrincipalProfile"
"[].servicePrincipalProfile.clientId"
"[].type"

仮にAKSを1つしか作成していなければ、以下でいける、と。

$ nodeResourceGroup=$(az aks list -o tsv --query "[].nodeResourceGroup")
$ az vm list -g $nodeResourceGroup -o tsv --query "[].hardwareProfile.vmSize"
Standard_D16s_v3
Standard_D16s_v3
Standard_D16s_v3
タイトルとURLをコピーしました