jq 解析 aws ec2 实例标签
jq to parse aws ec2 instance tags
我需要转换这个输出
$ aws ec2 describe-instances --instance-ids i-xxxxxxxx | jq -r '.Reservations[].Instances[].Tags[]' | jq -s 'from_entries | del(..|.["aws:autoscaling:groupName"]?)'
{
"Key": "ssh_user",
"Value": "abc"
}
{
"Key": "ssh_port",
"Value": "2200"
}
{
"Key": "aws:autoscaling:groupName",
"Value": "ASG-Api"
}
{
"Key": "Name",
"Value": "SV-V3-API"
}
使用 jq 进入这个:
{
"ssh_user":"abc",
"ssh_port":"2200",
"Name":"SV-V3-API"
}
请注意,我需要删除此键:aws:autoscaling:groupName
使用 jq 1.5:
$ jq -cs 'from_entries | del(.["aws:autoscaling:groupName"])'
使用 jq 1.3 或 1.4:
$ jq -M -c -s 'reduce .[] as $x
([]; . + [{"key" : $x.Key, "value": $x.Value}])
| from_entries
| del(.["aws:autoscaling:groupName"])'
在 Windows,您需要修改引号,或将 jq 命令放入文件中。
我认为@peak 关于 sed
的评论很重要,足以得到一个单独的答案。
jq
1.4 版及更低版本无法使用 from_entries
原生使用亚马逊的 key/value 对,因为亚马逊使用大写键,而 jq
需要小写键。 jq
团队 addressed this 使用版本 1.5。
要将您的 EC2 实例的标签作为 key: value
对而不是 {key, value}
条目,请使用:
aws ... | \
sed -e 's/"Key":/"key":/g' -e 's/"Value":/"value":/g' | \
jq '.Reservations[]|.Instances[]|.Tags|from_entries'
我需要转换这个输出
$ aws ec2 describe-instances --instance-ids i-xxxxxxxx | jq -r '.Reservations[].Instances[].Tags[]' | jq -s 'from_entries | del(..|.["aws:autoscaling:groupName"]?)'
{
"Key": "ssh_user",
"Value": "abc"
}
{
"Key": "ssh_port",
"Value": "2200"
}
{
"Key": "aws:autoscaling:groupName",
"Value": "ASG-Api"
}
{
"Key": "Name",
"Value": "SV-V3-API"
}
使用 jq 进入这个:
{
"ssh_user":"abc",
"ssh_port":"2200",
"Name":"SV-V3-API"
}
请注意,我需要删除此键:aws:autoscaling:groupName
使用 jq 1.5:
$ jq -cs 'from_entries | del(.["aws:autoscaling:groupName"])'
使用 jq 1.3 或 1.4:
$ jq -M -c -s 'reduce .[] as $x
([]; . + [{"key" : $x.Key, "value": $x.Value}])
| from_entries
| del(.["aws:autoscaling:groupName"])'
在 Windows,您需要修改引号,或将 jq 命令放入文件中。
我认为@peak 关于 sed
的评论很重要,足以得到一个单独的答案。
jq
1.4 版及更低版本无法使用 from_entries
原生使用亚马逊的 key/value 对,因为亚马逊使用大写键,而 jq
需要小写键。 jq
团队 addressed this 使用版本 1.5。
要将您的 EC2 实例的标签作为 key: value
对而不是 {key, value}
条目,请使用:
aws ... | \
sed -e 's/"Key":/"key":/g' -e 's/"Value":/"value":/g' | \
jq '.Reservations[]|.Instances[]|.Tags|from_entries'