按标签名称对EC2实例进行排序 [英] Sort EC2 Instances by Tag Name

查看:54
本文介绍了按标签名称对EC2实例进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,除了jq工具之外,我还使用aws cli检索实例列表.我使用以下命令执行此操作.

So I'm using the aws cli to retrieve a list of instances in addition to the jq tool. I do so with the following command.

aws ec2 describe-instances | jq '.Reservations[].Instances[]'

带有以下示例json响应.

With the following example json response.

{
    "Reservations": [
        {
            "OwnerId": "1234", 
            "ReservationId": "r-124991k", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2015-07-31T16:48:01.000Z", 
                    "PrivateIpAddress": "11.123.104.123", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-sdfsdf9109", 
                    "StateTransitionReason": "", 
                    "InstanceId": "i-1223421", 
                    "ImageId": "ami-aklasd131", 
                    "PrivateDnsName": "private.dns.name", 
                    "KeyName": "testkey", 
                    "SecurityGroups": [
                        {
                            "GroupName": "secur-grou", 
                            "GroupId": "sg-a3d234a"
                        }
                    ], 
                    "ClientToken": "", 
                    "SubnetId": "subnet-57absfsdf", 
                    "InstanceType": "m3.large", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "12:c3:f2:5c:5f:4d", 
                            "SourceDestCheck": false, 
                            "VpcId": "vpc-sdfsdf9109", 
                            "Description": "", 
                            "NetworkInterfaceId": "eni-81b1234", 
                            "PrivateIpAddresses": [
                                {
                                    "Primary": true, 
                                    "PrivateIpAddress": "11.123.104.123"
                                }
                            ], 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-5asdf234", 
                                "AttachTime": "2015-07-30T16:10:39.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "secur-grou", 
                                    "GroupId": "sg-a3d234a"
                                }
                            ], 
                            "SubnetId": "subnet-57absfsdf", 
                            "OwnerId": "1234", 
                            "PrivateIpAddress": "11.123.104.123"
                        }
                    ], 
                    "SourceDestCheck": false, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": "", 
                        "AvailabilityZone": "us-east-1c"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/sda1", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-28c882", 
                                "AttachTime": "2015-07-30T16:10:43.000Z"
                            }
                        }, 
                        {
                            "DeviceName": "/dev/sdb", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-3f1d5", 
                                "AttachTime": "2015-07-30T16:10:43.000Z"
                            }
                        }, 
                        {
                            "DeviceName": "/dev/sdg", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-81c8b", 
                                "AttachTime": "2015-07-30T16:10:43.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/sda1", 
                    "VirtualizationType": "hvm", 
                    "Tags": [
                        {
                            "Value": "apple", 
                            "Key": "Name"
                        }, 
                        {
                            "Value": "tag", 
                            "Key": "extra"
                        }
                    ], 
                    "AmiLaunchIndex": 0
                }
            ]
        }, 
        {
            "OwnerId": "1234", 
            "ReservationId": "r-2a72342", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2015-07-31T16:48:01.000Z", 
                    "PrivateIpAddress": "11.123.104.83", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-sdfsdf9109", 
                    "StateTransitionReason": "", 
                    "InstanceId": "i-f9271451", 
                    "ImageId": "ami-aklasd131", 
                    "PrivateDnsName": "private.name.here", 
                    "KeyName": "testkey", 
                    "SecurityGroups": [
                        {
                            "GroupName": "secur-grou", 
                            "GroupId": "sg-a3d234a"
                        }
                    ], 
                    "ClientToken": "", 
                    "SubnetId": "subnet-5llqjwdf", 
                    "InstanceType": "m3.large", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "12:ea:06:8b:ff:86", 
                            "SourceDestCheck": false, 
                            "VpcId": "vpc-sdfsdf9109", 
                            "Description": "", 
                            "NetworkInterfaceId": "eni-82kjs91", 
                            "PrivateIpAddresses": [
                                {
                                    "Primary": true, 
                                    "PrivateIpAddress": "11.123.104.13"
                                }
                            ], 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-35312341", 
                                "AttachTime": "2015-07-30T17:29:00.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "secur-grou", 
                                    "GroupId": "sg-a3d234a"
                                }
                            ], 
                            "SubnetId": "subnet-57absfsdf", 
                            "OwnerId": "1234", 
                            "PrivateIpAddress": "11.123.104.83"
                        }
                    ], 
                    "SourceDestCheck": false, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": "", 
                        "AvailabilityZone": "us-east-1c"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/sda1", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-ee1c21", 
                                "AttachTime": "2015-07-30T17:29:02.000Z"
                            }
                        }, 
                        {
                            "DeviceName": "/dev/sdb", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-13awer", 
                                "AttachTime": "2015-07-30T17:29:02.000Z"
                            }
                        }, 
                        {
                            "DeviceName": "/dev/sdg", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-3111", 
                                "AttachTime": "2015-07-30T17:29:02.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/sda1", 
                    "VirtualizationType": "hvm", 
                    "Tags": [
                        {
                            "Value": "banana", 
                            "Key": "Name"
                        }, 
                        {
                            "Value": "tag", 
                            "Key": "extra"
                        }
                    ], 
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}

但是对于返回的实例,我希望它们按其标签名称进行排序.我尝试了以下方法.

But for the instances that are returned I would like them to be sorted by their Tag Name. I've tried the following.

jq '.Reservations[]|=sort_by(.Instances[].Tags[.Key="Name"].Value)'

但是jq抱怨了几条错误消息.有没有一种简单的方法可以按Name标签对json进行排序,然后按该排序顺序获取PrivateDNS条目?

But jq complains with several error messages. Is there an easy way to sort the json by Name tag and then get the PrivateDNS entry in that sorted order?

推荐答案

好的,我不确定我是否了解您想要的内容.我想这就是您要的;如果我错了,请纠正我:

Okay, I'm not sure if I understood what you want. This is what I think you are asking for; please correct me if I'm wrong:

在每个Reservation中,我希望内部的InstancesKey"Name"

In each Reservation, I want the Instances inside to be sorted by the Value of the Tag whose Key is "Name"

这就是解决该问题的方法:

And this is what would solve that problem:

jq '.Reservations[].Instances |= sort_by(.Tags[] | select(.Key == "Name").Value)'

或者,您可能会要求这样做:

Alternatively, you may be asking for this:

我希望ReservationsInstances"Name"TagValue排序.

这将通过以下方式解决:

Which would be solved by this:

jq '.Reservations |= sort_by(.Instances[].Tags[] | select(.Key == "Name").Value)'

(如果Reservation中有多个Instances,则此脚本的行为可能毫无意义)

(The behavior of this script is probably meaningless if there are several Instances in a Reservation)

从获取私有DNS值开始,只需选择带有以下内容的值即可:

As of getting the private DNS values out, just select those with something like:

.Reservations[].Instances[].PrivateDnsName

您也可以在排序脚本之后通过管道进行传输.

You can also pipe that after the sorting script.

这篇关于按标签名称对EC2实例进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