首页 > 解决方案 > 带有json的天蓝色jmespath查询列表

问题描述

我从 Azure nsg 查询中得到了类似的东西

    [
      {
        "access": "Deny",
        ...
        ...
        "sourceAddressPrefixes": [
          "x.x.x.x",
          "y.y.y.y"
        ],
        "sourceApplicationSecurityGroups": null,
        ...
        ..
      },
    ]

我想sourceAddressPrefixes从该列表中查询x.x.x.xy.y.y.y以及其他属性。

这些是我尝试过的:

我在https://jmespath.org/tutorial.html中尝试了这个,它可以在 just中正常工作[?direction=='Inbound'].[access,sourceAddressPrefixes],但不能在命令行中正常工作,即使在查询周围有引号也是如此。

我在 Ubuntu 18.4
python-jmespath 版本 0.9.3上运行它

标签: azureazure-clijmespath

解决方案


您对 Azure CLI 命令产生了误解。我看到您想要过滤 NSG 的入站规则并查看一些属性。如果要查看入站规则的所有属性:

az network nsg rule list -g groupName--nsg-name nsgName --query "[?direction=='Inbound']"

如果要查看入站规则的一些属性:

az network nsg rule list -g groupName--nsg-name nsgName --query "[?direction=='Inbound'].[name,destinationAddressPrefix,sourceAddressPrefix,sourceAddressPrefixes,priority]"

但为此,我建议您使用另一种格式:

az network nsg rule list -g charles --nsg-name azurevmNSG --query "[?direction=='Inbound'].{name:name,destinationAddressPrefix:destinationAddressPrefix,sourceAddressPrefix:sourceAddressPrefix,sourceAddressPrefixes:sourceAddressPrefixes,priority:priority}"

这种方式将向您显示属性的名称和值。如果添加参数,可能会更方便阅读-o table


推荐阅读