AWS CLI 命令参数学习 如何使用 --query 选项筛选输出
AWS CLI 使用 --query 选项提供内置输出筛选功能。为演示其工作方式,我们首先来看看下面的默认 JSON 输出,它描述了连接到不同 EC2 实例的两个 EBS (Elastic Block Storage) 卷。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
$ aws ec2 describe-volumes { "Volumes": [ { "AvailabilityZone": "us-west-2a", "Attachments": [ { "AttachTime": "2013-09-17T00:55:03.000Z", "InstanceId": "i-a071c394", "VolumeId": "vol-e11a5288", "State": "attached", "DeleteOnTermination": true, "Device": "/dev/sda1" } ], "VolumeType": "standard", "VolumeId": "vol-e11a5288", "State": "in-use", "SnapshotId": "snap-f23ec1c8", "CreateTime": "2013-09-17T00:55:03.000Z", "Size": 30 }, { "AvailabilityZone": "us-west-2a", "Attachments": [ { "AttachTime": "2013-09-18T20:26:16.000Z", "InstanceId": "i-4b41a37c", "VolumeId": "vol-2e410a47", "State": "attached", "DeleteOnTermination": true, "Device": "/dev/sda1" } ], "VolumeType": "standard", "VolumeId": "vol-2e410a47", "State": "in-use", "SnapshotId": "snap-708e8348", "CreateTime": "2013-09-18T20:26:15.000Z", "Size": 8 } ] }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
首先,我们可以使用以下命令仅显示 Volumes 列表中的第一个卷。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
$ aws ec2 describe-volumes --query 'Volumes[0]' { "AvailabilityZone": "us-west-2a", "Attachments": [ { "AttachTime": "2013-09-17T00:55:03.000Z", "InstanceId": "i-a071c394", "VolumeId": "vol-e11a5288", "State": "attached", "DeleteOnTermination": true, "Device": "/dev/sda1" } ], "VolumeType": "standard", "VolumeId": "vol-e11a5288", "State": "in-use", "SnapshotId": "snap-f23ec1c8", "CreateTime": "2013-09-17T00:55:03.000Z", "Size": 30 }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
现在,我们使用通配符表示法 [*] 循环访问整个列表,并筛选出三个元 素:VolumeId、AvailabilityZone 和 Size。请注意,词典表示法要求您为每个键提供一个别名,如: {Alias1:Key1,Alias2:Key2}。词典本身是无序的,因此,此种结构中的键别名的顺序在某些情况下可能不一 致。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
$ aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,AZ:AvailabilityZone,Size:Size}' [ { "AZ": "us-west-2a", "ID": "vol-e11a5288", "Size": 30 }, { "AZ": "us-west-2a", "ID": "vol-2e410a47", "Size": 8 } ]
![复制代码](https://common.cnblogs.com/images/copycode.gif)
在使用词典表示法时,您也可以使用串联的键(如 key1.key2[0].key3)来筛选深度嵌套在结构中的元 素。下面的示例通过 Attachments[0].InstanceId 键(其别名为简单的 InstanceId)对此进行演 示。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
$ aws ec2 describe-volumes --query 'Volumes[*]. {ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}' [ { "InstanceId": "i-a071c394", "AZ": "us-west-2a", "ID": "vol-e11a5288", "Size": 30 }, { "InstanceId": "i-4b41a37c", "AZ": "us-west-2a", "ID": "vol-2e410a47", "Size": 8 } ]
![复制代码](https://common.cnblogs.com/images/copycode.gif)
您也可以使用列表表示法筛选多个元素:[key1, key2]。这样做会对每个对象将筛选出的所有属性格式化 为一个排序 列表,而不管类型如何。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
$ aws ec2 describe-volumes --query 'Volumes[*].[VolumeId, Attachments[0].InstanceId, AvailabilityZone, Size]' [ [ "vol-e11a5288", "i-a071c394", "us-west-2a", 30 ], [ "vol-2e410a47", "i-4b41a37c", "us-west-2a", 8 ] ]
![复制代码](https://common.cnblogs.com/images/copycode.gif)
要按特定字段的值筛选结果,请使用 JMESPath "?" 运算符。以下示例查询仅输出 us-west-2a 可用区中的 卷:
$ aws ec2 describe-volumes --query 'Volumes[?AvailabilityZone==`us-west-2a`]'
eg:
aws events list-targets-by-rule --rule per_5m_2 --output text --query Targets[].Id | sed 's/\s\+/\n/g'
来源:oschina
链接:https://my.oschina.net/u/4072296/blog/4774771