Azure上使用Jmespath快速查找VM镜像

*本文在wekang的博文"Azure创建隐藏版本VM实例"基础上完成,在此特别感谢。也请大家多去看看wekang的博文,干货多多哦。
*本文下述内容均以AZURE CLI 2.x为例。操作环境是Windows10.
azure上使用portal create vm的命令比较简单。而且在image市场当中可以有各种各样的image选择,很多调整操作系统的时间可以节约。
比如用下面一条命令就可以很快得到一个标准的Ubuntu Linux的vm:

az vm create --resource-group myResourceGroup --name myVM --image UbuntuLTS --admin-username azureuser  --generate-ssh-keys --size Standard_A0  

但是当我们开始用命令行去创建vm的时候,就会发现portal当中的image找起来很是麻烦。
azure文档标准步骤分为四步。
第一步是先用下列命令找到 publishers 列表

az vm image list-publishers --location chinanorth --output table  
C:\Users\xxx>az vm image list-publishers --location chinanorth --output table  
Location    Name  
----------  --------------------------------------------------------   
chinanorth  360-cn  
chinanorth  A10Networks  
....[此处省略200行]  
chinanorth  zilliz-cn  
chinanorth  zzxt  

*假设我们知道ubuntu image在chinanorth publish image最多的是canonical.
第二步是找到对应pulisher的offer列表,此处以publisher canonical为例

az vm image list-offers --publisher canonical --location chinanorth --output table 
C:\Users\xxx>az vm image list-offers --publisher canonical --location chinanorth --output table  
Location    Name  
----------  ------------------------------------  
chinanorth  0001-com-ubuntu-minimal-focal-daily  
chinanorth  0001-com-ubuntu-server-eoan  
....[此处省略5行]  
chinanorth  0003-com-ubuntu-minimal-eoan-daily  
chinanorth  UbuntuServer  

第三步是找到对应pulisher,offer的sku列表,此处以publisher canonical, offer server为例
*publisher, offer使用部分名称即可

az vm image list-skus --publisher canonical --offer UbuntuServer --location chinanorth --output table  
C:\Users\xxx>az vm image list-skus --publisher canonical --offer UbuntuServer --location chinanorth --output table  
Location Name  
---------- --------------------  
chinanorth 12.04.5-LTS  
chinanorth 14.04.2-LTS  
...[此处省略20行]  
chinanorth 16.04.0-LTS  
...[此处省略20行]  
chinanorth 19_10-daily-gen2  

第四步是找到对应publisher,offer,sku的urn

C:\Users\xxx>az vm image list --all --publisher canonical --offer ubuntuserver --sku 16.04-LTS --location chinanorth --output table
Offer         Publisher    Sku        Urn                                               Version
------------  -----------  ---------  ------------------------------------------------  ---------------
UbuntuServer  Canonical    16.04-LTS  Canonical:UbuntuServer:16.04-LTS:16.04.201701130  16.04.201701130
UbuntuServer  Canonical    16.04-LTS  Canonical:UbuntuServer:16.04-LTS:16.04.201702020  16.04.201702020
.....[此处省略200行]
UbuntuServer  Canonical    16.04-LTS  Canonical:UbuntuServer:16.04-LTS:16.04.202003170  16.04.202003170
UbuntuServer  Canonical    16.04-LTS  Canonical:UbuntuServer:16.04-LTS:16.04.202004010  16.04.202004010

这个时候,才终于可以选择特定的urn来create虚机。

az vm create --name ubun-B1ls-vm01 --resource-group jucuiRG --image Canonical:UbuntuServer:16.04-LTS:16.04.202001070 -size Standard_B1ls --admin-username azureuser -ssh-publickey-file D:\Users\xxx\.ssh\id_rsa.pub

以上四步操作按部就班,可以节约一些不必要的服务器端数据索取。缩短服务器查询时间。
但很多同学到这里都会有一大堆问题了,比如我怎么会知道Ubuntu的image是Canonical publish的?
要是我找一个比较冷门的image岂不是只能从portal来找了么?
......好像还不如用portal鼠标点来的快嘛!

好吧好吧,博主为了避免被大家吐槽准备了一些小知识,可以解决大家的问题。
答案就是Jmespath。

用过azure cli的同学们都知道其实azure cli的默认输出是这样的:

