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--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--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 $ --resource-group $'
{
"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进行过滤和选择。

Azure CLI的Query的更多相关文章

  1. 使用 Azure CLI 管理 Azure 磁盘

    Azure 虚拟机使用磁盘来存储 VM 操作系统.应用程序和数据. 创建 VM 时,请务必选择适用于所需工作负荷的磁盘大小和配置. 本教程介绍如何部署和管理 VM 磁盘. 学习内容: OS 磁盘和临时 ...

  2. 使用 Azure CLI 创建和管理 Linux VM

    Azure 虚拟机提供完全可配置的灵活计算环境. 本教程介绍 Azure 虚拟机的基本部署项目,例如选择 VM 大小.选择 VM 映像和部署 VM. 你将学习如何执行以下操作: 创建并连接到 VM 选 ...

  3. 使用 Azure CLI 2.0 从自定义磁盘创建 Linux VM

    本文说明如何在 Azure 中上传自定义的虚拟硬盘 (VHD) 或复制现有 VHD,并从自定义磁盘创建 Linux 虚拟机 (VM). 可以根据要求安装并配置 Linux 分发版,并使用该 VHD 快 ...

  4. Azure CLI (一)如何安装和配置Azure CLI

    什么是Azure CLI 快速安装 Azure 命令行界面 (Azure CLI),以便使用一组基于 shell 的开源命令在 Azure 中创建和管理资源. 步骤 1:安装 . 登录https:// ...

  5. 在Docker上部署使用Azure CLI镜像

    Docker是非常流行的容器技术,在Docker中安装部署多种工具非常快速和方便:而Azure CLI是微软提供的可以在Linux/Mac上运行的跨平台命令行管理工具,本文介绍如何在Azure上安装部 ...

  6. Linux上使用Azure CLI来管理Azure

    在Windows上我们有强大的Powershell提供各种命令来管理Azure的服务,在Linux上微软提供了基于Node.JS的跨平台的Azure Command Line来帮助Linux用户来管理 ...

  7. Windows系统安装Azure CLI

    本文将介绍在Windos系统下如下安装CLI 1.打开Azure官方链接:https://www.azure.cn/downloads/ 2.按照向导进行安装 3.打开Windows Powershe ...

  8. Azure cli使用arm创建多网卡虚拟机

    登录 Azure CLI 并使用 Resource Manager 模式: azure config mode arm 在以下示例中,请将示例参数名称替换为你自己的值.示例参数名称包括 myResou ...

  9. 学习使用azure CLI创建linux环境

    学习使用azure CLI创建linux环境 选用了容器的方法来登录 docker run -it microsoft/azure-cli 进入交互界面后登录到我的订阅 azure login -e ...

随机推荐

  1. 第四篇、linux系统文件属性三

    一.linux文件属性之文件权限体系介绍 二.linux中连接介绍 三.软连接 四.图解 五文件删除原理 主要内容

  2. java写出图形界面

    1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...

  3. hql学习记录

    ` String hql = "from SysUser o join o.set where owner_id = :newName"; Query query = this.g ...

  4. nginx常见面试题1

    Nginx是网页服务器运维人员不可能绕开的一个弯,剩下几个比较高危的面试范围是:linux基础.网络知识基础.python,或许还会有zabbix等监控工具.这里先说nginx,后面几个肯定也会写. ...

  5. Yii框架和Vue的完美结合完成前后端分离项目

    背景说明 本文假设你对Yii和Vue都比较熟悉,至少都在项目里用过,另外笔者新人,以后不定时放一些干货,欢迎程序媛关注 Yii是一个PHP全端框架,典型的mvc的项目结构,后端接口都是一个控制器里放了 ...

  6. sql server parameter validation of stored procedure

    https://stackoverflow.com/questions/41908156/validating-missing-parameter-from-procedure-calls I don ...

  7. 【bzoj1040】骑士[ZJOI2008](树形dp)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1040 这道题,很明显根据仇恨关系构造出的图形是一堆环套树.如果是普通的树,就可以马上裸树 ...

  8. C# XML对象序列化、反序列化 - PEPE YU

    http://www.tuicool.com/articles/IjE7ban http://www.cnblogs.com/johnsmith/archive/2012/12/03/2799795. ...

  9. IntelliJ Idea中配置、使用技巧(持续更新)

    当前项目配置和全局配置 idea中的File-->settings...和File-->other settings-->Default settings...分别是对当前项目的配置 ...

  10. djang-分页

    分页 views from django.shortcuts import render,HttpResponse # Create your views here. from app01.model ...