很多时候,我们都需要通过cli命令来获取volume信息,snapshots信息,但是,我们知道这些volume的name都是存在tags中的,例如:
1 |
aws ec2 describe-volumes |
我们得到的结果是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
{ "Volumes": [ { "AvailabilityZone": "us-east-1a", "Attachments": [ { "AttachTime": "2013-12-18T22:35:00.000Z", "InstanceId": "i-1234567890abcdef0", "VolumeId": "vol-049df61146c4d7901", "State": "attached", "DeleteOnTermination": true, "Device": "/dev/sda1" } ], "VolumeType": "standard", "VolumeId": "vol-049df61146c4d7901", "State": "in-use", "SnapshotId": "snap-1234567890abcdef0", "CreateTime": "2013-12-18T22:35:00.084Z", "Size": 8 }, { "AvailabilityZone": "us-east-1a", "Attachments": [], "VolumeType": "io1", "VolumeId": "vol-1234567890abcdef0", "State": "available", "Iops": 1000, "SnapshotId": null, "CreateTime": "2014-02-27T00:02:41.791Z", "Size": 100 } ] } |
这个时候我们需要的信息可能需要过滤一下,只显示我们需要的信息,这个时候我们就需要query
1 |
aws ec2 describe-volumes --filters Name=tag-key,Values="Name" Name=tag-value,Values="Test*" --query 'Volumes[*].{ID:VolumeId,Tag:Tags}' |
我们得到的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[ { "Tag": [ { "Value": "Test2", "Key": "Name" } ], "ID": "vol-1234567890abcdef0" }, { "Tag": [ { "Value": "Test1", "Key": "Name" } ], "ID": "vol-049df61146c4d7901" } ] |
这个时候问题来了,我们时候需要显示volume的name,很多时候我们有很多的tag,并不是一个,我们需要如何调用呢?下面的例子:
aws ec2 describe-snapshots –filters Name=tag:Name,Values=*test* –region us-east-1 –query ‘Snapshots[*].{ID:SnapshotId,Time:StartTime,Name:Tags[?Key==Name
].Value[]}’
1 |
调用的是describe-snapshots , 所有的name中包含test,然后只显示SnapshotID,Time,Name |
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021