[
  {
    "id": "/Subscriptions/xxxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxx/Providers/Microsoft.Compute/Locations/chinanorth/Publishers/canonical/ArtifactTypes/VMImage/Offers/0001-com-ubuntu-minimal-focal-daily",
    "location": "chinanorth",
    "name": "0001-com-ubuntu-minimal-focal-daily",
    "tags": null
  }
...........[此处省略n行和m个{}]
]

是不是每次看到就有眩晕的感觉?嗯,这就对了,因为json并不是设计来方便人类理解的。这种格式最适合程序API之间交换数据。所以好心的博主在前面都加了 --output table,整理成了列表输出,以防大家文章没读完就弃疗了。
json格式的数据是可以过滤的,具体的操作可以参考官网教程 https://jmespath.org/tutorial.html
上面的网页是可以让你贴入自己的输入结果和过滤表达式,实时查看输出结果的,确实是一个很好的初学者练习环境。
不过不熟悉jmespath的同学拿到这个教程,翻完就会发现。官网的老师之讲了一年级的题目,高阶的过滤任务只留下过滤原则,没有sample代码。比如要想一次就找到azure image list里 offer 和sku都包含ubuntu的urn,不仅要靠好好学习,天天向上,还需要一点参悟的灵气。
为了安抚心中万马奔腾的同学们。博主准备了简单的答案:

az vm image list --all --query "[?contains(urn,'Ubuntu')]" --output table | findstr 16.04 

或者你也可以用两个contains 建立或的关系:

az vm image list --all --query "[?contains(offer,'Ubuntu')]||[?contains(sku,'Ubuntu')]" --output table | findstr 16.04

*[?contains(offer,'Ubuntu')]表示在offer这个key里面找到包含Ubuntu关键字的匹配项,目前还是只能大小写敏感.
*||表示前后两个contains条件为或的关系: sku和offer里只要有一个包含ubuntu关键字就命中结果。此外还可以使用&&.
*最后额外使用了windows的findstr来进一步过滤,只显示16.04版本的Ubuntu
*命令执行需要比较长的时间--我等待了3分钟左右

C:\Users\xxx>az vm image list --all --query "[?contains(urn,'Ubuntu')]" --output table | findstr 16.04
You are retrieving all the images from server which could take more than a minute. To shorten the wait, provide '--publisher', '--offer' or '--sku'. Partial name search is supported.
Canonical              UbuntuServer                              16.04-DAILY-LTS       16.04.202002290  Canonical:UbuntuServer:16.04-DAILY-LTS:16.04.202002290
Canonical              UbuntuServer                              16.04-DAILY-LTS       16.04.202003110  Canonical:UbuntuServer:16.04-DAILY-LTS:16.04.202003110
....[]
Canonical              UbuntuServer                              16.04.0-LTS           16.04.202003170  Canonical:UbuntuServer:16.04.0-LTS:16.04.202003170
Canonical              UbuntuServer                              16.04.0-LTS           16.04.202004010  Canonical:UbuntuServer:16.04.0-LTS:16.04.202004010
....[]
Canonical              UbuntuServer                              16_04_0-lts-gen2      16.04.202003170  Canonical:UbuntuServer:16_04_0-lts-gen2:16.04.202003170
Canonical              UbuntuServer                              16_04_0-lts-gen2      16.04.202004010  Canonical:UbuntuServer:16_04_0-lts-gen2:16.04.202004010

这下我们确认了整个市场里Canonical发布的Ubuntu 16.04版本的image应该是最多的。
现在可以使用经过全集搜索得到的urn来创建我们的vm了。这次可以选择一个2020年3月的发布(urn Canonical:UbuntuServer:16.04.0-LTS:16.04.202003170)试试。

az vm create --name ubun-B1ls-vm01 --resource-group jucuiRG --image Canonical:UbuntuServer:16.04.0-LTS:16.04.202003170 -size Standard_B1ls --admin-username azureuser --ssh-key-values c:\Users\xxx\.ssh\id_rsa.pub

*本文在wekang的博文"Azure创建隐藏版本VM实例"基础上完成,在此特别感谢。也请大家多去看看wekang的博文。

附注:
*如果用中国azure需要以下命令:

az cloud set -n AzureChinaCloud

*实验使用的azure cli版本。

az --version
azure-cli                          2.2.0 *
command-modules-nspkg              2.0.3
core                               2.2.0 *
nspkg                              3.0.4
telemetry                          1.0.4
posted @ 2020-04-06 10:57  acui2010  阅读(238)  评论(2)    收藏  举报