JMESPath 查询结合搜索与 ?并在 azure cloudshell bash cli 中包含关键字

问题描述

我正在尝试编写一个 azure cli JMESPath 查询输出包含单词 db 并且属于 osType 窗口的所有名称
为此,我编写了以下查询,该查询通过调用名为 grep 的外部 bash 实用程序来工作。
但是我无法通过内置函数 contains 的 JMESPath 语言过滤来完成它。
这是一个有效的查询

az vm list --query "[?storageProfile.osdisk.osType=='Windows'].[name]" -o tsv | grep db

这是我尝试过但未能获得结果的查询

az vm list --query "[?storageProfile.osdisk.osType=='Windows'].[?contains(name,'db')]" -o tsv

解决方法

您只需要使用 and expression

给定查询:

[?storageProfile.osDisk.osType=='Windows' && contains(name,'db')].name

关于 JSON

[
  {
    "name": "db-of-windows-application","storageProfile": {
      "osDisk": {
        "osType": "Windows"
      }
    }
  },{
    "name": "db-of-linux-application","storageProfile": {
      "osDisk": {
        "osType": "Linux"
      }
    }
  },{
    "name": "I-am-not-the-database-you-are-looking-for","storageProfile": {
      "osDisk": {
        "osType": "Windows"
      }
    }
  }
]

这将给出:

[
  "db-of-windows-application"
]