Merge pull request #5287 from ebrevdo/ec2_vol_existing

ec2_vol module: add support for name / id to mount volume on instance
reviewable/pr18780/r1
jctanner 11 years ago
commit 3945dab508

@ -46,6 +46,18 @@ options:
required: false
default: null
aliases: []
name:
description:
- volume Name tag if you wish to attach an existing volume (requires instance)
required: false
default: null
aliases: []
id:
description:
- volume id if you wish to attach an existing volume (requires instance)
required: false
default: null
aliases: []
volume_size:
description:
- size of volume (in GB) to create.
@ -145,6 +157,26 @@ EXAMPLES = '''
volume_size: 5
with_items: ec2.instances
register: ec2_vol
# Idempotent playbook example combined with single instance launch
# Volume must exist in the same zone; will not do anything if it is
# already attached.
- local_action:
module: ec2
keypair: "{{ keypair }}"
image: "{{ image }}"
zone: YYYYYY
id: my_instance
wait: yes
count: 1
register: ec2
- local_action:
module: ec2_vol
instance: "{{ item.id }}"
name: my_existing_volume_Name_tag
device_name: /dev/xvdf
with_items: ec2.instances
register: ec2_vol
'''
# Note: this module needs to be made idempotent. Possible solution is to use resource tags with the volumes.
@ -165,7 +197,9 @@ def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
instance = dict(),
volume_size = dict(required=True),
id = dict(),
name = dict(),
volume_size = dict(),
iops = dict(),
device_name = dict(),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
@ -174,6 +208,8 @@ def main():
)
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')
@ -183,11 +219,16 @@ def main():
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]
@ -209,14 +250,51 @@ def main():
volume_type = 'standard'
# If no instance supplied, try volume creation based on module parameters.
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:
module.fail_json(msg = "Parameters are not compatible: [id or name] and [iops or volume_size]")
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)
try:
volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops)
while volume.status != 'available':
time.sleep(3)
volume.update()
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
volume = vols.pop()
if volume.attachment_state() is not None:
adata = volume.attach_data
if adata.instance_id != instance:
module.fail_json(msg = "Volume %s is already attached to another instance: %s"
% (name or id, adata.instance_id))
else:
module.exit_json(msg="Volume %s is already mapped on instance %s: %s" %
(name or id, adata.instance_id, adata.device),
volume_id=id,
device=adata.device,
changed=False)
else:
try:
volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops)
while volume.status != 'available':
time.sleep(3)
volume.update()
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
# Attach the created volume.
@ -236,7 +314,6 @@ def main():
# (bounds for devices, as above) with instance.block_device_mapping data would be tricky. For me ;)
# Use password data attribute to tell whether the instance is Windows or Linux
if device_name is None and instance:
try:
if not ec2.get_password_data(inst.id):

Loading…
Cancel
Save