使用JMESPath和AWS EC2描述实例以输出多个标签值 [英] Using JMESPath and aws ec2 describe instances to output multiple tag values
问题描述
我正在尝试从ec2实例描述中输出多个标签.我想要的标记值为Name和aws:autoscaling:groupName.
I'm trying to output multiple tags from an ec2 instances description. The tag values that I want are Name and aws:autoscaling:groupName.
"Tags": [
{
"Value": "somename",
"Key": "Name"
},
{
"Value": "some-asg-name",
"Key": "aws:autoscaling:groupName"
},
{
"Value": "somethingelse",
"Key": "project"
}
],
这是我到目前为止的内容:
Here's what I have so far:
aws ec2 describe-instances --instance-ids i-12345678 --query 'Reservations[].Instances[].[Tags[? contains(`["aws:autoscaling:groupName","Name"]`, Key)] | [0].Value,[1].Value,InstanceId]' --output text
这将导致:
somename None i-12345678
代替:
somename some-asg-name i-12345678
我尝试了双管道||
和contains
,但无法获得所需的输出.另外,我不确定[1].Value
是获取第二个匹配标签的正确方法.
I tried both double pipe ||
and contains
but can't get the output I need. Also, I'm not sure [1].Value
is the right way to get the 2nd matching tag.
推荐答案
如果您将过滤标记拆分为多个部分,然后将输出选择为单独的部分,则可能更容易想到.
This might be easier to think about if you split filtering your tags and selecting your output into separate pieces.
首先,选择所有实例:
Reservations[].Instances[]
然后使用管道过滤仅包含两个所需标签的实例:
Then pipe to filter for only instances with both of your desired tags:
| [? Tags[? Key == 'Name']] | [? Tags[? Key == 'aws:autoscaling:groupName']]
然后选择InstanceId和标记值:
Then select the InstanceId and tag values:
.[InstanceId,Tags[? Key == 'Name' || Key == 'aws:autoscaling:groupName'].Value]
aws ec2 describe-instances --query "Reservations[].Instances[] | [? Tags[? Key == 'Name']] | [? Tags[? Key == 'aws:autoscaling:groupName']].[InstanceId,Tags[? Key == 'Name' || Key == 'aws:autoscaling:groupName'].Value]"
示例输出
i-aaaa1234
myNameValue myASGvalue
i-bbbb1234
myNameValue myASGvalue
进一步阅读
- AWS文档-从AWS命令行控制命令输出界面
- AWS Documentation - Controlling Command Output from the AWS Command Line Interface
Further Reading
这篇关于使用JMESPath和AWS EC2描述实例以输出多个标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!