你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure CLI 命令的输出格式

Azure CLI 使用 JSON 作为默认输出格式,但提供其他格式。 --output 使用 (--out-o) 参数设置 Azure CLI 输出的格式。 输出的参数值和类型为:

--输出 DESCRIPTION
json JSON 字符串。 此设置为默认设置
jsonc 彩色 JSON
table 使用键作为列标题的 ASCII 表
tsv 制表符分隔值,没有键
yaml YAML,一种人工可读的 JSON 替代格式
yamlc 彩色 YAML
none 除了错误和警告以外不输出任何内容

警告

为了避免公开 API 密钥和凭据等机密,请使用变量中的 none 输出格式或存储命令输出。 注意: 某些 CI/CD 环境可能会将执行的命令的输出存储在日志中。 最好确认这些日志文件的内容以及谁有权访问日志。 有关详细信息,请参阅 None 输出格式

JSON 输出格式(默认格式)

以下示例以默认 JSON 格式显示订阅中的虚拟机列表。

az vm list --output json

以下输出有为简便起见而省略的一些字段并替换了标识信息。

[
  {
    "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 格式将输出打印为 YAML(一种纯文本数据序列化格式)。 YAML 往往比 JSON 更容易读取,并映射到该格式。 某些应用程序和 Azure CLI 命令采用 YAML 作为配置输入,而不是 JSON。

az vm list --output yaml

以下输出有为简便起见而省略的一些字段并替换了标识信息。

- 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 格式以 ASCII 表的形式列显输出,因此可以轻松阅读和扫描输出。 嵌套对象不包括在表输出中,但仍可以作为查询的一部分进行筛选。 某些字段不会包含在表中,因此,当你想要数据的快速、人工可搜索的概述时,此格式最佳。

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

可以使用 --query 参数来自定义要在列表输出中显示的属性和列。 以下示例演示如何只在 list 命令中选择 VM 名称和资源组名称。

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

注释

默认情况下,某些键不会打印在表视图中。 这些键包括idtypeetag。 如果在输出中需要它们,可以使用 JMESPath 重新生成密钥功能更改密钥名称并避免筛选。

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

若要详细了解如何使用查询来筛选数据,请参阅在 Azure CLI 中使用 JMESPath 查询

TSV 输出格式

输出 tsv 格式返回制表符和换行符分隔值,无需额外的格式、键或其他符号。 采用这种格式可在需要以某种形式处理文本的其他命令和工具中轻松使用输出。 与 table 格式一样,tsv 不会列显嵌套的对象。

在前面的示例中使用 tsv 选项会输出制表符分隔结果。

az vm list --output 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 输出格式的一个限制是无法保证输出的顺序。 Azure CLI 尽力通过按字母顺序对响应 JSON 中的键进行排序来保留顺序,然后打印其值以便 tsv 输出。 不能保证订单始终相同,因为 Azure 服务响应格式可能会更改。

若要强制实施一致的排序,需要使用 --query 参数和 多选列表 格式。 当 Azure CLI 命令返回单个 JSON 字典时,请使用常规格式 [key1, key2, ..., keyN] 强制键顺序。 对于返回数组的 Azure CLI 命令,请使用常规格式 [].[key1, key2, ..., keyN] 对列值进行排序。

例如,若要按 ID、位置、资源组和 VM 名称显示此信息进行排序:

az vm list --output 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 中的其他命令。 该查询用于筛选输出并强制排序, grep 选择包含文本 RGD 的项,然后 cut 命令选择第四个字段以显示输出中 VM 的名称。

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

将值分配给变量时,通常会使用 tsv 输出格式。 此示例获取活动订阅 ID,并将其存储到变量中以用于脚本。

# Bash Script
subscriptionID=$(az account show --query id --output tsv)
echo "Using subscription ID $subscriptionID"

有关更多 --query 参数示例,请参阅如何查询 Azure CLI 命令输出

None 输出格式

某些 Azure CLI 命令会输出你必须保护的信息。 下面是 4 个示例:

  • 密码
  • 连接字符串
  • 机密
  • 密钥

若要在使用 Azure CLI 命令时保护机密和密钥,请选择以下选项之一:

选项 益处 用例
--output none 输出格式 防止敏感信息显示在控制台中。 如果命令失败,仍会收到错误消息。 1.当命令输出可供在以后检索时使用。
2.对输出没有需求时使用。
3.托管标识或服务主体用于管理 Azure 资源时的常见选择。
--query 参数 将输出存储在变量中。 1.当命令输出不可供在以后检索时使用。
2.需要在脚本中使用命令输出值时使用。

稍后检索安全信息

某些 Azure 机密可供在以后检索。 例如,存储在 Azure Key Vault 中的机密。 下面的示例将 az keyvault secret set--output none 选项配合使用来创建一个 Azure Key Vault 机密。 可在以后使用 az keyvault secret show 命令检索该机密。

az keyvault secret set --name MySecretName \
                       --vault-name MyKeyVaultName \
                       --value MySecretValue\
                       --output none

将安全信息存储到变量

从技术上来讲,使用 --query 将输出存储到变量中不是一种输出格式。 这是保护机密的解决方案,也是使用 --output none替代方法。 例如,重置了服务主体凭据时,将无法再次检索到该密码。

重置服务主体凭据,并以默认的 json 格式返回输出:

# reset service principal credentials using default output format (json).
az ad sp credential reset --id myServicePrincipalID --output json

控制台输出,其中显示了控制台中的新密码。

{
  "appId": "myServicePrincipalID",
  "password": "myServicePrincipalNewPassword",
  "tenant": "myTenantID"
}

更好的解决方案是将敏感信息返回到变量。

# Bash Script
# reset service principal credentials returning results to a variable
myNewPassword=$(az ad sp credential reset --id myServicePrincipalID --query password --output tsv)

# Display the new password (remove this line in production for security)
echo "New password: $myNewPassword"

有关将输出存储到变量的更多示例,请参阅成功使用 Azure CLI - 将值传递给另一个命令。 若要详细了解 --query 参数语法,请参阅如何查询 Azure CLI 命令输出

设置默认输出格式

Azure CLI 命令提供可通过两种方式控制的输出:

输出控制 益处 操作说明
全局设置 选择使用最多的默认输出值,这样就不必为每个参考命令持续提供 --output 参数。 使用 az config set 指定默认输出格式。
命令参数 在命令级别指定输出,并为脚本提供最大的灵活性。 可以控制每个参考命令的控制台输出和变量输入。 使用参考命令的 --output 参数替代默认设置。

Azure CLI 的默认输出是 json。 当不需要控制台输出录时,将默认输出设置为 none

az config set core.output=none

可以使用--output参数重写任何 Azure CLI 引用命令的默认输出。 下面是更改和测试命令输出的命令脚本:

# set your default output to table
az config set core.output=table

# show your active subscription in table format
# notice how only a subset of properties are returned in the table
az account show

# override your table default and show your active subscription in jsonc format
az account show --output jsonc

# reset your default output to json
az config set core.output=json

另请参阅