diff options
author | jchristi <jeremy.d.christian@gmail.com> | 2017-09-14 16:58:16 +0200 |
---|---|---|
committer | ansibot <ansibot@users.noreply.github.com> | 2017-09-14 16:58:16 +0200 |
commit | 9d5671db76011279f69def41a8db2466e6b238df (patch) | |
tree | eedb8306fa71d0ef182b252c5abdcdc508d64bd8 | |
parent | Fix Dell OS network module timeout (#30355) (diff) | |
download | ansible-9d5671db76011279f69def41a8db2466e6b238df.tar.xz ansible-9d5671db76011279f69def41a8db2466e6b238df.zip |
Allow filtering RDS instances by tags in the ec2 dynamic inventory script (#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
-rwxr-xr-x | contrib/inventory/ec2.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 8cb099a9d3..f8bccac2f6 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -623,6 +623,36 @@ class Ec2Inventory(object): 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 ''' @@ -648,8 +678,8 @@ class Ec2Inventory(object): 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: |