From 5b8d209b7e8076054f9963012653b6d723e2956c Mon Sep 17 00:00:00 2001 From: jjshoe Date: Tue, 12 Apr 2016 16:17:01 -0500 Subject: [PATCH] =?UTF-8?q?Speed=20up=20AMI=20code=20by=20not=20attempting?= =?UTF-8?q?=20to=20create=20the=20AMI=20without=20checkin=E2=80=A6=20(#255?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Speed up AMI code by not attempting to create the AMI without checking on the name first. Also simplifies code for reporting errors from AMI creation, greatly. * remove sys.exit --- lib/ansible/modules/cloud/amazon/ec2_ami.py | 26 ++++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_ami.py b/lib/ansible/modules/cloud/amazon/ec2_ami.py index 24f243f69b1..bdb5f98d83e 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_ami.py +++ b/lib/ansible/modules/cloud/amazon/ec2_ami.py @@ -228,17 +228,25 @@ def create_image(module, ec2): 'description': description, 'no_reboot': no_reboot} + images = ec2.get_all_images(filters={'name': name}) + + if images and images[0]: + module.exit_json(msg="AMI name already present", image_id=images[0].id, state=images[0].state, changed=False) + + if device_mapping: + bdm = BlockDeviceMapping() + for device in device_mapping: + if 'device_name' not in device: + module.fail_json(msg = 'Device name must be set for volume') + device_name = device['device_name'] + del device['device_name'] + bd = BlockDeviceType(**device) + bdm[device_name] = bd + params['block_device_mapping'] = bdm + image_id = ec2.create_image(**params) except boto.exception.BotoServerError, e: - if e.error_code == 'InvalidAMIName.Duplicate': - images = ec2.get_all_images() - for img in images: - if img.name == name: - module.exit_json(msg="AMI name already present", image_id=img.id, state=img.state, changed=False) - else: - module.fail_json(msg="Error in retrieving duplicate AMI details") - else: - module.fail_json(msg="%s: %s" % (e.error_code, e.error_message)) + module.fail_json(msg="%s: %s" % (e.error_code, e.error_message)) # Wait until the image is recognized. EC2 API has eventual consistency, # such that a successful CreateImage API call doesn't guarantee the success