使用AWS CLI查询嵌套路径并以表形式输出

时间:2018-08-20 17:27:07

标签: jq aws-cli

我试图在AWSCLI中使用--query命令输出表,但是由于某种原因,我无法在同一行中获取值。有什么错误吗?

.json文件是

asp-page-handler

}

我的查询是:

{
"Reservations": [
    {
        "Groups": [],
        "Instances": [
            {
                "AmiLaunchIndex": 0,
                "ImageId": "ami-2a7d75c0",
                "InstanceId": "i-099569e17a368446b",
                "InstanceType": "t2.micro",
                "KeyName": "ar_ecs_key",
                "LaunchTime": "2018-08-18T17:59:49.000Z",
                "Monitoring": {
                    "State": "disabled"
                },
                "Placement": {
                    "AvailabilityZone": "eu-west-1c",
                    "GroupName": "",
                    "Tenancy": "default"
                },
                "PrivateDnsName": "ip-172-31-46-99.eu-west-1.compute.internal",
                "PrivateIpAddress": "172.31.46.99",
                "ProductCodes": [],
                "PublicDnsName": "ec2-34-245-58-217.eu-west-1.compute.amazonaws.com",
                "PublicIpAddress": "34.245.58.217",
                "State": {
                    "Code": 16,
                    "Name": "running"
                },
                "StateTransitionReason": "",
                "SubnetId": "subnet-3407646f",
                "VpcId": "vpc-24ddec43",
                "Architecture": "x86_64",
                "BlockDeviceMappings": [
                    {
                        "DeviceName": "/dev/sda1",
                        "Ebs": {
                            "AttachTime": "2018-08-18T17:59:49.000Z",
                            "DeleteOnTermination": true,
                            "Status": "attached",
                            "VolumeId": "vol-0d9f4ab048e7999b7"
                        }
                    }
                ],
                "ClientToken": "",
                "EbsOptimized": false,
                "EnaSupport": true,
                "Hypervisor": "xen",
                "NetworkInterfaces": [
                    {
                        "Association": {
                            "IpOwnerId": "amazon",
                            "PublicDnsName": "ec2-34-245-58-217.eu-west-1.compute.amazonaws.com",
                            "PublicIp": "34.245.58.217"
                        },
                        "Attachment": {
                            "AttachTime": "2018-08-18T17:59:49.000Z",
                            "AttachmentId": "eni-attach-0bbfc61bf0a6729ad",
                            "DeleteOnTermination": true,
                            "DeviceIndex": 0,
                            "Status": "attached"
                        },
                        "Description": "",
                        "Groups": [
                            {
                                "GroupName": "default",
                                "GroupId": "sg-98ce7ee3"
                            }
                        ],
                        "Ipv6Addresses": [],
                        "MacAddress": "0a:df:58:fe:e5:d2",
                        "NetworkInterfaceId": "eni-02dcc4cbc45726b77",
                        "OwnerId": "300746241447",
                        "PrivateDnsName": "ip-172-31-46-99.eu-west-1.compute.internal",
                        "PrivateIpAddress": "172.31.46.99",
                        "PrivateIpAddresses": [
                            {
                                "Association": {
                                    "IpOwnerId": "amazon",
                                    "PublicDnsName": "ec2-34-245-58-217.eu-west-1.compute.amazonaws.com",
                                    "PublicIp": "34.245.58.217"
                                },
                                "Primary": true,
                                "PrivateDnsName": "ip-172-31-46-99.eu-west-1.compute.internal",
                                "PrivateIpAddress": "172.31.46.99"
                            }
                        ],
                        "SourceDestCheck": true,
                        "Status": "in-use",
                        "SubnetId": "subnet-3407646f",
                        "VpcId": "vpc-24ddec43"
                    }
                ],
                "RootDeviceName": "/dev/sda1",
                "RootDeviceType": "ebs",
                "SecurityGroups": [
                    {
                        "GroupName": "default",
                        "GroupId": "sg-98ce7ee3"
                    }
                ],
                "SourceDestCheck": true,
                "Tags": [
                    {
                        "Key": "webserver",
                        "Value": "production"
                    }
                ],
                "VirtualizationType": "hvm"
            }
        ],
        "OwnerId": "300746241447",
        "ReservationId": "r-047aab5e090b741af"
    }
]

结果是:

enter image description here

我确定它与展平运算符[]有关,但是我不知道是什么。已经尝试了所有版本...

提前

谢谢

A

1 个答案:

答案 0 :(得分:1)

嵌套BlockDeviceMappings中的所有值都在一个数组中。因此,您需要将其展平为各个值。您可以通过两种方式使用JMESPath实现此目的,您只需索引到第一个块设备即可。

Reservations[].Instances[].{
    InstanceId: InstanceId,
    type: InstanceType,
    Devicename: BlockDeviceMappings[0].DeviceName,
    Persistence_on: BlockDeviceMappings[0].Ebs.DeleteOnTermination,
    Status: BlockDeviceMappings[0].Ebs.Status,
    Volume_id: BlockDeviceMappings[0].Ebs.VolumeId
}

但是我认为出于可读性考虑,我会选择这种方法(假设aws-cli支持merge()):

Reservations[].Instances[].merge(
    {InstanceId: InstanceId, type: InstanceType},
    BlockDeviceMappings[0].{
        Devicename: DeviceName,
        Persistence_on: Ebs.DeleteOnTermination,
        Status: Ebs.Status,
        Volume_id: Ebs.VolumeId
    }
)

另一方面,我发现jq的使用更加容易。这应该产生与json相同的结果。

$ aws-cli ... | jq '[.Reservations[].Instances[] |
    { InstanceId, type: .InstanceType } +
    (.BlockDeviceMappings[] | {
        Devicename: .DeviceName,
        Persistence_on: .Ebs.DeleteOnTermination,
        Status: .Ebs.Status,
        Volume_id: .Ebs.VolumeId
    })
]'