From 633f86d30a3b2353982db5c899ee43d1b2d85424 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 17 Sep 2014 15:20:05 -0500 Subject: [PATCH] Allow filtering by availability zone for exact_count with ec2 Fixes #8863 --- cloud/ec2 | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cloud/ec2 b/cloud/ec2 index cb14d9b851f..a4776c74b83 100644 --- a/cloud/ec2 +++ b/cloud/ec2 @@ -483,10 +483,10 @@ except ImportError: print "failed=True msg='boto required for this module'" sys.exit(1) -def find_running_instances_by_count_tag(module, ec2, count_tag): +def find_running_instances_by_count_tag(module, ec2, count_tag, zone=None): # get reservations for instances that match tag(s) and are running - reservations = get_reservations(module, ec2, tags=count_tag, state="running") + reservations = get_reservations(module, ec2, tags=count_tag, state="running", zone=zone) instances = [] for res in reservations: @@ -507,7 +507,7 @@ def _set_none_to_blank(dictionary): return result -def get_reservations(module, ec2, tags=None, state=None): +def get_reservations(module, ec2, tags=None, state=None, zone=None): # TODO: filters do not work with tags that have underscores filters = dict() @@ -542,6 +542,9 @@ def get_reservations(module, ec2, tags=None, state=None): # http://stackoverflow.com/questions/437511/what-are-the-valid-instancestates-for-the-amazon-ec2-api filters.update({'instance-state-name': state}) + if zone: + filters.update({'availability-zone': zone}) + results = ec2.get_all_instances(filters=filters) return results @@ -655,8 +658,9 @@ def enforce_count(module, ec2): exact_count = module.params.get('exact_count') count_tag = module.params.get('count_tag') + zone = module.params.get('zone') - reservations, instances = find_running_instances_by_count_tag(module, ec2, count_tag) + reservations, instances = find_running_instances_by_count_tag(module, ec2, count_tag, zone) changed = None checkmode = False