From 53e476ad4e90f74edcdaef4932bd05d4e2a7e06a Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Fri, 13 Oct 2017 14:11:07 -0400 Subject: [PATCH] [cloud] Only enforce state (running/stopped/etc) in EC2 "exact_count" when state is specified (#31648) fix issue in ec2 module where exact count would create new instances if the instane state is stopped or terminated --- lib/ansible/modules/cloud/amazon/ec2.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index cdc0a0c7ea..a09442f289 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -639,13 +639,18 @@ except ImportError: def find_running_instances_by_count_tag(module, ec2, vpc, count_tag, zone=None): - # get reservations for instances that match tag(s) and are running - reservations = get_reservations(module, ec2, vpc, tags=count_tag, state="running", zone=zone) + # get reservations for instances that match tag(s) and are in the desired state + state = module.params.get('state') + if state not in ['running', 'stopped']: + state = None + reservations = get_reservations(module, ec2, vpc, tags=count_tag, state=state, zone=zone) instances = [] for res in reservations: if hasattr(res, 'instances'): for inst in res.instances: + if inst.state == 'terminated': + continue instances.append(inst) return reservations, instances