可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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"?
回答1:
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)'
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)'
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.
http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html
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.
回答2:
You can do that with jmespath (the engine that drives the --query
parameter) despite what others say:
aws ec2 describe-instances \
--query 'Reservations[].Instances[?!not_null(Tags[?Key == `Name`].Value)] | []'
Source: Using Amazon Web Services Command Line Interface (AWS CLI) to Find Instances without a 'Name' Tag.
回答3:
Since --filters
parameter doesn't seem to support inverse filtering, here's my solution to this problem using --query
parameter:
aws ec2 describe-instances \
--query 'Reservations[].Instances[?!contains(Tags[].Key, `Name`)][].InstanceId'
It looks at an array of tag keys for each instance and filters those instance that don't have Tag 'Name' in the array. Then flattens output to array of instance IDs.
- Advantage over some previous answers: no need for
jq
or other command to filter output.
- Disadvantage over true inverse filter: likely to be much slower over large number of instances.
回答4:
Unfortunately the underlying api call DescribeSnapshots does not support inverse tag filtering, and so neither does the CLI. You can, however, do client side filtering with the --query
parameter which performs a JMESPath search. This will prevent you from having to use pipes as with user2616321's answer.
For example:
aws ec2 describe-instances --query "Reservations[].Instances[?Tags[?Key == 'Name']][]"
Add .InstanceId
to the end of that to just get the instance ids.
回答5:
I was having the same problem and I figured out how to query on Tag-Values
you will most likely have the same tag-key defined for all the instances; I have defined a tag-key "MachineName" on all my instances and I want to filter by the the values of the Tag-key Name
Below is the example to filter where the Name=Machine1
use the option
--filters "Name=tag-key,Values=MachineName" "Name=tag-values,Values=Machine1"
This works fine for me
回答6:
AFAIK directly through the CLI you won't be able to do that.
By the syntax you are using, I can guess you are using the old cli. I suggest you to download the new CLI http://aws.amazon.com/cli/ and call
aws ec2 describe-instances --output json
from python, ruby or any scripting language you may like to parse the json output filtering using the proper regular expression according to your needs
回答7:
I too was totally shocked by how difficult this is to do via the CLI. I liked user2616321's answer, but I was having a little trouble making it output the exact fields I wanted per instance. After spending a while messing around and failing with JMESPath in the query syntax, I ended up just making a little ruby script to do this. In case anyone wants to save a few minutes writing one of their own, here it is:
#!/usr/bin/env ruby
require 'json'
# We'll output any instance that doesn't contain all of these tags
desired_tags = if ARGV.empty?
%w(Name)
else
ARGV
end
# Put the keys we want to output per instance/reservation here
reservation_keys = %w(OwnerId RequesterId)
instance_keys = %w(Tags InstanceId InstanceType PublicDnsName LaunchTime PrivateIpAddress KeyName)
instances_without_tags = []
# Just use CLI here to avoid AWS dependencies
reservations = JSON.parse(
`aws ec2 describe-instances`
)["Reservations"]
# A reservation is a single call to spin up instances. You could potentially
# have more than one instance in a reservation, but often only one is
# spun up at a time, meaning there is a single instance per reservation.
reservations.each do |reservation|
reservation["Instances"].each do |instance|
# Filter instances without the desired tags
tag_keys = instance["Tags"].map { |t| t["Key"] }
unless (tag_keys & desired_tags).length == desired_tags.length
instances_without_tags <<
reservation.select { |k| reservation_keys.include?(k) }.
merge(instance.select { |k| instance_keys.include?(k) })
end
end
end
puts JSON.pretty_generate(instances_without_tags)
回答8:
You could always do this:
ec2-describe-instances | grep -v "Name"
:p