您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

Azure CLI 命令的输出格式Output formats for Azure CLI commands

Azure CLI 使用 JSON 作为默认输出格式,但提供其他格式。The Azure CLI uses JSON as its default output format, but offers other formats. 使用 --output--out-o)参数设置 CLI 输出的格式。Use the --output (--out or -o) parameter to format CLI output. 输出的参数值和类型为:The argument values and types of output are:

--output--output 说明Description
json JSON 字符串。JSON string. 此设置为默认设置This setting is the default
jsonc 彩色 JSONColorized JSON
yaml YAML,一种机器可读的 JSON 替代格式YAML, a machine-readable alternative to JSON
table 将键作为列标题的 ASCII 表ASCII table with keys as column headings
tsv 制表符分隔值,没有键Tab-separated values, with no keys
none 除了错误和警告以外不输出任何内容No output other than errors and warnings

JSON 输出格式JSON output format

以下示例以默认 JSON 格式显示订阅中的虚拟机列表。The following example displays the list of virtual machines in your subscriptions in the default JSON format.

az vm list --output json

以下输出有为简便起见而省略的一些字段并替换了标识信息。The following output has some fields omitted for brevity, and identifying information replaced.

[
  {
    "availabilitySet": null,
    "diagnosticsProfile": null,
    "hardwareProfile": {
      "vmSize": "Standard_DS1"
    },
    "id": "/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010",
    "instanceView": null,
    "licenseType": null,
    "location": "westus",
    "name": "DemoVM010",
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "/subscriptions/.../resourceGroups/demorg1/providers/Microsoft.Network/networkInterfaces/DemoVM010VMNic",
          "primary": null,
          "resourceGroup": "demorg1"
        }
      ]
    },
          ...
          ...
          ...
]

YAML 输出格式YAML output format

yaml 格式将输出打印为 YAML(一种纯文本数据序列化格式)。The yaml format prints output as YAML, a plain-text data serialization format. YAML 往往比 JSON 更容易阅读,并且可以轻松映射到该格式。YAML tends to be easier to read than JSON, and easily maps to that format. 某些应用程序和 CLI 命令将 YAML(而不是 JSON)作为配置输入。Some applications and CLI commands take YAML as configuration input, instead of JSON.

az vm list --out yaml

以下输出有为简便起见而省略的一些字段并替换了标识信息。The following output has some fields omitted for brevity, and identifying information replaced.

- availabilitySet: null
  diagnosticsProfile: null
  hardwareProfile:
    vmSize: Standard_DS1_v2
  id: /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010
  identity: null
  instanceView: null
  licenseType: null
  location: westus
  name: ExampleVM1
  networkProfile:
    networkInterfaces:
    - id: /subscriptions/.../resourceGroups/DemoRG1/providers/Microsoft.Network/networkInterfaces/DemoVM010Nic
      primary: null
      resourceGroup: DemoRG1
  ...
...

表输出格式Table output format

table 格式以 ASCII 表的形式列显输出,因此可以轻松阅读和扫描输出。The table format prints output as an ASCII table, making it easy to read and scan. 嵌套对象不会包含在表输出中,但仍可以作为查询的一部分进行筛选。Nested objects aren't included in table output, but can still be filtered as part of a query. 某些字段不会包含在表中,因此,当你想要数据的快速、人工可搜索的概述时,此格式最佳。Some fields aren't included in the table, so this format is best when you want a quick, human-searchable overview of data.

az vm list --out table
Name         ResourceGroup    Location
-----------  ---------------  ----------
DemoVM010    DEMORG1          westus
demovm212    DEMORG1          westus
demovm213    DEMORG1          westus
KBDemo001VM  RGDEMO001        westus
KBDemo020    RGDEMO001        westus

可以使用 --query 参数来自定义要在列表输出中显示的属性和列。You can use the --query parameter to customize the properties and columns you want to show in the list output. 以下示例演示如何只在 list 命令中选择 VM 名称和资源组名称。The following example shows how to select just the VM Name and the Resource Group Name in the list command.

az vm list --query "[].{resource:resourceGroup, name:name}" -o table
Resource    Name
----------  -----------
DEMORG1     DemoVM010
DEMORG1     demovm212
DEMORG1     demovm213
RGDEMO001   KBDemo001VM
RGDEMO001   KBDemo020

备注

默认情况下,某些键不会在表视图中列显。Some keys are not printed in the table view by default. 这些键是 idtypeetagThese are id, type, and etag. 如果需要在输出中查看这些键,可以使用 JMESPath 重新键入功能更改键名称,并避免筛选。If you need to see these in your output, you can use the JMESPath re-keying feature to change the key name and avoid filtering.

az vm list --query "[].{objectID:id}" -o table

若要详细了解如何使用查询来筛选数据,请参阅在 Azure CLI 中使用 JMESPath 查询For more about using queries to filter data, see Use JMESPath queries with Azure CLI.

TSV 输出格式TSV output format

