From 87946920d8a57c5b6710178cb12ec2f624a34dc9 Mon Sep 17 00:00:00 2001 From: jjshoe Date: Tue, 19 Apr 2016 17:00:16 -0500 Subject: [PATCH] Fix an issue where by a call to ec2.get_image wasn't wrapped in a try/except, and would occasionally throw an exception in the wild. Also greatly simplifies two of the ugliest known loops to man down into a single loop. (#2554) --- lib/ansible/modules/cloud/amazon/ec2_ami.py | 24 ++++++++------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_ami.py b/lib/ansible/modules/cloud/amazon/ec2_ami.py index bdb5f98d83e..57770bf312d 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_ami.py +++ b/lib/ansible/modules/cloud/amazon/ec2_ami.py @@ -254,23 +254,17 @@ def create_image(module, ec2): for i in range(wait_timeout): try: img = ec2.get_image(image_id) - break + + if img.state == 'available': + break except boto.exception.EC2ResponseError, e: - if 'InvalidAMIID.NotFound' in e.error_code and wait: - time.sleep(1) - else: - module.fail_json(msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help.") - else: - module.fail_json(msg="timed out waiting for image to be recognized") + if ('InvalidAMIID.NotFound' not in e.error_code and 'InvalidAMIID.Unavailable' not in e.error_code) and wait and i == wait_timeout - 1: + module.fail_json(msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help. %s: %s" % (e.error_code, e.error_message)) + finally: + time.sleep(1) - # wait here until the image is created - wait_timeout = time.time() + wait_timeout - while wait and wait_timeout > time.time() and (img is None or img.state != 'available'): - img = ec2.get_image(image_id) - time.sleep(3) - if wait and wait_timeout <= time.time(): - # waiting took too long - module.fail_json(msg = "timed out waiting for image to be created") + if img.state != 'available': + module.fail_json(msg="Error while trying to find the new image. Using wait=yes and/or a longer wait_timeout may help.") if tags: try: