使用 jq 通过 Lightsail 解析 AWS CLI 工具的 json 输出
Using jq to parse json output of AWS CLI tools with Lightsail
我正在尝试修改脚本以自动执行 lightsail 快照,但在修改 jq 查询时遇到问题。
我正在尝试解析 aws lightsail get-instance-snapshots
的输出
这是脚本的原始行:
aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | select(.[0].fromInstanceName == "WordPress-Test-Instance") | .[].name'
其中 return 是快照名称列表,每行一个。
我需要修改查询,这样就不会 return 所有快照,而只是名称以 'autosnap' 开头的快照。我这样做是因为脚本会旋转快照,但我不希望它删除我手动创建的快照(不会以 'autosnap' 开头)。
这是来自 aws lightsail get-instance-snapshots
的编辑示例输出
{
"instanceSnapshots": [
{
"location": {
"availabilityZone": "all",
"regionName": "*****"
},
"arn": "*****",
"fromBlueprintId": "wordpress_4_9_2_1",
"name": "autosnap-WordPress-Test-Instance-2018-04-16_01.46",
"fromInstanceName": "WordPress-Test-Instance",
"fromBundleId": "nano_1_2",
"supportCode": "*****",
"sizeInGb": 20,
"createdAt": 1523843190.117,
"fromAttachedDisks": [],
"fromInstanceArn": "*****",
"resourceType": "InstanceSnapshot",
"state": "available"
},
{
"location": {
"availabilityZone": "all",
"regionName": "*****"
},
"arn": "*****",
"fromBlueprintId": "wordpress_4_9_2_1",
"name": "Premanent-WordPress-Test-Instance-2018-04-16_01.40",
"fromInstanceName": "WordPress-Test-Instance",
"fromBundleId": "nano_1_2",
"supportCode": "*****",
"sizeInGb": 20,
"createdAt": 1523842851.69,
"fromAttachedDisks": [],
"fromInstanceArn": "*****",
"resourceType": "InstanceSnapshot",
"state": "available"
}
]
}
我本来以为这样的事情会奏效,但经过多次尝试后我没有任何运气......
aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | select(.[0].fromInstanceName == "WordPress-Test-Instance") | select(.[0].name | test("autosnap")) |.[].name'
如有任何帮助,我们将不胜感激!
进行您描述的选择的基本查询是:
.instanceSnapshots | map(select(.name|startswith("autosnap")))
(如果你不需要保留数组结构,你可以选择:
.instanceSnapshots[] | select(.name|startswith("autosnap"))
)
然后您可以通过扩展管道执行额外的过滤。
如果您要使用 test/1
,则适当的调用应该是 test("^autosnap")
或者 test("^autosnap-")
。
例子
.instanceSnapshots
| map(select(.name|startswith("autosnap")))
| map(select(.fromInstanceName == "WordPress-Test-Instance"))
| sort_by(.createdAt)
| .[].name
两个连续的select
当然可以压缩成一个。为了效率,排序应该越晚越好。
后记
虽然您确实可以使用 .[]
而不是 .instanceSnapshots
开始管道,但建议使用后者以防 JSON 架构发生变化。从某种意义上说,像 JSON 这样的数据格式的全部意义在于使编写相对于(理智的)schema-evolution.
健壮的查询变得容易
我正在尝试修改脚本以自动执行 lightsail 快照,但在修改 jq 查询时遇到问题。
我正在尝试解析 aws lightsail get-instance-snapshots
这是脚本的原始行:
aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | select(.[0].fromInstanceName == "WordPress-Test-Instance") | .[].name'
其中 return 是快照名称列表,每行一个。
我需要修改查询,这样就不会 return 所有快照,而只是名称以 'autosnap' 开头的快照。我这样做是因为脚本会旋转快照,但我不希望它删除我手动创建的快照(不会以 'autosnap' 开头)。
这是来自 aws lightsail get-instance-snapshots
{
"instanceSnapshots": [
{
"location": {
"availabilityZone": "all",
"regionName": "*****"
},
"arn": "*****",
"fromBlueprintId": "wordpress_4_9_2_1",
"name": "autosnap-WordPress-Test-Instance-2018-04-16_01.46",
"fromInstanceName": "WordPress-Test-Instance",
"fromBundleId": "nano_1_2",
"supportCode": "*****",
"sizeInGb": 20,
"createdAt": 1523843190.117,
"fromAttachedDisks": [],
"fromInstanceArn": "*****",
"resourceType": "InstanceSnapshot",
"state": "available"
},
{
"location": {
"availabilityZone": "all",
"regionName": "*****"
},
"arn": "*****",
"fromBlueprintId": "wordpress_4_9_2_1",
"name": "Premanent-WordPress-Test-Instance-2018-04-16_01.40",
"fromInstanceName": "WordPress-Test-Instance",
"fromBundleId": "nano_1_2",
"supportCode": "*****",
"sizeInGb": 20,
"createdAt": 1523842851.69,
"fromAttachedDisks": [],
"fromInstanceArn": "*****",
"resourceType": "InstanceSnapshot",
"state": "available"
}
]
}
我本来以为这样的事情会奏效,但经过多次尝试后我没有任何运气......
aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | select(.[0].fromInstanceName == "WordPress-Test-Instance") | select(.[0].name | test("autosnap")) |.[].name'
如有任何帮助,我们将不胜感激!
进行您描述的选择的基本查询是:
.instanceSnapshots | map(select(.name|startswith("autosnap")))
(如果你不需要保留数组结构,你可以选择:
.instanceSnapshots[] | select(.name|startswith("autosnap"))
)
然后您可以通过扩展管道执行额外的过滤。
如果您要使用 test/1
,则适当的调用应该是 test("^autosnap")
或者 test("^autosnap-")
。
例子
.instanceSnapshots
| map(select(.name|startswith("autosnap")))
| map(select(.fromInstanceName == "WordPress-Test-Instance"))
| sort_by(.createdAt)
| .[].name
两个连续的select
当然可以压缩成一个。为了效率,排序应该越晚越好。
后记
虽然您确实可以使用 .[]
而不是 .instanceSnapshots
开始管道,但建议使用后者以防 JSON 架构发生变化。从某种意义上说,像 JSON 这样的数据格式的全部意义在于使编写相对于(理智的)schema-evolution.