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:
2013-08-13 13:30:56 +00:00
aws_secret_key:
description:
- AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used.
required: false
default: None
aliases: ['ec2_secret_key', 'secret_key' ]
aws_access_key:
description:
- AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
required: false
default: None
aliases: ['ec2_access_key', 'access_key' ]
ec2_url:
description:
- Url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints). Must be specified if region is not used. If not set then the value of the EC2_URL environment variable, if any, is used
required: false
default: null
aliases: []
2013-02-14 14:18:35 +00:00
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: []
volume_size:
description:
2013-02-14 14:40:28 +00:00
- size of volume (in GB) to create.
2013-02-14 14:18:35 +00:00
required: true
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-02-14 14:18:35 +00:00
requirements: [ "boto" ]
author: Lester Wade
'''
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
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-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)
2013-08-13 13:30:56 +00:00
AWS_REGIONS = ['ap-northeast-1',
'ap-southeast-1',
'ap-southeast-2',
'eu-west-1',
'sa-east-1',
'us-east-1',
'us-west-1',
'us-west-2']
2013-02-14 14:18:35 +00:00
def main():
module = AnsibleModule(
argument_spec = dict(
instance = dict(),
volume_size = dict(required=True),
2013-08-02 16:31:31 +00:00
iops = dict(),
2013-02-14 14:18:35 +00:00
device_name = dict(),
2013-08-13 13:30:56 +00:00
region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
ec2_url = dict(),
aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True),
aws_access_key = dict(aliases=['ec2_access_key', 'access_key']),
2013-02-14 14:18:35 +00:00
)
)
instance = module.params.get('instance')
volume_size = module.params.get('volume_size')
2013-08-02 16:31:31 +00:00
iops = module.params.get('iops')
2013-02-14 14:18:35 +00:00
device_name = module.params.get('device_name')
2013-04-23 00:27:57 +00:00
region = module.params.get('region')
2013-02-14 14:18:35 +00:00
zone = module.params.get('zone')
ec2_url = module.params.get('ec2_url')
2013-08-13 13:30:56 +00:00
aws_secret_key = module.params.get('aws_secret_key')
aws_access_key = module.params.get('aws_access_key')
2013-02-14 14:18:35 +00:00
# allow eucarc environment variables to be used if ansible vars aren't set
if not ec2_url and 'EC2_URL' in os.environ:
ec2_url = os.environ['EC2_URL']
2013-08-13 13:30:56 +00:00
if not aws_secret_key:
if 'AWS_SECRET_KEY' in os.environ:
aws_secret_key = os.environ['AWS_SECRET_KEY']
elif 'EC2_SECRET_KEY' in os.environ:
aws_secret_key = os.environ['EC2_SECRET_KEY']
if not aws_access_key:
if 'AWS_ACCESS_KEY' in os.environ:
aws_access_key = os.environ['AWS_ACCESS_KEY']
elif 'EC2_ACCESS_KEY' in os.environ:
aws_access_key = os.environ['EC2_ACCESS_KEY']
if not region:
if 'AWS_REGION' in os.environ:
region = os.environ['AWS_REGION']
elif 'EC2_REGION' in os.environ:
region = os.environ['EC2_REGION']
2013-04-23 00:27:57 +00:00
# If we have a region specified, connect to its endpoint.
if region:
try:
2013-08-13 13:30:56 +00:00
ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
2013-04-23 00:27:57 +00:00
except boto.exception.NoAuthHandlerFound, e:
2013-05-11 21:20:38 +00:00
module.fail_json(msg = str(e))
2013-04-23 00:27:57 +00:00
# Otherwise, no region so we fallback to the old connection method
2013-08-13 13:30:56 +00:00
elif ec2_url:
2013-04-23 00:27:57 +00:00
try:
2013-08-13 13:30:56 +00:00
ec2 = boto.connect_ec2_endpoint(ec2_url, aws_access_key, aws_secret_key)
2013-04-23 00:27:57 +00:00
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e))
2013-08-13 13:30:56 +00:00
else:
module.fail_json(msg="Either region or ec2_url must be specified")
2013-02-14 14:18:35 +00:00
# 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
2013-03-17 22:46:33 +00:00
# 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),
2013-08-02 16:31:31 +00:00
2013-03-17 22:46:33 +00:00
changed=False)
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.
try:
2013-08-02 16:31:31 +00:00
volume = ec2.create_volume(volume_size, zone, None, volume_type, iops)
2013-02-14 14:18:35 +00:00
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.
if device_name and instance:
try:
attach = volume.attach(inst.id, device_name)
while volume.attachment_state() != 'attached':
time.sleep(3)
volume.update()
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
# 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-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-03-18 17:02:39 +00:00
2013-02-14 14:18:35 +00:00
if device_name is None and instance:
try:
2013-04-24 16:45:01 +00:00
if not ec2.get_password_data(inst.id):
2013-02-14 14:18:35 +00:00
device_name = '/dev/sdf'
attach = volume.attach(inst.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'
2013-02-14 14:18:35 +00:00
attach = volume.attach(inst.id, device_name)
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
print json.dumps({
"volume_id": volume.id,
"device": device_name
})
sys.exit(0)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()