AWS CLI 返回嵌套结果

AWS CLI 返回嵌套结果

我正在尝试对 EC2 执行 AWS CLI 查询。我已编写了以下查询,但三重嵌套似乎对 InstanceStatus.Details.Status 造成了影响。

aws ec2 describe-instance-status --query 'InstanceStatuses[*].{InstanceId:InstanceId,AvailabilityZone:AvailabilityZone,InstanceState:InstanceState.Name,InstanceStatusDetails:InstanceStatus.Details.Status,Status:SystemStatus.Status}' --profile $PROFILE --output table

|                                    DescribeInstanceStatus                                     |
+------------------+----------------------+----------------+-------------------------+----------+
| AvailabilityZone |     InstanceId       | InstanceState  |  InstanceStatusDetails  | Status   |
+------------------+----------------------+----------------+-------------------------+----------+
|  us-east-1a      |  i-asdfasdfasfdsdfsa |  running       |  None                   |  ok      |

如果我去掉 .Status,它将返回我不想要的整个表。

aws ec2 describe-instance-status --query 'InstanceStatuses[*].{InstanceId:InstanceId,AvailabilityZone:AvailabilityZone,InstanceState:InstanceState.Name,InstanceStatusDetails:InstanceStatus.Details,Status:SystemStatus.Status}' --profile $PROFILE --output table

|                        DescribeInstanceStatus                        |
+-------------------+-----------------------+----------------+---------+
| AvailabilityZone  |      InstanceId       | InstanceState  | Status  |
+-------------------+-----------------------+----------------+---------+
|  us-east-1a       |  i-asdfasdfasfdsdfsa  |  running       |  ok     |
+-------------------+-----------------------+----------------+---------+
||                        InstanceStatusDetails                       ||
|+-----------------------------------------+--------------------------+|
||                  Name                   |         Status           ||
|+-----------------------------------------+--------------------------+|
||  reachability                           |  passed                  ||
|+-----------------------------------------+--------------------------+|

我需要做什么才能返回 InstanceStatus.Details.Status 的结果以及其余查询?

谢谢!

答案1

字典结构作为提醒:

{
    "AvailabilityZone": "eu-west-1b",
    "InstanceId": "i-00aabb11cc22dd33",
    "InstanceState": {
        "Code": 16,
        "Name": "running"
    },
    "InstanceStatus": {
        "Details": [
            {
                "Name": "reachability",
                "Status": "passed"
            }
        ],
        "Status": "ok"
    },
    "SystemStatus": {
        "Details": [
            {
                "Name": "reachability",
                "Status": "passed"
            }
        ],
        "Status": "ok"
    }
},

实例状态.详细信息是一个数组。你必须知道你想从数组的哪个元素中获取“状态”键

因此,添加[0]实例状态.详细信息

因此:

aws ec2 describe-instance-status --query 'InstanceStatuses[*].{InstanceId:InstanceId,AvailabilityZone:AvailabilityZone,InstanceState:InstanceState.Name,InstanceStatusDetails:InstanceStatus.Details[0].Status,Status:SystemStatus.Status}' --profile $PROFILE --output table

相关内容