按标签名称排序EC2实例



所以除了jq工具之外,我还使用aws cli来检索实例列表。我用下面的命令来做。

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

使用以下示例json响应。

{
    "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
                }
            ]
        }
    ]
}

但是对于返回的实例,我希望它们按照它们的Tag Name排序。我试过以下方法。

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

但是jq报错了几个错误消息。是否有一个简单的方法来排序json名称标签,然后得到PrivateDNS条目在排序的顺序?

好吧,我不确定我是否明白你想要什么。这就是我认为你所要求的;如果我说错了,请纠正我:

在每个Reservation中,我希望里面的Instances按照Key"Name"TagValue排序

下面是解决这个问题的方法:

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

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

我希望ReservationsTagValue排序,KeyInstances中的"Name"

可以这样求解:

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

(如果一个Reservation中有几个Instances,这个脚本的行为可能是没有意义的)

要获得私有DNS值,只需选择那些具有以下内容的值:

.Reservations[].Instances[].PrivateDnsName

您也可以在排序脚本之后使用管道。

最新更新