AWS cli JMESpath 查询标签值
AWS cli JMESpath query tag values
作为我的 的后续行动,我无法让标签在此类输出中正常工作。我想打印一个 table 每个 属性 作为一行。这就是我期望的样子:
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Type', Value: InstanceType }]"
-------------------------------
| DescribeInstances |
+-----------+-----------------+
| Property | Value |
+-----------+-----------------+
| Type | g4dn.12xlarge |
+-----------+-----------------+
但是标签名称看起来像这样:
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Name', Value: Tags[?Key =='Name'].Value }]"
-------------------
|DescribeInstances|
+-----------------+
| Property |
+-----------------+
| Name |
+-----------------+
|| Value ||
|+---------------+|
|| Elliott-TKD ||
|+---------------+|
标签值正确,但格式很奇怪,当与更多其他行组合时,table 变得非常难看。
您查询的过滤器部分 ([?Key == 'Name']
) 正在创建 JMESPath 所描述的 projection。
您必须重置此投影才能从中提取单个字符串。
可以使用 pipes.
来重置投影
Projections are an important concept in JMESPath. However, there are times when projection semantics are not what you want. A common scenario is when you want to operate of the result of a projection rather than projecting an expression onto each element in the array.
For example, the expression people[*].first
will give you an array containing the first names of everyone in the people array. What if you wanted the first element in that list? If you tried people[*].first[0]
that you just evaluate first[0]
for each element in the people array, and because indexing is not defined for strings, the final result would be an empty array, []
. To accomplish the desired result, you can use a pipe expression, <expression> | <expression>
, to indicate that a projection must stop.
来源:https://jmespath.org/tutorial.html#pipe-expressions
所以你的问题与他们在文档中描述的非常接近,并且可以使用以下方法重置该投影:
Tags[?Key =='Name']|[0].Value
或者,与:
Tags[?Key =='Name'].Value | [0]
这是两个完全相同的查询。
鉴于 JSON:
{
"Reservations": [
{
"Instances": [
{
"Tags": [
{
"Key": "Name",
"Value": "Elliott-TKD"
},
{
"Key": "Foo",
"Value": "Bar"
}
]
}
]
}
]
}
查询
Reservations[].Instances[0].[{ Property: `Name`, Value: Tags[?Key == `Name`]|[0].Value }]
会给出预期
[
[
{
"Property": "Name",
"Value": "Elliott-TKD"
}
]
]
因此它将在您的 table
中正确呈现
------------------------------
| DescribeInstance |
+------------+---------------+
| Property | Value |
+------------+---------------+
| Name | Elliott-TKD |
+------------+---------------+
作为我的
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Type', Value: InstanceType }]"
-------------------------------
| DescribeInstances |
+-----------+-----------------+
| Property | Value |
+-----------+-----------------+
| Type | g4dn.12xlarge |
+-----------+-----------------+
但是标签名称看起来像这样:
% aws --output table ec2 describe-instances --instance-id $id --query "Reservations[].Instances[0].[{ Property: 'Name', Value: Tags[?Key =='Name'].Value }]"
-------------------
|DescribeInstances|
+-----------------+
| Property |
+-----------------+
| Name |
+-----------------+
|| Value ||
|+---------------+|
|| Elliott-TKD ||
|+---------------+|
标签值正确,但格式很奇怪,当与更多其他行组合时,table 变得非常难看。
您查询的过滤器部分 ([?Key == 'Name']
) 正在创建 JMESPath 所描述的 projection。
您必须重置此投影才能从中提取单个字符串。
可以使用 pipes.
Projections are an important concept in JMESPath. However, there are times when projection semantics are not what you want. A common scenario is when you want to operate of the result of a projection rather than projecting an expression onto each element in the array. For example, the expression
people[*].first
will give you an array containing the first names of everyone in the people array. What if you wanted the first element in that list? If you triedpeople[*].first[0]
that you just evaluatefirst[0]
for each element in the people array, and because indexing is not defined for strings, the final result would be an empty array,[]
. To accomplish the desired result, you can use a pipe expression,<expression> | <expression>
, to indicate that a projection must stop.
来源:https://jmespath.org/tutorial.html#pipe-expressions
所以你的问题与他们在文档中描述的非常接近,并且可以使用以下方法重置该投影:
Tags[?Key =='Name']|[0].Value
或者,与:
Tags[?Key =='Name'].Value | [0]
这是两个完全相同的查询。
鉴于 JSON:
{
"Reservations": [
{
"Instances": [
{
"Tags": [
{
"Key": "Name",
"Value": "Elliott-TKD"
},
{
"Key": "Foo",
"Value": "Bar"
}
]
}
]
}
]
}
查询
Reservations[].Instances[0].[{ Property: `Name`, Value: Tags[?Key == `Name`]|[0].Value }]
会给出预期
[
[
{
"Property": "Name",
"Value": "Elliott-TKD"
}
]
]
因此它将在您的 table
中正确呈现------------------------------
| DescribeInstance |
+------------+---------------+
| Property | Value |
+------------+---------------+
| Name | Elliott-TKD |
+------------+---------------+