mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Creating ami should be idempotent
If the ami already exists, return details of the duplicate ami instead of failing with an error.
This commit is contained in:
parent
dd6d5f0cb5
commit
e6e8f65e92
1 changed files with 10 additions and 1 deletions
|
@ -165,7 +165,16 @@ def create_image(module, ec2):
|
|||
|
||||
image_id = ec2.create_image(**params)
|
||||
except boto.exception.BotoServerError, e:
|
||||
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
||||
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)
|
||||
sys.exit(0)
|
||||
else:
|
||||
module.fail_json(msg="Error in retrieving duplicate AMI details")
|
||||
else:
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue