我正在尝试使用AWS构建一个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"
}我最近看到的是使用多选择散列,但这使每个项成为一个列,因此,如果有几个项以上,看起来就不太好了。
% 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 |
+---------------------------+----------------+----------------+发布于 2021-02-17 21:52:38
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"
}
]
}
]
}因此,这将为您提供此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 |
+--------------+-----------------------------+https://stackoverflow.com/questions/66244870
复制相似问题