2013-03-19 17:07:36 +01:00
|
|
|
#!/usr/bin/python
|
2013-02-14 15:18:35 +01: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 23:46:33 +01: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 15:18:35 +01:00
|
|
|
version_added: "1.1"
|
|
|
|
options:
|
|
|
|
instance:
|
|
|
|
description:
|
2015-02-18 19:32:50 +01:00
|
|
|
- instance ID if you wish to attach the volume. Since 1.9 you can set to None to detach.
|
2013-02-14 15:18:35 +01:00
|
|
|
required: false
|
2015-02-18 19:32:50 +01:00
|
|
|
default: null
|
2013-12-13 22:45:42 +01:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- volume Name tag if you wish to attach an existing volume (requires instance)
|
|
|
|
required: false
|
|
|
|
default: null
|
2014-03-11 17:01:57 +01:00
|
|
|
version_added: "1.6"
|
2014-02-28 20:56:42 +01:00
|
|
|
id:
|
2013-12-13 22:45:42 +01:00
|
|
|
description:
|
2014-03-21 02:04:30 +01:00
|
|
|
- volume id if you wish to attach an existing volume (requires instance) or remove an existing volume
|
2013-12-13 22:45:42 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2014-03-11 17:01:57 +01:00
|
|
|
version_added: "1.6"
|
2013-02-14 15:18:35 +01:00
|
|
|
volume_size:
|
|
|
|
description:
|
2013-02-14 15:40:28 +01:00
|
|
|
- size of volume (in GB) to create.
|
2014-03-21 02:04:30 +01:00
|
|
|
required: false
|
2013-02-14 15:18:35 +01:00
|
|
|
default: null
|
2014-09-27 04:29:08 +02:00
|
|
|
volume_type:
|
|
|
|
description:
|
|
|
|
- Type of EBS volume; standard (magnetic), gp2 (SSD), io1 (Provisioned IOPS). "Standard" is the old EBS default
|
2015-12-01 04:02:28 +01:00
|
|
|
and continues to remain the Ansible default for backwards compatibility.
|
2014-09-27 04:29:08 +02:00
|
|
|
required: false
|
|
|
|
default: standard
|
2015-01-19 15:32:50 +01:00
|
|
|
version_added: "1.9"
|
2013-08-02 18:31:31 +02:00
|
|
|
iops:
|
|
|
|
description:
|
|
|
|
- the provisioned IOPs you want to associate with this volume (integer).
|
|
|
|
required: false
|
|
|
|
default: 100
|
2013-08-06 21:54:20 +02:00
|
|
|
version_added: "1.3"
|
2014-08-25 23:10:00 +02:00
|
|
|
encrypted:
|
|
|
|
description:
|
|
|
|
- Enable encryption at rest for this volume.
|
|
|
|
default: false
|
|
|
|
version_added: "1.8"
|
2013-02-14 15:18:35 +01:00
|
|
|
device_name:
|
|
|
|
description:
|
2013-03-17 23:30:16 +01:00
|
|
|
- device id to override device mapping. Assumes /dev/sdf for Linux/UNIX and /dev/xvdf for Windows.
|
2013-02-14 15:18:35 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2016-01-07 13:14:10 +01:00
|
|
|
delete_on_termination:
|
|
|
|
description:
|
|
|
|
- When set to "yes", the volume will be deleted upon instance termination.
|
|
|
|
required: false
|
|
|
|
default: "no"
|
|
|
|
choices: ["yes", "no"]
|
2013-02-14 15:18:35 +01:00
|
|
|
zone:
|
|
|
|
description:
|
2015-12-01 04:02:28 +01:00
|
|
|
- zone in which to create the volume, if unset uses the zone the instance is in (if set)
|
2013-02-14 15:18:35 +01:00
|
|
|
required: false
|
|
|
|
default: null
|
2013-08-13 15:30:56 +02:00
|
|
|
aliases: ['aws_zone', 'ec2_zone']
|
2013-10-15 08:26:11 +02:00
|
|
|
snapshot:
|
|
|
|
description:
|
|
|
|
- snapshot ID on which to base the volume
|
|
|
|
required: false
|
|
|
|
default: null
|
2014-04-08 05:10:24 +02:00
|
|
|
version_added: "1.5"
|
2014-02-13 19:12:08 +01: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"]
|
|
|
|
version_added: "1.5"
|
2014-03-21 02:04:30 +01:00
|
|
|
state:
|
2015-12-01 04:02:28 +01:00
|
|
|
description:
|
2014-09-16 20:20:39 +02:00
|
|
|
- whether to ensure the volume is present or absent, or to list existing volumes (The C(list) option was added in version 1.8).
|
2014-03-21 02:04:30 +01:00
|
|
|
required: false
|
|
|
|
default: present
|
2014-04-03 11:39:07 +02:00
|
|
|
choices: ['absent', 'present', 'list']
|
2014-03-21 02:04:30 +01:00
|
|
|
version_added: "1.6"
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Lester Wade (@lwade)"
|
2015-10-07 22:16:24 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- aws
|
|
|
|
- ec2
|
2013-02-14 15:18:35 +01:00
|
|
|
'''
|
|
|
|
|
2013-04-26 04:22:48 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Simple attachment action
|
2015-12-01 04:02:28 +01:00
|
|
|
- ec2_vol:
|
|
|
|
instance: XXXXXX
|
|
|
|
volume_size: 5
|
2013-04-26 04:22:48 +02:00
|
|
|
device_name: sdd
|
2013-08-02 18:31:31 +02:00
|
|
|
|
2015-12-01 04:02:28 +01:00
|
|
|
# Example using custom iops params
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2015-12-01 04:02:28 +01:00
|
|
|
instance: XXXXXX
|
|
|
|
volume_size: 5
|
2015-10-01 05:23:41 +02:00
|
|
|
iops: 100
|
2013-08-02 18:31:31 +02:00
|
|
|
device_name: sdd
|
2013-08-10 23:58:03 +02:00
|
|
|
|
2014-01-28 15:58:13 +01:00
|
|
|
# Example using snapshot id
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2014-01-28 15:58:13 +01:00
|
|
|
instance: XXXXXX
|
|
|
|
snapshot: "{{ snapshot }}"
|
|
|
|
|
2015-12-01 04:02:28 +01:00
|
|
|
# Playbook example combined with instance launch
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2:
|
2013-07-19 15:40:53 +02:00
|
|
|
keypair: "{{ keypair }}"
|
|
|
|
image: "{{ image }}"
|
2015-12-01 04:02:28 +01:00
|
|
|
wait: yes
|
2013-04-26 04:22:48 +02:00
|
|
|
count: 3
|
2015-04-09 12:10:49 +02:00
|
|
|
register: ec2
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2015-12-01 04:02:28 +01:00
|
|
|
instance: "{{ item.id }} "
|
2013-04-26 04:22:48 +02:00
|
|
|
volume_size: 5
|
2015-07-28 08:40:49 +02:00
|
|
|
with_items: ec2.instances
|
2015-04-09 12:10:49 +02:00
|
|
|
register: ec2_vol
|
2013-12-13 22:45:42 +01:00
|
|
|
|
2015-01-30 15:53:46 +01:00
|
|
|
# Example: Launch an instance and then add a volume if not already attached
|
|
|
|
# * Volume will be created with the given name if not already created.
|
2014-03-11 17:01:57 +01:00
|
|
|
# * Nothing will happen if the volume is already attached.
|
2015-08-14 20:01:44 +02:00
|
|
|
# * Requires Ansible 2.0
|
2014-03-11 17:01:57 +01:00
|
|
|
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2:
|
2013-12-13 22:45:42 +01:00
|
|
|
keypair: "{{ keypair }}"
|
|
|
|
image: "{{ image }}"
|
|
|
|
zone: YYYYYY
|
|
|
|
id: my_instance
|
2015-02-18 19:32:50 +01:00
|
|
|
wait: yes
|
2013-12-13 22:45:42 +01:00
|
|
|
count: 1
|
2015-04-09 12:10:49 +02:00
|
|
|
register: ec2
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2015-02-18 19:32:50 +01:00
|
|
|
instance: "{{ item.id }}"
|
2013-12-13 22:45:42 +01:00
|
|
|
name: my_existing_volume_Name_tag
|
|
|
|
device_name: /dev/xvdf
|
2015-06-18 17:48:27 +02:00
|
|
|
with_items: ec2.instances
|
|
|
|
register: ec2_vol
|
2014-03-21 02:04:30 +01:00
|
|
|
|
|
|
|
# Remove a volume
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2014-03-21 02:04:30 +01:00
|
|
|
id: vol-XXXXXXXX
|
|
|
|
state: absent
|
2014-04-03 11:39:07 +02:00
|
|
|
|
2015-02-18 19:32:50 +01:00
|
|
|
# Detach a volume (since 1.9)
|
2014-09-27 04:02:25 +02:00
|
|
|
- ec2_vol:
|
|
|
|
id: vol-XXXXXXXX
|
|
|
|
instance: None
|
|
|
|
|
2014-04-03 11:39:07 +02:00
|
|
|
# List volumes for an instance
|
2014-12-01 21:14:57 +01:00
|
|
|
- ec2_vol:
|
2014-04-03 11:39:07 +02:00
|
|
|
instance: i-XXXXXX
|
|
|
|
state: list
|
2015-02-18 19:32:50 +01:00
|
|
|
|
2014-09-27 04:29:08 +02:00
|
|
|
# Create new volume using SSD storage
|
2014-09-27 04:02:25 +02:00
|
|
|
- ec2_vol:
|
|
|
|
instance: XXXXXX
|
|
|
|
volume_size: 50
|
2014-09-27 04:29:08 +02:00
|
|
|
volume_type: gp2
|
|
|
|
device_name: /dev/xvdf
|
2016-01-07 13:14:10 +01:00
|
|
|
|
|
|
|
# Attach an existing volume to instance. The volume will be deleted upon instance termination.
|
|
|
|
- ec2_vol:
|
|
|
|
instance: XXXXXX
|
|
|
|
id: XXXXXX
|
|
|
|
device_name: /dev/sdf
|
|
|
|
delete_on_termination: yes
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
device:
|
|
|
|
description: device name of attached volume
|
|
|
|
returned: when success
|
|
|
|
type: string
|
|
|
|
sample: "/def/sdf"
|
|
|
|
volume_id:
|
|
|
|
description: the id of volume
|
|
|
|
returned: when success
|
|
|
|
type: string
|
|
|
|
sample: "vol-35b333d9"
|
|
|
|
volume_type:
|
|
|
|
description: the volume type
|
|
|
|
returned: when success
|
|
|
|
type: string
|
|
|
|
sample: "standard"
|
|
|
|
volume:
|
|
|
|
description: a dictionary containing detailed attributes of the volume
|
|
|
|
returned: when success
|
|
|
|
type: string
|
|
|
|
sample: {
|
|
|
|
"attachment_set": {
|
|
|
|
"attach_time": "2015-10-23T00:22:29.000Z",
|
|
|
|
"deleteOnTermination": "false",
|
|
|
|
"device": "/dev/sdf",
|
|
|
|
"instance_id": "i-8356263c",
|
|
|
|
"status": "attached"
|
|
|
|
},
|
|
|
|
"create_time": "2015-10-21T14:36:08.870Z",
|
|
|
|
"encrypted": false,
|
|
|
|
"id": "vol-35b333d9",
|
|
|
|
"iops": null,
|
|
|
|
"size": 1,
|
|
|
|
"snapshot_id": "",
|
|
|
|
"status": "in-use",
|
|
|
|
"tags": {
|
|
|
|
"env": "dev"
|
|
|
|
},
|
|
|
|
"type": "standard",
|
|
|
|
"zone": "us-east-1b"
|
|
|
|
}
|
2013-04-26 04:22:48 +02:00
|
|
|
'''
|
|
|
|
|
2013-02-14 15:18:35 +01:00
|
|
|
import time
|
|
|
|
|
2014-08-25 23:10:00 +02:00
|
|
|
from distutils.version import LooseVersion
|
|
|
|
|
2013-02-14 15:18:35 +01:00
|
|
|
try:
|
2013-04-23 02:27:57 +02:00
|
|
|
import boto.ec2
|
2015-10-01 05:23:41 +02:00
|
|
|
from boto.exception import BotoServerError
|
2016-01-07 13:14:10 +01:00
|
|
|
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2013-02-14 15:18:35 +01:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
|
|
|
|
2013-02-14 15:18:35 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
def get_volume(module, ec2):
|
|
|
|
name = module.params.get('name')
|
2013-12-13 22:45:42 +01:00
|
|
|
id = module.params.get('id')
|
2014-03-21 02:04:30 +01:00
|
|
|
zone = module.params.get('zone')
|
|
|
|
filters = {}
|
|
|
|
volume_ids = None
|
2015-10-01 05:23:41 +02:00
|
|
|
|
|
|
|
# If no name or id supplied, just try volume creation based on module parameters
|
|
|
|
if id is None and name is None:
|
|
|
|
return None
|
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
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:
|
2015-01-30 15:53:46 +01:00
|
|
|
if id:
|
|
|
|
msg = "Could not find the volume with id: %s" % id
|
|
|
|
if name:
|
|
|
|
msg += (" and name: %s" % name)
|
|
|
|
module.fail_json(msg=msg)
|
|
|
|
else:
|
|
|
|
return None
|
2015-10-01 05:23:41 +02:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
if len(vols) > 1:
|
|
|
|
module.fail_json(msg="Found more than one volume in zone (if specified) with name: %s" % name)
|
|
|
|
return vols[0]
|
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
2014-04-03 11:39:07 +02:00
|
|
|
def get_volumes(module, ec2):
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2014-04-03 11:39:07 +02:00
|
|
|
instance = module.params.get('instance')
|
|
|
|
|
|
|
|
try:
|
2015-10-01 05:23:41 +02:00
|
|
|
if not instance:
|
|
|
|
vols = ec2.get_all_volumes()
|
|
|
|
else:
|
|
|
|
vols = ec2.get_all_volumes(filters={'attachment.instance-id': instance})
|
2014-04-03 11:39:07 +02:00
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
|
|
|
return vols
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
def delete_volume(module, ec2):
|
2015-06-04 10:41:02 +02:00
|
|
|
volume_id = module.params['id']
|
|
|
|
try:
|
|
|
|
ec2.delete_volume(volume_id)
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
except boto.exception.EC2ResponseError as ec2_error:
|
|
|
|
if ec2_error.code == 'InvalidVolume.NotFound':
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
module.fail_json(msg=ec2_error.message)
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
2014-08-25 23:10:00 +02:00
|
|
|
def boto_supports_volume_encryption():
|
|
|
|
"""
|
|
|
|
Check if Boto library supports encryption of EBS volumes (added in 2.29.0)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if boto library has the named param as an argument on the request_spot_instances method, else False
|
|
|
|
"""
|
|
|
|
return hasattr(boto, 'Version') and LooseVersion(boto.Version) >= LooseVersion('2.29.0')
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
def create_volume(module, ec2, zone):
|
2015-10-01 05:23:41 +02:00
|
|
|
changed = False
|
2013-12-13 22:45:42 +01:00
|
|
|
name = module.params.get('name')
|
2013-08-02 18:31:31 +02:00
|
|
|
iops = module.params.get('iops')
|
2014-08-25 23:10:00 +02:00
|
|
|
encrypted = module.params.get('encrypted')
|
2014-03-21 02:04:30 +01:00
|
|
|
volume_size = module.params.get('volume_size')
|
2014-09-27 04:29:08 +02:00
|
|
|
volume_type = module.params.get('volume_type')
|
2013-10-15 08:26:11 +02:00
|
|
|
snapshot = module.params.get('snapshot')
|
2013-08-02 18:31:31 +02:00
|
|
|
# If custom iops is defined we use volume_type "io1" rather than the default of "standard"
|
|
|
|
if iops:
|
|
|
|
volume_type = 'io1'
|
|
|
|
|
2015-01-30 15:53:46 +01:00
|
|
|
volume = get_volume(module, ec2)
|
2015-10-01 05:23:41 +02:00
|
|
|
if volume is None:
|
2013-12-13 22:45:42 +01:00
|
|
|
try:
|
2014-08-25 23:10:00 +02:00
|
|
|
if boto_supports_volume_encryption():
|
|
|
|
volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops, encrypted)
|
2015-10-01 05:23:41 +02:00
|
|
|
changed = True
|
2014-08-25 23:10:00 +02:00
|
|
|
else:
|
|
|
|
volume = ec2.create_volume(volume_size, zone, snapshot, volume_type, iops)
|
2015-10-01 05:23:41 +02:00
|
|
|
changed = True
|
2014-08-25 23:10:00 +02:00
|
|
|
|
2013-12-13 22:45:42 +01:00
|
|
|
while volume.status != 'available':
|
|
|
|
time.sleep(3)
|
|
|
|
volume.update()
|
2015-01-30 15:53:46 +01:00
|
|
|
|
|
|
|
if name:
|
|
|
|
ec2.create_tags([volume.id], {"Name": name})
|
2013-12-13 22:45:42 +01:00
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
2015-01-30 15:53:46 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
return volume, changed
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2013-02-14 15:18:35 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
def attach_volume(module, ec2, volume, instance):
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
device_name = module.params.get('device_name')
|
2016-01-07 13:14:10 +01:00
|
|
|
delete_on_termination = module.params.get('delete_on_termination')
|
2015-10-01 05:23:41 +02:00
|
|
|
changed = False
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2013-02-14 15:18:35 +01: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
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2013-02-14 15:40:28 +01: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 ;)
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2013-03-18 13:00:52 +01:00
|
|
|
# Use password data attribute to tell whether the instance is Windows or Linux
|
2015-10-01 05:23:41 +02:00
|
|
|
if device_name is None:
|
2013-02-14 15:18:35 +01:00
|
|
|
try:
|
2014-04-30 11:40:23 +02:00
|
|
|
if not ec2.get_password_data(instance.id):
|
2013-02-14 15:18:35 +01:00
|
|
|
device_name = '/dev/sdf'
|
|
|
|
else:
|
2013-03-18 18:02:39 +01:00
|
|
|
device_name = '/dev/xvdf'
|
2013-02-14 15:18:35 +01:00
|
|
|
except boto.exception.BotoServerError, e:
|
2013-03-18 13:00:52 +01:00
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
if volume.attachment_state() is not None:
|
|
|
|
adata = volume.attach_data
|
|
|
|
if adata.instance_id != instance.id:
|
|
|
|
module.fail_json(msg = "Volume %s is already attached to another instance: %s"
|
|
|
|
% (volume.id, adata.instance_id))
|
2016-01-07 13:14:10 +01:00
|
|
|
else:
|
|
|
|
# Volume is already attached to right instance
|
|
|
|
changed = modify_dot_attribute(module, ec2, instance, device_name)
|
2014-09-27 04:02:25 +02:00
|
|
|
else:
|
2015-10-01 05:23:41 +02:00
|
|
|
try:
|
|
|
|
volume.attach(instance.id, device_name)
|
|
|
|
while volume.attachment_state() != 'attached':
|
|
|
|
time.sleep(3)
|
|
|
|
volume.update()
|
|
|
|
changed = True
|
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
modify_dot_attribute(module, ec2, instance, device_name)
|
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
return volume, changed
|
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
|
|
|
def modify_dot_attribute(module, ec2, instance, device_name):
|
|
|
|
""" Modify delete_on_termination attribute """
|
|
|
|
|
|
|
|
delete_on_termination = module.params.get('delete_on_termination')
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
instance.update()
|
|
|
|
dot = instance.block_device_mapping[device_name].delete_on_termination
|
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
|
|
|
|
|
|
|
if delete_on_termination != dot:
|
|
|
|
try:
|
|
|
|
bdt = BlockDeviceType(delete_on_termination=delete_on_termination)
|
|
|
|
bdm = BlockDeviceMapping()
|
|
|
|
bdm[device_name] = bdt
|
|
|
|
|
|
|
|
ec2.modify_instance_attribute(instance_id=instance.id, attribute='blockDeviceMapping', value=bdm)
|
|
|
|
|
|
|
|
while instance.block_device_mapping[device_name].delete_on_termination != delete_on_termination:
|
|
|
|
time.sleep(3)
|
|
|
|
instance.update()
|
|
|
|
changed = True
|
|
|
|
except boto.exception.BotoServerError, e:
|
|
|
|
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
|
|
|
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
def detach_volume(module, ec2, volume):
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
changed = False
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
if volume.attachment_state() is not None:
|
|
|
|
adata = volume.attach_data
|
|
|
|
volume.detach()
|
|
|
|
while volume.attachment_state() is not None:
|
|
|
|
time.sleep(3)
|
|
|
|
volume.update()
|
|
|
|
changed = True
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
return volume, changed
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
def get_volume_info(volume, state):
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
# If we're just listing volumes then do nothing, else get the latest update for the volume
|
|
|
|
if state != 'list':
|
|
|
|
volume.update()
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
volume_info = {}
|
|
|
|
attachment = volume.attach_data
|
|
|
|
|
|
|
|
volume_info = {
|
|
|
|
'create_time': volume.create_time,
|
2016-01-07 13:14:10 +01:00
|
|
|
'encrypted': volume.encrypted,
|
2015-10-01 05:23:41 +02:00
|
|
|
'id': volume.id,
|
|
|
|
'iops': volume.iops,
|
|
|
|
'size': volume.size,
|
|
|
|
'snapshot_id': volume.snapshot_id,
|
|
|
|
'status': volume.status,
|
|
|
|
'type': volume.type,
|
|
|
|
'zone': volume.zone,
|
|
|
|
'attachment_set': {
|
|
|
|
'attach_time': attachment.attach_time,
|
|
|
|
'device': attachment.device,
|
|
|
|
'instance_id': attachment.instance_id,
|
|
|
|
'status': attachment.status
|
|
|
|
},
|
|
|
|
'tags': volume.tags
|
|
|
|
}
|
2016-01-07 13:14:10 +01:00
|
|
|
if hasattr(attachment, 'deleteOnTermination'):
|
|
|
|
volume_info['attachment_set']['deleteOnTermination'] = attachment.deleteOnTermination
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
return volume_info
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2016-01-07 13:14:10 +01:00
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
def main():
|
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(dict(
|
|
|
|
instance = dict(),
|
|
|
|
id = dict(),
|
|
|
|
name = dict(),
|
|
|
|
volume_size = dict(),
|
2014-09-27 04:29:08 +02:00
|
|
|
volume_type = dict(choices=['standard', 'gp2', 'io1'], default='standard'),
|
2014-03-21 02:04:30 +01:00
|
|
|
iops = dict(),
|
2015-12-09 04:07:16 +01:00
|
|
|
encrypted = dict(type='bool', default=False),
|
2014-03-21 02:04:30 +01:00
|
|
|
device_name = dict(),
|
2016-01-07 13:14:10 +01:00
|
|
|
delete_on_termination = dict(type='bool', default=False),
|
2014-03-21 02:04:30 +01:00
|
|
|
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
|
|
|
|
snapshot = dict(),
|
2014-04-03 11:39:07 +02:00
|
|
|
state = dict(choices=['absent', 'present', 'list'], default='present')
|
2014-03-21 02:04:30 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2014-03-21 02:04:30 +01:00
|
|
|
id = module.params.get('id')
|
|
|
|
name = module.params.get('name')
|
|
|
|
instance = module.params.get('instance')
|
|
|
|
volume_size = module.params.get('volume_size')
|
2014-08-25 23:10:00 +02:00
|
|
|
encrypted = module.params.get('encrypted')
|
2014-03-21 02:04:30 +01:00
|
|
|
device_name = module.params.get('device_name')
|
|
|
|
zone = module.params.get('zone')
|
|
|
|
snapshot = module.params.get('snapshot')
|
|
|
|
state = module.params.get('state')
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
# Ensure we have the zone or can get the zone
|
|
|
|
if instance is None and zone is None and state == 'present':
|
|
|
|
module.fail_json(msg="You must specify either instance or zone")
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
# Set volume detach flag
|
2014-09-27 04:02:25 +02:00
|
|
|
if instance == 'None' or instance == '':
|
|
|
|
instance = None
|
2015-10-01 05:23:41 +02:00
|
|
|
detach_vol_flag = True
|
|
|
|
else:
|
|
|
|
detach_vol_flag = False
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
# Set changed flag
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
if region:
|
|
|
|
try:
|
|
|
|
ec2 = connect_to_aws(boto.ec2, region, **aws_connect_params)
|
2015-12-01 04:02:28 +01:00
|
|
|
except (boto.exception.NoAuthHandlerFound, AnsibleAWSError), e:
|
2015-10-01 05:23:41 +02:00
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="region must be specified")
|
2014-03-21 02:04:30 +01:00
|
|
|
|
2014-04-03 11:39:07 +02:00
|
|
|
if state == 'list':
|
|
|
|
returned_volumes = []
|
|
|
|
vols = get_volumes(module, ec2)
|
|
|
|
|
|
|
|
for v in vols:
|
|
|
|
attachment = v.attach_data
|
|
|
|
|
2015-10-01 05:23:41 +02:00
|
|
|
returned_volumes.append(get_volume_info(v, state))
|
2014-04-03 11:39:07 +02:00
|
|
|
|
|
|
|
module.exit_json(changed=False, volumes=returned_volumes)
|
|
|
|
|
2014-08-25 23:10:00 +02:00
|
|
|
if encrypted and not boto_supports_volume_encryption():
|
|
|
|
module.fail_json(msg="You must use boto >= v2.29.0 to use encrypted volumes")
|
|
|
|
|
2015-01-30 15:53:46 +01:00
|
|
|
# Here we need to get the zone info for the instance. This covers situation where
|
2014-03-21 02:04:30 +01:00
|
|
|
# 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.
|
2015-10-01 05:23:41 +02:00
|
|
|
inst = None
|
2014-03-21 02:04:30 +01:00
|
|
|
if instance:
|
2015-10-01 05:23:41 +02:00
|
|
|
try:
|
|
|
|
reservation = ec2.get_all_instances(instance_ids=instance)
|
|
|
|
except BotoServerError as e:
|
|
|
|
module.fail_json(msg=e.message)
|
2014-03-21 02:04:30 +01:00
|
|
|
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 04:14:05 +02: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
|
2015-07-17 15:54:39 +02:00
|
|
|
if not volume_size and not (id or name or snapshot):
|
|
|
|
module.fail_json(msg="You must specify volume_size or identify an existing volume by id, name, or snapshot")
|
2014-05-15 04:14:05 +02:00
|
|
|
|
2015-07-17 15:54:39 +02:00
|
|
|
if volume_size and (id or snapshot):
|
|
|
|
module.fail_json(msg="Cannot specify volume_size together with id or snapshot")
|
2015-12-01 04:02:28 +01:00
|
|
|
|
2014-04-03 11:39:07 +02:00
|
|
|
if state == 'present':
|
2015-10-01 05:23:41 +02:00
|
|
|
volume, changed = create_volume(module, ec2, zone)
|
|
|
|
if detach_vol_flag:
|
2015-12-01 04:02:28 +01:00
|
|
|
volume, changed = detach_volume(module, ec2, volume)
|
2015-10-01 05:23:41 +02:00
|
|
|
elif inst is not None:
|
|
|
|
volume, changed = attach_volume(module, ec2, volume, inst)
|
|
|
|
|
|
|
|
# Add device, volume_id and volume_type parameters separately to maintain backward compatability
|
|
|
|
volume_info = get_volume_info(volume, state)
|
|
|
|
module.exit_json(changed=changed, volume=volume_info, device=volume_info['attachment_set']['device'], volume_id=volume_info['id'], volume_type=volume_info['type'])
|
|
|
|
elif state == 'absent':
|
|
|
|
delete_volume(module, ec2)
|
2013-02-14 15:18:35 +01:00
|
|
|
|
2013-11-01 16:59:24 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
2013-02-14 15:18:35 +01:00
|
|
|
|
2015-12-01 04:02:28 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|