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 required: false
default: null default: null
aliases: [] 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: volume_size:
description: description:
- size of volume (in GB) to create. - size of volume (in GB) to create.
@ -145,6 +157,26 @@ EXAMPLES = '''
volume_size: 5 volume_size: 5
with_items: ec2.instances with_items: ec2.instances
register: ec2_vol 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. # 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 = ec2_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
instance = dict(), instance = dict(),
volume_size = dict(required=True), id = dict(),
name = dict(),
volume_size = dict(),
iops = dict(), iops = dict(),
device_name = dict(), device_name = dict(),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']), zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
@ -174,6 +208,8 @@ def main():
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
id = module.params.get('id')
name = module.params.get('name')
instance = module.params.get('instance') instance = module.params.get('instance')
volume_size = module.params.get('volume_size') volume_size = module.params.get('volume_size')
iops = module.params.get('iops') iops = module.params.get('iops')
@ -183,11 +219,16 @@ def main():
ec2 = ec2_connect(module) 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 # Here we need to get the zone info for the instance. This covers situation where
# instance is specified but zone isn't. # instance is specified but zone isn't.
# Useful for playbooks chaining instance launch with volume create + attach and where the # Useful for playbooks chaining instance launch with volume create + attach and where the
# zone doesn't matter to the user. # zone doesn't matter to the user.
if instance: if instance:
reservation = ec2.get_all_instances(instance_ids=instance) reservation = ec2.get_all_instances(instance_ids=instance)
inst = reservation[0].instances[0] inst = reservation[0].instances[0]
@ -209,7 +250,44 @@ def main():
volume_type = 'standard' volume_type = 'standard'
# 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 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)
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: try:
volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops) volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops)
while volume.status != 'available': while volume.status != 'available':
@ -236,7 +314,6 @@ def main():
# (bounds for devices, as above) with instance.block_device_mapping data would be tricky. For me ;) # (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 # Use password data attribute to tell whether the instance is Windows or Linux
if device_name is None and instance: if device_name is None and instance:
try: try:
if not ec2.get_password_data(inst.id): if not ec2.get_password_data(inst.id):

Loading…
Cancel
Save