From b154ad4cbb20f6b258f2afd3e0985101b7f0868b Mon Sep 17 00:00:00 2001 From: Hagai Kariti Date: Thu, 4 Jun 2015 11:41:02 +0300 Subject: [PATCH] ec2_vol: fix race conditions because we handle errors before actually deleting Just try to delete the volume and handle the error amazon sends --- lib/ansible/modules/cloud/amazon/ec2_vol.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_vol.py b/lib/ansible/modules/cloud/amazon/ec2_vol.py index 3065b550457..d5b4dd4f3bc 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_vol.py +++ b/lib/ansible/modules/cloud/amazon/ec2_vol.py @@ -239,15 +239,14 @@ def get_volumes(module, ec2): return vols def delete_volume(module, ec2): - vol = get_volume(module, ec2) - if not vol: - module.exit_json(changed=False) - else: - if vol.attachment_state() is not None: - adata = vol.attach_data - module.fail_json(msg="Volume %s is attached to an instance %s." % (vol.id, adata.instance_id)) - ec2.delete_volume(vol.id) - module.exit_json(changed=True) + volume_id = module.params['id'] + try: + ec2.delete_volume(volume_id) + module.exit_json(changed=True) + except boto.exception.EC2ResponseError as ec2_error: + if ec2_error.code == 'InvalidVolume.NotFound': + module.exit_json(changed=False) + module.fail_json(msg=ec2_error.message) def boto_supports_volume_encryption(): """