tsv 输出格式返回制表符和换行符分隔的值,而不带附加格式设置、键或其他符号。The tsv output format returns tab- and newline-separated values without additional formatting, keys, or other symbols. 采用这种格式可在需要以某种形式处理文本的其他命令和工具中轻松使用输出。This format makes it easy to consume the output into other commands and tools that need to process the text in some form. table 格式一样,tsv 不会列显嵌套的对象。Like the table format, tsv doesn't print nested objects.

在前面的示例中使用 tsv 选项会输出制表符分隔结果。Using the preceding example with the tsv option outputs the tab-separated result.

az vm list --out tsv
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010    None    None    westus    DemoVM010            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    cbd56d9b-9340-44bc-a722-25f15b578444
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm212    None    None    westus    demovm212            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    4bdac85d-c2f7-410f-9907-ca7921d930b4
None    None        /subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm213    None    None    westus    demovm213            None    Succeeded    DEMORG1    None            Microsoft.Compute/virtualMachines    2131c664-221a-4b7f-9653-f6d542fbfa34
None    None        /subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo001VM    None    None    westus    KBDemo001VM            None    Succeeded    RGDEMO001    None            Microsoft.Compute/virtualMachines    14e74761-c17e-4530-a7be-9e4ff06ea74b
None    None        /subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo020   None    None    westus    KBDemo020            None    Succeeded    RGDEMO001    None            Microsoft.Compute/virtualMachines    36baa9-9b80-48a8-b4a9-854c7a858ece

TSV 输出格式的一个限制是不能保证输出顺序。One restriction of the TSV output format is that there isn't a guarantee on output ordering. CLI 通过按字母顺序对响应 JSON 中的键进行排序,然后按 TSV 输出的顺序输出其值,来尽可能地保持顺序。The CLI makes a best effort to preserve ordering by sorting keys in the response JSON alphebetically, and then printing their values in order for TSV output. 但这并不能保证顺序始终相同,因为 Azure 服务响应格式可能会更改。This isn't a guarantee that the order is always identical though, since the Azure service response format may change.

若要强制执行一致的排序,需要使用 --query 参数和多选列表格式。In order to enforce consistent ordering, you'll need to use the --query parameter and the multiselect list format. 当 CLI 命令返回单个 JSON 字典时,请使用常规格式 [key1, key2, ..., keyN] 强制键顺序。When a CLI command returns a single JSON dictionary, use the general format [key1, key2, ..., keyN] to force a key order. 对于返回数组的 CLI 命令,请使用常规格式 [].[key1, key2, ..., keyN] 对列值进行排序。For CLI commands which return an array, use the general format [].[key1, key2, ..., keyN] to order column values.

例如,若要按 ID、位置、资源组和 VM 名称对显示的信息进行排序,请执行以下命令:For example, to order the information displayed above by ID, location, resource group, and VM name:

az vm list --out tsv --query '[].[id, location, resourceGroup, name]'
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/DemoVM010    westus    DEMORG1    DemoVM010
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm212    westus    DEMORG1    demovm212
/subscriptions/.../resourceGroups/DEMORG1/providers/Microsoft.Compute/virtualMachines/demovm213    westus    DEMORG1    demovm213
/subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo001VM     westus  RGDEMO001       KBDemo001VM
/subscriptions/.../resourceGroups/RGDEMO001/providers/Microsoft.Compute/virtualMachines/KBDemo020       westus  RGDEMO001       KBDemo020

以下示例演示如何通过管道将 tsv 输出传送到 bash 中的其他命令。The next example shows how tsv output can be piped to other commands in bash. 该查询用于筛选输出和强制排序,grep 选择其中包含文本“RGD”的项,然后 cut 命令选择第四个字段以在输出中显示 VM 的名称。The query is used to filter output and force ordering, grep selects items that have text "RGD" in them, then the cut command selects the fourth field to show the name of the VM in output.

az vm list --out tsv --query '[].[id, location, resourceGroup, name]' | grep RGD | cut -f4
KBDemo001VM
KBDemo020

设置默认输出格式Set the default output format

使用交互式 az configure 命令设置环境并建立输出格式的默认设置。Use the interactive az configure command to set up your environment and establish default settings for output formats. 默认输出格式为 jsonThe default output format is json.

az configure
Welcome to the Azure CLI! This command will guide you through logging in and setting some default values.

Your settings can be found at /home/defaultuser/.azure/config
Your current configuration is as follows:

  ...

Do you wish to change your settings? (y/N): y

What default output format would you like?
 [1] json - JSON formatted output that most closely matches API responses.
 [2] jsonc - Colored JSON formatted output that most closely matches API responses.
 [3] table - Human-readable output format.
 [4] tsv - Tab- and Newline-delimited. Great for GREP, AWK, etc.
 [5] yaml - YAML formatted output. An alternative to JSON. Great for configuration files.
 [6] none - No output, except for errors and warnings.
Please enter a choice [1]:

若要详细了解如何配置环境,请参阅 Azure CLI 配置To learn more about configuring your environment, see Azure CLI configuration.