AWS CLI命令/ JMESPATH查询问题

时间:2019-01-02 18:31:46

标签: amazon-web-services aws-cli jmespath

在下面的“什么不起作用?”部分中,您中的任何人都可以通过CLI命令/ JMESPATH查询来帮助我识别问题吗

P.S。下面给出的JSON输出是有效的,您可以使用输出在JMESPATH.org上测试其中的JMESPATH查询部分

什么有效?

1)aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[]
2)aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[].FromPort
3)aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[].IpProtocol

什么不起作用?

1)aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[?IpProtocol=='tcp'].IpProtocol
2)aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[?FromPort=='22'].FromPort

JSON输出

{
    "SecurityGroups": [
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-06d7c8d3300000000",
                            "UserId": "400000000000"
                        }
                    ]
                }
            ],
            "OwnerId": "400000000000",
            "GroupId": "sg-06d7c000000000000",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": []
                }
            ],
            "VpcId": "vpc-0d26c7ba200000000"
        },
        {
            "Description": "BastionSG",
            "GroupName": "BastionSG",
            "IpPermissions": [
                {
                    "FromPort": 22,
                    "IpProtocol": "tcp",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "ToPort": 22,
                    "UserIdGroupPairs": []
                }
            ],
            "OwnerId": "400000000000",
            "GroupId": "sg-0a26abc0a00000000",
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
 "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": []
                }
            ],
            "VpcId": "vpc-0d26c7ba200000000"
        }
    ]
}

预期/实际结果

aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[?IpProtocol=='tcp'].IpProtocol

结果

预期-tcp,实际-没有返回结果

aws ec2 describe-security-groups --query SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[?FromPort=='22'].FromPort

结果

预期-22,实际-没有结果

2 个答案:

答案 0 :(得分:1)

SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[] | [?IpProtocol=='tcp'].IpProtocol | [0]

SecurityGroups[?GroupId=='sg-0a26abc0a00000000'].IpPermissions[] | [?FromPort==`22`].FromPort | [0]

或者,引用/调整bash:

'SecurityGroups[?GroupId==`"sg-0a26abc0a00000000"`].IpPermissions[] | [?IpProtocol==`"tcp"`].IpProtocol | [0]'

'SecurityGroups[?GroupId==`"sg-0a26abc0a00000000`"].IpPermissions[] | [?FromPort==`22`].FromPort | [0]'

您会注意到IpPermissions末尾的[]使列表变平。如果您不执行此操作(或执行SecurityGroups[?GroupId=='sg-0a26abc0a00000000'][]),则该过滤器将应用于列表的顶层,其中IpPermissions不存在。

对于我来说,为什么这是真的还不清楚。这似乎是错误的,因为如果没有较早的过滤器,则以后的过滤器将应用于子列表。

答案 1 :(得分:1)

以下是我尝试过并获得预期数据的方法。让我知道这是否适合您。

aws ec2 describe-security-groups --query 'SecurityGroups[?GroupId==`sg-xxxxxx`].[IpPermissions[?IpProtocol==`tcp`] | [0].IpProtocol]' --output text
//tcp
aws ec2 describe-security-groups --query 'SecurityGroups[?GroupId==`sg-xxxxxx`].[IpPermissions[?FromPort==`22`] | [0].FromPort]' --output text
//22

注意-

  1. 最好将查询括在单引号中,以便CLI可以正确解析整个查询。
  2. 您可以使用反引号(`)填充条件。