查找所有亚马逊AWS实例不具备一定的标签 [英] Finding all Amazon AWS Instances That Do Not Have a Certain Tag

查看:167
本文介绍了查找所有亚马逊AWS实例不具备一定的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Amazon AWS命令行工具,以找到不具有指定标签的所有实例。

I'm trying to use the Amazon AWS Command Line Tools to find all instances that do not have a specified tag.

查找带标签的所有实例是很简单的,如:

Finding all instances WITH a tag is simple enough, e.g.

ec2-describe-instances --filter "tag-key=Name"

但我怎么才能反转的过滤器,只返回有没有标签名称?

But how would I invert that filter to return only the instances that have no tag "Name"?

推荐答案

这会做什么你问 - 寻找不包含名为YOUR_KEY_NAME_HERE(第2行过滤器实例没有命名的标记标签的每一个实例名称):

This will do what you're asking - find every instance which doesn't contain a tag named "YOUR_KEY_NAME_HERE" (2nd line filters for instances without tags named "Name"):

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "YOUR_KEY_NAME_HERE"} ]}) | not)' 
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Name"} ]}) | not)' 

如果你想对标签的值筛选,而不是变量的名称,该查询列出不包含名为YOUR_KEY_NAME_HERE其值为EXCLUDE_ME标签的所有实例。 (第2行列出了未命名testbox1实例。)

If you wanted to filter against the value of the tag, instead of the name of the tag, this query lists all instances which don't contain a tag named YOUR_KEY_NAME_HERE whose value is EXCLUDE_ME. (2nd line lists instances which aren't named "testbox1".)

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "YOUR_KEY_NAME_HERE"}, {Value: "EXCLUDE_ME"}]}) | not)'
aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Name"}, {Value: "testbox1"}]}) | not)'

菲利普是正确的。解析输出去,因为AWS API不提供这项功能,也没有正式的CLI AWS任的唯一途径。 JSON输出非常解析的,尤其是在比较多行文字记录,以旧的CLI打印默认情况下。

Felipe is correct. Parsing the output is the only way to go, since the AWS API does not provide this feature, nor do either of the official AWS CLIs. JSON output is very parseable, especially when compared to the multi-line text records which the old CLI prints by default.

<一个href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html">http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html

API本身返回JSON和新awscli打印件JSON作为其缺省输出格式。而JQ计划是分析它非常有用,并且当发送到终端甚至会上色,也可以--output文本回缩短为字符串。

The API itself returns JSON, and the new awscli prints that JSON as its default output format. The "jq" program is very useful to parse it, and will even colorize when sent to a terminal, or you can --output text to reduce it back to strings.

这篇关于查找所有亚马逊AWS实例不具备一定的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