Azure CLI的Query

Azure CLI 2.0是基于Python的命令行。其命令直观,使用非常方便。

其输出有四种模式:

--output -o : Output format. Allowed values: json, jsonc, table, tsv. Default: json.

其输出各种类型如下:

Table:

az vm list -o table
Name ResourceGroup Location
----------- --------------- ----------
hwmig01 HWMIGT-MIGRATED chinanorth
testfgnew01 TESTFG chinanorth
testfgnew02 TESTFG chinanorth
hwcisco CISCOROUTER chinaeast

TSV:

az vm list -o tsv
None None /subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/HWMIGT-MIGRATED/providers/Microsoft.Compute/virtualMachines/hwmig01 None None None chinanorth hwmig01 None None Succeeded HWMIGT-MIGRATED None None Microsoft.Compute/virtualMachines f8ff9972-32a6-421a-8911-6d3759b9bb6c None
/subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/TESTFG/providers/Microsoft.Compute/virtualMachines/testfgnew01 None None None chinanorth testfgnew01 None None Succeeded TESTFG None Microsoft.Compute/virtualMachines 4a96f861-b4c0-4a5e-ba2b-af341c0fdb03 None
/subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/TESTFG/providers/Microsoft.Compute/virtualMachines/testfgnew02 None None None chinanorth testfgnew02 None None Succeeded TESTFG None Microsoft.Compute/virtualMachines a6f06ea8-ab6f-4351-abcb-2cdfa7537304 None
/subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/CISCOROUTER/providers/Microsoft.Compute/virtualMachines/hwcisco None None None chinaeast hwcisco None Succeeded CISCOROUTER None Microsoft.Compute/virtualMachines 8f7d6ed0-8a95-49c4-bf1a-03a17127125b None

Json:

{
    "availabilitySet": null,
    "diagnosticsProfile": null,
    "hardwareProfile": {
      "vmSize": "Standard_A1"
    },
    "id": "/subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/HWMIGT-MIGRATED/providers/Microsoft.Compute/virtualMachines/hwmig01",
    "identity": null,
    "instanceView": null,
    "licenseType": null,
    "location": "chinanorth",
    "name": "hwmig01",
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/42e8b20d-29ec-40a5-b020-b2229f3dda56/resourceGroups/hwmigt-Migrated/providers/Microsoft.Network/networkInterfaces/hwmig01-PrimaryNic",
          "primary": true,
          "resourceGroup": "hwmigt-Migrated"
        }
      ]
    },
    "osProfile": null,
    "plan": null,
    "provisioningState": "Succeeded",
    "resourceGroup": "HWMIGT-MIGRATED",
    "resources": null,
    "storageProfile": {
      "dataDisks": [],
      "imageReference": null,
      "osDisk": {
        "caching": "ReadWrite",
        "createOption": "attach",
        "diskSizeGb": null,
        "encryptionSettings": null,
        "image": null,
        "managedDisk": null,
        "name": "hwmig01-hwmig01-0-201709060415490504",
        "osType": "Linux",
        "vhd": {
          "uri": "https://tbportalvhdss898wc2ldx4q.blob.core.chinacloudapi.cn/vhds/hwmig-hwmig01-2017-09-06.vhd"
        }
      }
    },
    "tags": null,
    "type": "Microsoft.Compute/virtualMachines",
    "vmId": "f8ff9972-32a6-421a-8911-6d3759b9bb6c",
    "zones": null
  }

Jsonc是有颜色的Json输出。

可以看到table和TSV的输出内容相对比较少,Json输出的内容最丰富,但要截取其中的内容只用grep、awk等各种工具非常不方便。

在Azure CLI中,已经支持了JMESPath query,通过JMESPath query可以精确的把Json的内容取出。

JQuery的官方网站是:

http://jmespath.org/

 

其使用方法如下:

1 筛选内容

比如,资源的类型:

az vm list --query [*].type
[
"Microsoft.Compute/virtualMachines",
"Microsoft.Compute/virtualMachines",
"Microsoft.Compute/virtualMachines",
"Microsoft.Compute/virtualMachines"
]

比如列出所有VM的名称:

az vm list --query [*].name
[
"hwmig01",
"testfgnew01",
"testfgnew02",
"hwcisco"
]

还可以层级的列出VM的存储信息中,系统盘的,OS系统

az vm list --query [*].storageProfile.osDisk.osType
[
"Linux",
"Linux",
"Linux",
"Linux"
]

可以多个内容同时选择:

az vm list --query [*].[name,type,storageProfile.osDisk.osType]
[
  [
    "hwmig01",
    "Microsoft.Compute/virtualMachines",
    "Linux"
  ],
  [
    "testfgnew01",
    "Microsoft.Compute/virtualMachines",
    "Linux"
  ],
  [
    "testfgnew02",
    "Microsoft.Compute/virtualMachines",
    "Linux"
  ],
  [
    "hwcisco",
    "Microsoft.Compute/virtualMachines",
    "Linux"
  ]
]

带标签的输出:

az vm list --query "[].{ VMName:name,OSType:storageProfile.osDisk.osType }"
[
  {
    "OSType": "Linux",
    "VMName": "hwmig01"
  },
  {
    "OSType": "Linux",
    "VMName": "testfgnew01"
  },
  {
    "OSType": "Linux",
    "VMName": "testfgnew02"
  },
  {
    "OSType": "Linux",
    "VMName": "hwcisco"
  }
]

2 条件查询

查出VM名称中包含hw的:

az vm list --query "[?contains(name,'hw')].{ VMName:name,OSType:storageProfile.osDisk.osType }"
[
  {
    "OSType": "Linux",
    "VMName": "hwmig01"
  },
  {
    "OSType": "Linux",
    "VMName": "hwcisco"
  }
]

查出运行状态的VM:

az vm list -d --query "[?contains(powerState,'running')].{Name:name,resourceGroup:resourceGroup}"
[
  {
    "Name": "hwmig01",
    "resourceGroup": "HWMIGT-MIGRATED"
  }
]

或者:

az vm list -d --query "[?(powerState == 'VM running')].{Name:name,resourceGroup:resourceGroup}"
[
  {
    "Name": "hwmig01",
    "resourceGroup": "HWMIGT-MIGRATED"
  }
]

查找出运行的VM,并关闭:

az vm list -d --query "[?(powerState == 'VM running')].{Name:name,resourceGroup:resourceGroup}" -o tsv | xargs -L1 bash -c 'az vm deallocat
e --name $0 --resource-group $1'
{
  "endTime": "2017-09-30T09:03:21.617483+00:00",
  "error": null,
  "name": "a94b4d79-b291-46c8-8447-0768b6df2ebb",
  "startTime": "2017-09-30T09:00:44.157134+00:00",
  "status": "Succeeded"
}

总结:

在使用Azure CLI时,通过Query的选项,可以方便的对输出的Json进行过滤和选择。

 

posted @ 2017-09-30 17:13  衡子  阅读(822)  评论(1编辑  收藏  举报