2013-03-19 16:07:36 +00:00
#!/usr/bin/python
2013-02-14 14:18:35 +00:00
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
module: ec2_vol
short_description: create and attach a volume, return volume id and device map
description:
2013-03-17 22:46:33 +00:00
- creates an EBS volume and optionally attaches it to an instance. If both an instance ID and a device name is given and the instance has a device at the device name, then no volume is created and no attachment is made. This module has a dependency on python-boto.
2013-02-14 14:18:35 +00:00
version_added: "1.1"
options:
instance:
description:
2013-02-14 14:40:28 +00:00
- instance ID if you wish to attach the volume.
2013-02-14 14:18:35 +00:00
required: false
default: null
aliases: []
2013-12-13 21:45:42 +00:00
name:
description:
- volume Name tag if you wish to attach an existing volume (requires instance)
required: false
default: null
aliases: []
2014-03-11 16:01:57 +00:00
version_added: "1.6"
2014-02-28 19:56:42 +00:00
id:
2013-12-13 21:45:42 +00:00
description:
2014-03-21 01:04:30 +00:00
- volume id if you wish to attach an existing volume (requires instance) or remove an existing volume
2013-12-13 21:45:42 +00:00
required: false
default: null
aliases: []
2014-03-11 16:01:57 +00:00
version_added: "1.6"
2013-02-14 14:18:35 +00:00
volume_size:
description:
2013-02-14 14:40:28 +00:00
- size of volume (in GB) to create.
2014-03-21 01:04:30 +00:00
required: false
2013-02-14 14:18:35 +00:00
default: null
aliases: []
2013-08-02 16:31:31 +00:00
iops:
description:
- the provisioned IOPs you want to associate with this volume (integer).
required: false
default: 100
aliases: []
2013-08-06 19:54:20 +00:00
version_added: "1.3"
2013-02-14 14:18:35 +00:00
device_name:
description:
2013-03-17 22:30:16 +00:00
- device id to override device mapping. Assumes /dev/sdf for Linux/UNIX and /dev/xvdf for Windows.
2013-02-14 14:18:35 +00:00
required: false
default: null
aliases: []
2013-04-23 00:27:57 +00:00
region:
description:
2013-08-13 13:30:56 +00:00
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
2013-04-23 00:27:57 +00:00
required: false
default: null
2013-08-13 13:30:56 +00:00
aliases: ['aws_region', 'ec2_region']
2013-02-14 14:18:35 +00:00
zone:
description:
- zone in which to create the volume, if unset uses the zone the instance is in (if set)
required: false
default: null
2013-08-13 13:30:56 +00:00
aliases: ['aws_zone', 'ec2_zone']
2013-10-15 06:26:11 +00:00
snapshot:
description:
- snapshot ID on which to base the volume
required: false
default: null
2014-04-08 03:10:24 +00:00
version_added: "1.5"
2014-02-13 18:12:08 +00:00
validate_certs:
description:
- When set to "no", SSL certificates will not be validated for boto versions >= 2.6.0.
required: false
default: "yes"
choices: ["yes", "no"]
aliases: []
version_added: "1.5"
2014-03-21 01:04:30 +00:00
state:
description:
2014-04-03 09:39:07 +00:00
- whether to ensure the volume is present or absent, or to list existing volumes
2014-03-21 01:04:30 +00:00
required: false
default: present
2014-04-03 09:39:07 +00:00
choices: ['absent', 'present', 'list']
2014-03-21 01:04:30 +00:00
version_added: "1.6"
2013-02-14 14:18:35 +00:00
author: Lester Wade
2014-04-09 06:43:55 +00:00
extends_documentation_fragment: aws
2013-02-14 14:18:35 +00:00
'''
2013-04-26 02:22:48 +00:00
EXAMPLES = '''
# Simple attachment action
2013-06-17 06:53:46 +00:00
- local_action:
2013-04-26 02:22:48 +00:00
module: ec2_vol
instance: XXXXXX
volume_size: 5
device_name: sdd
2013-08-02 16:31:31 +00:00
# Example using custom iops params
- local_action:
module: ec2_vol
instance: XXXXXX
volume_size: 5
iops: 200
device_name: sdd
2013-08-10 21:58:03 +00:00
2014-01-28 14:58:13 +00:00
# Example using snapshot id
- local_action:
module: ec2_vol
instance: XXXXXX
snapshot: "{{ snapshot }}"
2013-04-26 02:22:48 +00:00
# Playbook example combined with instance launch
2013-06-17 06:53:46 +00:00
- local_action:
2013-04-26 02:22:48 +00:00
module: ec2
2013-07-19 13:40:53 +00:00
keypair: "{{ keypair }}"
image: "{{ image }}"
2013-04-26 02:22:48 +00:00
wait: yes
count: 3
register: ec2
2013-06-17 06:53:46 +00:00
- local_action:
2013-04-26 02:22:48 +00:00
module: ec2_vol
2013-07-19 13:40:53 +00:00
instance: "{{ item.id }} "
2013-04-26 02:22:48 +00:00
volume_size: 5
2013-07-19 13:40:53 +00:00
with_items: ec2.instances
2013-04-26 02:22:48 +00:00
register: ec2_vol
2013-12-13 21:45:42 +00:00
2014-03-11 16:01:57 +00:00
# Example: Launch an instance and then add a volue if not already present
# * Nothing will happen if the volume is already attached.
# * Volume must exist in the same zone.
2013-12-13 21:45:42 +00:00
- local_action:
module: ec2
keypair: "{{ keypair }}"
image: "{{ image }}"
zone: YYYYYY
id: my_instance
wait: yes
count: 1
register: ec2
2014-03-21 01:04:30 +00:00
2013-12-13 21:45:42 +00:00
- 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
2014-03-21 01:04:30 +00:00
# Remove a volume
2014-05-07 08:35:16 +00:00
- local_action:
2014-03-21 01:04:30 +00:00
module: ec2_vol
id: vol-XXXXXXXX
state: absent
2014-04-03 09:39:07 +00:00
# List volumes for an instance
- local_action:
module: ec2_vol
instance: i-XXXXXX
state: list
2013-04-26 02:22:48 +00:00
'''
2013-02-18 10:14:09 +00:00
# Note: this module needs to be made idempotent. Possible solution is to use resource tags with the volumes.
# if state=present and it doesn't exist, create, tag and attach.
# Check for state by looking for volume attachment with tag (and against block device mapping?).
# Would personally like to revisit this in May when Eucalyptus also has tagging support (3.3).
2013-02-14 14:18:35 +00:00
import sys
import time
try:
2013-04-23 00:27:57 +00:00
import boto.ec2
2013-02-14 14:18:35 +00:00
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
2014-03-21 01:04:30 +00:00
def get_volume(module, ec2):
name = module.params.get('name')
2013-12-13 21:45:42 +00:00
id = module.params.get('id')
2014-03-21 01:04:30 +00:00
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]
2014-04-03 09:39:07 +00:00
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
2014-03-21 01:04:30 +00:00
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):
2013-12-13 21:45:42 +00:00
name = module.params.get('name')
2014-03-21 01:04:30 +00:00
id = module.params.get('id')
2013-02-14 14:18:35 +00:00
instance = module.params.get('instance')
2013-08-02 16:31:31 +00:00
iops = module.params.get('iops')
2014-03-21 01:04:30 +00:00
volume_size = module.params.get('volume_size')
2013-10-15 06:26:11 +00:00
snapshot = module.params.get('snapshot')
2013-08-02 16:31:31 +00:00
# If custom iops is defined we use volume_type "io1" rather than the default of "standard"
if iops:
volume_type = 'io1'
else:
volume_type = 'standard'
2013-02-14 14:18:35 +00:00
# If no instance supplied, try volume creation based on module parameters.
2013-12-13 21:45:42 +00:00
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]")
2014-03-21 01:04:30 +00:00
volume = get_volume(module, ec2)
2013-12-13 21:45:42 +00:00
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))
2014-03-21 01:04:30 +00:00
return volume
2013-02-14 14:18:35 +00:00
2014-03-21 01:04:30 +00:00
def attach_volume(module, ec2, volume, instance):
device_name = module.params.get('device_name')
2013-02-14 14:18:35 +00:00
if device_name and instance:
try:
2014-03-21 01:04:30 +00:00
attach = volume.attach(instance.id, device_name)
2013-02-14 14:18:35 +00:00
while volume.attachment_state() != 'attached':
time.sleep(3)
volume.update()
except boto.exception.BotoServerError, e:
2013-12-13 21:45:42 +00:00
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
2013-02-14 14:18:35 +00:00
# If device_name isn't set, make a choice based on best practices here:
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
2013-12-13 21:45:42 +00:00
2013-02-14 14:40:28 +00:00
# In future this needs to be more dynamic but combining block device mapping best practices
# (bounds for devices, as above) with instance.block_device_mapping data would be tricky. For me ;)
2013-03-18 12:00:52 +00:00
# Use password data attribute to tell whether the instance is Windows or Linux
2013-02-14 14:18:35 +00:00
if device_name is None and instance:
try:
2014-04-30 09:40:23 +00:00
if not ec2.get_password_data(instance.id):
2013-02-14 14:18:35 +00:00
device_name = '/dev/sdf'
2014-04-30 09:40:23 +00:00
attach = volume.attach(instance.id, device_name)
2013-03-18 12:00:52 +00:00
while volume.attachment_state() != 'attached':
time.sleep(3)
volume.update()
2013-02-14 14:18:35 +00:00
else:
2013-03-18 17:02:39 +00:00
device_name = '/dev/xvdf'
2014-04-30 09:40:23 +00:00
attach = volume.attach(instance.id, device_name)
2013-02-14 14:18:35 +00:00
while volume.attachment_state() != 'attached':
time.sleep(3)
volume.update()
except boto.exception.BotoServerError, e:
2013-03-18 12:00:52 +00:00
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
2013-02-14 14:18:35 +00:00
2014-03-21 01:04:30 +00:00
def main():
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(),
2014-04-03 09:39:07 +00:00
state = dict(choices=['absent', 'present', 'list'], default='present')
2014-03-21 01:04:30 +00:00
)
)
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)
2014-04-03 09:39:07 +00:00
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)
2014-03-21 01:04:30 +00:00
if id and name:
module.fail_json(msg="Both id and name cannot be specified")
# 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)
2014-05-15 02:14:05 +00:00
# Delaying the checks until after the instance check allows us to get volume ids for existing volumes
# without needing to pass an unused volume_size
if not volume_size and not (id or name):
module.fail_json(msg="You must specify an existing volume with id or name or a volume_size")
if volume_size and (id or name):
module.fail_json(msg="Cannot specify volume_size and either one of name or id")
2014-03-21 01:04:30 +00:00
if state == 'absent':
delete_volume(module, ec2)
2014-04-03 09:39:07 +00:00
if state == 'present':
2014-03-21 01:04:30 +00:00
volume = create_volume(module, ec2, zone)
if instance:
attach_volume(module, ec2, volume, inst)
module.exit_json(volume_id=volume.id, device=device_name)
2013-02-14 14:18:35 +00:00
2013-11-01 15:59:24 +00:00
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
2013-02-14 14:18:35 +00:00
main()