14

我正在尝试理解 aws ec2 cli 调用。我希望在自定义标签上描述所有 VPC 然后文件管理器(vpcname=myvpc,但是在尝试了多种组合之后,我不断收到关于格式和使用 --filters 的冲突错误。使用作为参考 [ http://docs.aws .amazon.com/cli/latest/reference/ec2/describe-vpcs.html][1]

aws --profile myProfile --region eu-west-1 ec2 describe-vpcs --filters vpcname,myvpc

但是这会返回

Error parsing parameter '--filters': should be: Key value pairs, where values are separated by commas, and multiple pairs are separated by spaces.
--filters Name=string1,Values=string1,string2 Name=string1,Values=string1,string2

如此努力

aws --profile myProfile --region eu-west-1 ec2 describe-vpcs --filters Name=vpcname,Values=myvpc

它返回

A client error (InvalidParameterValue) occurred when calling the DescribeVpcs operation: The filter 'vpcname' is invalid

所以尝试其他一些组合

aws --profile myProfile --region eu-west-1 ec2 describe-vpcs --filters tag :Name=vpcname,Values=myvpc

Error parsing parameter '--filters': should be: Key value pairs, where values are separated by commas, and multiple pairs are separated by spaces.
--filters Name=string1,Values=string1,string2 Name=string1,Values=string1,string2

关于如何格式化此请求的任何建议?

4

3 回答 3

29

你已经非常接近解决它了——唯一的问题是你没有为 describe-vpcs 指定一个有效的过滤器。这是与您的用例相关的过滤器:

tag:key=*value* - The key/value combination of a tag assigned to the resource.

因此,当它要求时Name=string1,Values=string1...,它期望:

  • 名称=标签:标签名称
  • 值=标签值

试试这个,用不同的自定义标签在本地为我工作:

aws ec2 describe-vpcs --filters Name=tag:vpcname,Values=myvpc
于 2014-11-21T11:20:13.530 回答
0

如果您尝试使用 Ansible AWS *_facts 调用来做同样的事情,那么您会遇到同样的问题。在 Ansible 中,正确的语法是:

ec2_vpc_net_facts: filters: "tag:vpcname": "myvpc"

我在这里只提到这一点是因为在我尝试正确使用 Ansible 时,我的大多数谷歌搜索中都会出现这个 SO 问题,并且我一直在 Ansible 中使用上面的 AWS cli 示例,因为我无法正确使用过滤器。

于 2018-08-21T12:33:42.957 回答
-1

在 TAG 变量中定义标签,在 VALUE 变量中定义值:

TAG=vpcname
VALUE=myvpc
aws ec2 describe-vpcs |\
jq -r ".Vpcs[] | select (.Tags[] | select(.Key==\"$TAG\") |\
select(.Value==\"$VALUE\"))"
于 2016-02-04T15:06:48.360 回答