1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Allow state=absent for ec2_vol

Add methods to the module to make it more readable and allow reuse
This commit is contained in:
willthames 2014-03-21 11:04:30 +10:00 committed by Will Thames
parent 14499e8bf3
commit c0ac615a07

View file

@ -55,7 +55,7 @@ options:
version_added: "1.6" version_added: "1.6"
id: id:
description: description:
- volume id if you wish to attach an existing volume (requires instance) - volume id if you wish to attach an existing volume (requires instance) or remove an existing volume
required: false required: false
default: null default: null
aliases: [] aliases: []
@ -63,7 +63,7 @@ options:
volume_size: volume_size:
description: description:
- size of volume (in GB) to create. - size of volume (in GB) to create.
required: true required: false
default: null default: null
aliases: [] aliases: []
iops: iops:
@ -118,7 +118,13 @@ options:
default: null default: null
aliases: [] aliases: []
version_added: "1.6" version_added: "1.6"
state:
description:
- whether to ensure the volume is present or absent
required: false
default: present
choices: ['absent', 'present']
version_added: "1.6"
requirements: [ "boto" ] requirements: [ "boto" ]
author: Lester Wade author: Lester Wade
''' '''
@ -173,6 +179,7 @@ EXAMPLES = '''
wait: yes wait: yes
count: 1 count: 1
register: ec2 register: ec2
- local_action: - local_action:
module: ec2_vol module: ec2_vol
instance: "{{ item.id }}" instance: "{{ item.id }}"
@ -180,6 +187,12 @@ EXAMPLES = '''
device_name: /dev/xvdf device_name: /dev/xvdf
with_items: ec2.instances with_items: ec2.instances
register: ec2_vol register: ec2_vol
# Remove a volume
- location: action
module: ec2_vol
id: vol-XXXXXXXX
state: absent
''' '''
# Note: this module needs to be made idempotent. Possible solution is to use resource tags with the volumes. # Note: this module needs to be made idempotent. Possible solution is to use resource tags with the volumes.
@ -196,57 +209,50 @@ except ImportError:
print "failed=True msg='boto required for this module'" print "failed=True msg='boto required for this module'"
sys.exit(1) sys.exit(1)
def main(): def get_volume(module, ec2):
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
instance = dict(),
id = dict(),
name = dict(),
volume_size = dict(),
iops = dict(),
device_name = dict(),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
snapshot = dict(),
)
)
module = AnsibleModule(argument_spec=argument_spec)
id = module.params.get('id')
name = module.params.get('name') name = module.params.get('name')
instance = module.params.get('instance') id = module.params.get('id')
volume_size = module.params.get('volume_size')
iops = module.params.get('iops')
device_name = module.params.get('device_name')
zone = module.params.get('zone') zone = module.params.get('zone')
filters = {}
volume_ids = None
if zone:
filters['availability_zone'] = zone
if name:
filters = {'tag:Name': name}
if id:
volume_ids = [id]
try:
vols = ec2.get_all_volumes(volume_ids=volume_ids, filters=filters)
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
if not vols:
module.fail_json(msg="Could not find volume in zone (if specified): %s" % name or id)
if len(vols) > 1:
module.fail_json(msg="Found more than one volume in zone (if specified) with name: %s" % name)
return vols[0]
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)
def create_volume(module, ec2, zone):
name = module.params.get('name')
id = module.params.get('id')
instance = module.params.get('instance')
iops = module.params.get('iops')
volume_size = module.params.get('volume_size')
snapshot = module.params.get('snapshot') snapshot = module.params.get('snapshot')
ec2 = ec2_connect(module)
if id and name:
module.fail_json(msg="Both id and name cannot be specified")
if not (id or name or volume_size):
module.fail_json(msg="Cannot specify volume_size and either one of name or id")
# Here we need to get the zone info for the instance. This covers situation where
# instance is specified but zone isn't.
# Useful for playbooks chaining instance launch with volume create + attach and where the
# zone doesn't matter to the user.
if instance:
reservation = ec2.get_all_instances(instance_ids=instance)
inst = reservation[0].instances[0]
zone = inst.placement
# Check if there is a volume already mounted there.
if device_name:
if device_name in inst.block_device_mapping:
module.exit_json(msg="Volume mapping for %s already exists on instance %s" % (device_name, instance),
volume_id=inst.block_device_mapping[device_name].volume_id,
device=device_name,
changed=False)
# If custom iops is defined we use volume_type "io1" rather than the default of "standard" # If custom iops is defined we use volume_type "io1" rather than the default of "standard"
if iops: if iops:
volume_type = 'io1' volume_type = 'io1'
else: else:
@ -259,26 +265,7 @@ def main():
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]")
filters = {} volume = get_volume(module, ec2)
volume_ids = None
if zone:
filters['availability_zone'] = zone
if name:
filters = {'tag:Name': name}
if id:
volume_ids = [id]
try:
vols = ec2.get_all_volumes(volume_ids=volume_ids, filters=filters)
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
if not vols:
module.fail_json(msg = "Could not find volume in zone (if specified): %s" % name or id)
if len(vols) > 1:
module.fail_json(msg =
"Found more than one volume in zone (if specified) with name: %s" % name)
volume = vols.pop()
if volume.attachment_state() is not None: if volume.attachment_state() is not None:
adata = volume.attach_data adata = volume.attach_data
if adata.instance_id != instance: if adata.instance_id != instance:
@ -298,12 +285,15 @@ def main():
volume.update() volume.update()
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))
return volume
# Attach the created volume.
def attach_volume(module, ec2, volume, instance):
device_name = module.params.get('device_name')
if device_name and instance: if device_name and instance:
try: try:
attach = volume.attach(inst.id, device_name) attach = volume.attach(instance.id, device_name)
while volume.attachment_state() != 'attached': while volume.attachment_state() != 'attached':
time.sleep(3) time.sleep(3)
volume.update() volume.update()
@ -334,11 +324,65 @@ def main():
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))
print json.dumps({
"volume_id": volume.id, def main():
"device": device_name argument_spec = ec2_argument_spec()
}) argument_spec.update(dict(
sys.exit(0) instance = dict(),
id = dict(),
name = dict(),
volume_size = dict(),
iops = dict(),
device_name = dict(),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
snapshot = dict(),
state = dict(choices=['absent', 'present'], default='present')
)
)
module = AnsibleModule(argument_spec=argument_spec)
id = module.params.get('id')
name = module.params.get('name')
instance = module.params.get('instance')
volume_size = module.params.get('volume_size')
iops = module.params.get('iops')
device_name = module.params.get('device_name')
zone = module.params.get('zone')
snapshot = module.params.get('snapshot')
state = module.params.get('state')
ec2 = ec2_connect(module)
if id and name:
module.fail_json(msg="Both id and name cannot be specified")
if not (id or name or volume_size):
module.fail_json(msg="Cannot specify volume_size and either one of name or id")
# Here we need to get the zone info for the instance. This covers situation where
# instance is specified but zone isn't.
# Useful for playbooks chaining instance launch with volume create + attach and where the
# zone doesn't matter to the user.
if instance:
reservation = ec2.get_all_instances(instance_ids=instance)
inst = reservation[0].instances[0]
zone = inst.placement
# Check if there is a volume already mounted there.
if device_name:
if device_name in inst.block_device_mapping:
module.exit_json(msg="Volume mapping for %s already exists on instance %s" % (device_name, instance),
volume_id=inst.block_device_mapping[device_name].volume_id,
device=device_name,
changed=False)
if state == 'absent':
delete_volume(module, ec2)
else:
volume = create_volume(module, ec2, zone)
if instance:
attach_volume(module, ec2, volume, inst)
module.exit_json(volume_id=volume.id, device=device_name)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *