From 2fac64795593f894f1f0a9fafcea83e994c1e7ba Mon Sep 17 00:00:00 2001 From: Will Thames Date: Sat, 27 Jan 2018 07:12:21 +1000 Subject: [PATCH] [cloud] ec2_asg should wait for ASG to delete (#35327) `wait_for_instances: no` means do not wait for instances to become InService/Healthy before terminating them. It does not mean don't wait for the ASG to delete. Not waiting for the ASG to delete can cause problems when recreating it. Ensure that waiting for the ASG to delete respects `wait_timeout`. --- lib/ansible/modules/cloud/amazon/ec2_asg.py | 40 +++++++++++---------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py index a580c12682f..517f9bc83b8 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_asg.py +++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py @@ -1089,29 +1089,31 @@ def delete_autoscaling_group(connection): del_notification_config(connection, group_name, notification_topic) groups = describe_autoscaling_groups(connection, group_name) if groups: + wait_timeout = time.time() + wait_timeout if not wait_for_instances: delete_asg(connection, group_name, force_delete=True) - return True - - wait_timeout = time.time() + wait_timeout - updated_params = dict(AutoScalingGroupName=group_name, MinSize=0, MaxSize=0, DesiredCapacity=0) - update_asg(connection, **updated_params) - instances = True - while instances and wait_for_instances and wait_timeout >= time.time(): - tmp_groups = describe_autoscaling_groups(connection, group_name) - if tmp_groups: - tmp_group = tmp_groups[0] - if not tmp_group.get('Instances'): - instances = False - time.sleep(10) - + else: + updated_params = dict(AutoScalingGroupName=group_name, MinSize=0, MaxSize=0, DesiredCapacity=0) + update_asg(connection, **updated_params) + instances = True + while instances and wait_for_instances and wait_timeout >= time.time(): + tmp_groups = describe_autoscaling_groups(connection, group_name) + if tmp_groups: + tmp_group = tmp_groups[0] + if not tmp_group.get('Instances'): + instances = False + time.sleep(10) + + if wait_timeout <= time.time(): + # waiting took too long + module.fail_json(msg="Waited too long for old instances to terminate. %s" % time.asctime()) + + delete_asg(connection, group_name, force_delete=False) + while describe_autoscaling_groups(connection, group_name) and wait_timeout >= time.time(): + time.sleep(5) if wait_timeout <= time.time(): # waiting took too long - module.fail_json(msg="Waited too long for old instances to terminate. %s" % time.asctime()) - - delete_asg(connection, group_name, force_delete=False) - while describe_autoscaling_groups(connection, group_name): - time.sleep(5) + module.fail_json(msg="Waited too long for ASG to delete. %s" % time.asctime()) return True return False