我试图用AWS CLI构建一个JMESpath查询,该查询打印一个表,显示一些选定的属性作为行。我可以用jq
得到我想要的,但我只想用awscli
,这样它就可以格式化为表格。这可能吗?下面是我想要的输出,使用jq
对象构造语法:
% aws --output json ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[0]' | jq '.[0] | {InstanceType,PrivateIpAddress,LaunchTime}'
{
"InstanceType": "g4dn.4xlarge",
"PrivateIpAddress": "172.31.15.37",
"LaunchTime": "2021-02-17T14:49:30+00:00"
}
我最接近的方法是使用multiselect哈希,但这使每个项都成为一列,因此如果有多个项,它看起来不太好。
% aws --output table ec2 describe-instances --instance-id $id --query 'Reservations[].Instances[0].{size: InstanceType, PrivateIP: PrivateIpAddress, LaunchTime: LaunchTime}'
---------------------------------------------------------------
| DescribeInstances |
+---------------------------+----------------+----------------+
| LaunchTime | PrivateIP | size |
+---------------------------+----------------+----------------+
| 2021-02-17T14:49:30+00:00| 172.31.15.37 | g4dn.4xlarge |
+---------------------------+----------------+----------------+
table
输出将把不同的JSON对象视为不同的行。
如果您确实希望每行有一个属性,那么您可以使用JMESPath查询为每个属性创建一个对象,如下所示:
Reservations[].Instances[0].[ { Property: `LaunchTime`, Value: LaunchTime }, { Property: `Size`, Value: InstanceType }, { Property: `PrivateIP`, Value: PrivateIpAddress } ]
在JSON结构中:
{
"Reservations": [
{
"Instances": [
{
"InstanceType": "g4dn.4xlarge",
"PrivateIpAddress": "172.31.15.37",
"LaunchTime": "2021-02-17T14:49:30+00:00"
}
]
}
]
}
这将给你This JSON作为结果:
[
[
{
"Property": "LaunchTime",
"Value": "2021-02-17T14:49:30+00:00"
},
{
"Property": "Size",
"Value": "g4dn.4xlarge"
},
{
"Property": "PrivateIP",
"Value": "172.31.15.37"
}
]
]
表应该是这样的:
----------------------------------------------
| DescribeInstances |
+--------------+-----------------------------+
| Property | Value |
+--------------+-----------------------------+
| LaunchTime | 2021-02-17T14:49:30+00:00 |
+--------------+-----------------------------+
| Size | g4dn.4xlarge |
+--------------+-----------------------------+
| PrivateIP | 172.31.15.37 |
+--------------+-----------------------------+