Skip to content

Commit

Permalink
Allow filtering RDS instances by tags in the ec2 dynamic inventory sc…
Browse files Browse the repository at this point in the history
…ript (ansible#24423)

* Allow filtering RDS instances by tags in the ec2.py dynamic inventory script

* PEP8 fix

* Fix no-bastring code smell

* Simplify logic in ec2.py RDS filtering by tag
  • Loading branch information
jchristi authored and ansibot committed Sep 14, 2017
1 parent ac69fcc commit 9d5671d
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions contrib/inventory/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,36 @@ def get_instances_by_region(self, region):
error = "Error connecting to %s backend.\n%s" % (backend, e.message)
self.fail_with_error(error, 'getting EC2 instances')

def tags_match_filters(self, tags):
''' return True if given tags match configured filters '''
if not self.ec2_instance_filters:
return True
match = self.stack_filters
for filter_name, filter_value in self.ec2_instance_filters.items():
if filter_name[:4] != 'tag:':
continue
filter_name = filter_name[4:]
if filter_name not in tags:
if self.stack_filters:
match = False
break
continue
if isinstance(filter_value, list):
if self.stack_filters and tags[filter_name] not in filter_value:
match = False
break
if not self.stack_filters and tags[filter_name] in filter_value:
match = True
break
if isinstance(filter_value, six.string_types):
if self.stack_filters and tags[filter_name] != filter_value:
match = False
break
if not self.stack_filters and tags[filter_name] == filter_value:
match = True
break
return match

def get_rds_instances_by_region(self, region):
''' Makes an AWS API call to the list of RDS instances in a particular
region '''
Expand All @@ -648,8 +678,8 @@ def get_rds_instances_by_region(self, region):
instance.tags = {}
for tag in tags:
instance.tags[tag['Key']] = tag['Value']

self.add_rds_instance(instance, region)
if self.tags_match_filters(instance.tags):
self.add_rds_instance(instance, region)
if not marker:
break
except boto.exception.BotoServerError as e:
Expand Down

0 comments on commit 9d5671d

Please sign in to comment.