Merge pull request #2 from atlashealth/ec2_detach_volume

Ability to detach an EBS volume from an EC2 instance
This commit is contained in:
Brian Coca 2015-01-06 12:06:16 -05:00
commit 76e75b6024

View file

@ -168,14 +168,18 @@ EXAMPLES = '''
id: vol-XXXXXXXX id: vol-XXXXXXXX
state: absent state: absent
# Detach a volume
- ec2_vol:
id: vol-XXXXXXXX
instance: None
# List volumes for an instance # List volumes for an instance
- ec2_vol: - ec2_vol:
instance: i-XXXXXX instance: i-XXXXXX
state: list state: list
# Create new volume using SSD storage # Create new volume using SSD storage
- local_action: - ec2_vol:
module: ec2_vol
instance: XXXXXX instance: XXXXXX
volume_size: 50 volume_size: 50
volume_type: gp2 volume_type: gp2
@ -261,15 +265,18 @@ def create_volume(module, ec2, zone):
if iops: if iops:
volume_type = 'io1' volume_type = 'io1'
if instance == 'None' or instance == '':
instance = None
# If no instance supplied, try volume creation based on module parameters. # If no instance supplied, try volume creation based on module parameters.
if name or id: if name or id:
if not instance:
module.fail_json(msg = "If name or id is specified, instance must also be specified")
if iops or volume_size: if iops or volume_size:
module.fail_json(msg = "Parameters are not compatible: [id or name] and [iops or volume_size]") module.fail_json(msg = "Parameters are not compatible: [id or name] and [iops or volume_size]")
volume = get_volume(module, ec2) volume = get_volume(module, ec2)
if volume.attachment_state() is not None: if volume.attachment_state() is not None:
if instance is None:
return volume
adata = volume.attach_data adata = volume.attach_data
if adata.instance_id != instance: if adata.instance_id != instance:
module.fail_json(msg = "Volume %s is already attached to another instance: %s" module.fail_json(msg = "Volume %s is already attached to another instance: %s"
@ -331,6 +338,13 @@ def attach_volume(module, ec2, volume, instance):
except boto.exception.BotoServerError, e: except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message)) module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
def detach_volume(module, ec2):
vol = get_volume(module, ec2)
if not vol or vol.attachment_state() is None:
module.exit_json(changed=False)
else:
vol.detach()
module.exit_json(changed=True)
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
@ -362,6 +376,9 @@ def main():
snapshot = module.params.get('snapshot') snapshot = module.params.get('snapshot')
state = module.params.get('state') state = module.params.get('state')
if instance == 'None' or instance == '':
instance = None
ec2 = ec2_connect(module) ec2 = ec2_connect(module)
if state == 'list': if state == 'list':
@ -428,6 +445,8 @@ def main():
volume = create_volume(module, ec2, zone) volume = create_volume(module, ec2, zone)
if instance: if instance:
attach_volume(module, ec2, volume, inst) attach_volume(module, ec2, volume, inst)
else:
detach_volume(module, ec2)
module.exit_json(volume_id=volume.id, device=device_name, volume_type=volume.type) module.exit_json(volume_id=volume.id, device=device_name, volume_type=volume.type)
# import module snippets # import module snippets