ec2: ability to list existing volumes

pull/6847/head
Atlas Health 11 years ago
parent 317c2f4bc0
commit 98edfa46c6

@ -120,10 +120,10 @@ options:
version_added: "1.6" version_added: "1.6"
state: state:
description: description:
- whether to ensure the volume is present or absent - whether to ensure the volume is present or absent, or to list existing volumes
required: false required: false
default: present default: present
choices: ['absent', 'present'] choices: ['absent', 'present', 'list']
version_added: "1.6" version_added: "1.6"
requirements: [ "boto" ] requirements: [ "boto" ]
author: Lester Wade author: Lester Wade
@ -193,6 +193,12 @@ EXAMPLES = '''
module: ec2_vol module: ec2_vol
id: vol-XXXXXXXX id: vol-XXXXXXXX
state: absent state: absent
# List volumes for an instance
- local_action:
module: ec2_vol
instance: i-XXXXXX
state: list
''' '''
# 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.
@ -232,6 +238,17 @@ def get_volume(module, ec2):
module.fail_json(msg="Found more than one volume in zone (if specified) with name: %s" % name) module.fail_json(msg="Found more than one volume in zone (if specified) with name: %s" % name)
return vols[0] return vols[0]
def get_volumes(module, ec2):
instance = module.params.get('instance')
if not instance:
module.fail_json(msg = "Instance must be specified to get volumes")
try:
vols = ec2.get_all_volumes(filters={'attachment.instance-id': instance})
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
return vols
def delete_volume(module, ec2): def delete_volume(module, ec2):
vol = get_volume(module, ec2) vol = get_volume(module, ec2)
@ -336,7 +353,7 @@ def main():
device_name = dict(), device_name = dict(),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']), zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
snapshot = dict(), snapshot = dict(),
state = dict(choices=['absent', 'present'], default='present') state = dict(choices=['absent', 'present', 'list'], default='present')
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
@ -353,6 +370,31 @@ def main():
ec2 = ec2_connect(module) ec2 = ec2_connect(module)
if state == 'list':
returned_volumes = []
vols = get_volumes(module, ec2)
for v in vols:
attachment = v.attach_data
returned_volumes.append({
'create_time': v.create_time,
'id': v.id,
'iops': v.iops,
'size': v.size,
'snapshot_id': v.snapshot_id,
'status': v.status,
'type': v.type,
'zone': v.zone,
'attachment_set': {
'attach_time': attachment.attach_time,
'device': attachment.device,
'status': attachment.status
}
})
module.exit_json(changed=False, volumes=returned_volumes)
if id and name: if id and name:
module.fail_json(msg="Both id and name cannot be specified") module.fail_json(msg="Both id and name cannot be specified")
@ -378,7 +420,8 @@ def main():
if state == 'absent': if state == 'absent':
delete_volume(module, ec2) delete_volume(module, ec2)
else:
if state == 'present':
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)

Loading…
Cancel
Save