2
# This file is part of Ansible
4
# Ansible is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
9
# Ansible is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
20
short_description: create and attach a volume, return volume id and device map
22
- 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.
27
- AWS secret key. If not set then the value of the AWS_SECRET_KEY environment variable is used.
30
aliases: ['ec2_secret_key', 'secret_key' ]
33
- AWS access key. If not set then the value of the AWS_ACCESS_KEY environment variable is used.
36
aliases: ['ec2_access_key', 'access_key' ]
39
- 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
45
- instance ID if you wish to attach the volume.
51
- size of volume (in GB) to create.
57
- the provisioned IOPs you want to associate with this volume (integer).
64
- device id to override device mapping. Assumes /dev/sdf for Linux/UNIX and /dev/xvdf for Windows.
70
- The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used.
73
aliases: ['aws_region', 'ec2_region']
76
- zone in which to create the volume, if unset uses the zone the instance is in (if set)
79
aliases: ['aws_zone', 'ec2_zone']
80
requirements: [ "boto" ]
85
# Simple attachment action
92
# Example using custom iops params
100
# Playbook example combined with instance launch
103
keypair: "{{ keypair }}"
110
instance: "{{ item.id }} "
112
with_items: ec2.instances
116
# Note: this module needs to be made idempotent. Possible solution is to use resource tags with the volumes.
117
# if state=present and it doesn't exist, create, tag and attach.
118
# Check for state by looking for volume attachment with tag (and against block device mapping?).
119
# Would personally like to revisit this in May when Eucalyptus also has tagging support (3.3).
127
print "failed=True msg='boto required for this module'"
130
AWS_REGIONS = ['ap-northeast-1',
140
module = AnsibleModule(
141
argument_spec = dict(
143
volume_size = dict(required=True),
145
device_name = dict(),
146
region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS),
147
zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']),
149
aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True),
150
aws_access_key = dict(aliases=['ec2_access_key', 'access_key']),
154
instance = module.params.get('instance')
155
volume_size = module.params.get('volume_size')
156
iops = module.params.get('iops')
157
device_name = module.params.get('device_name')
158
region = module.params.get('region')
159
zone = module.params.get('zone')
160
ec2_url = module.params.get('ec2_url')
161
aws_secret_key = module.params.get('aws_secret_key')
162
aws_access_key = module.params.get('aws_access_key')
164
# allow eucarc environment variables to be used if ansible vars aren't set
165
if not ec2_url and 'EC2_URL' in os.environ:
166
ec2_url = os.environ['EC2_URL']
168
if not aws_secret_key:
169
if 'AWS_SECRET_KEY' in os.environ:
170
aws_secret_key = os.environ['AWS_SECRET_KEY']
171
elif 'EC2_SECRET_KEY' in os.environ:
172
aws_secret_key = os.environ['EC2_SECRET_KEY']
174
if not aws_access_key:
175
if 'AWS_ACCESS_KEY' in os.environ:
176
aws_access_key = os.environ['AWS_ACCESS_KEY']
177
elif 'EC2_ACCESS_KEY' in os.environ:
178
aws_access_key = os.environ['EC2_ACCESS_KEY']
181
if 'AWS_REGION' in os.environ:
182
region = os.environ['AWS_REGION']
183
elif 'EC2_REGION' in os.environ:
184
region = os.environ['EC2_REGION']
186
# If we have a region specified, connect to its endpoint.
189
ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)
190
except boto.exception.NoAuthHandlerFound, e:
191
module.fail_json(msg = str(e))
192
# Otherwise, no region so we fallback to the old connection method
195
ec2 = boto.connect_ec2_endpoint(ec2_url, aws_access_key, aws_secret_key)
196
except boto.exception.NoAuthHandlerFound, e:
197
module.fail_json(msg = str(e))
199
module.fail_json(msg="Either region or ec2_url must be specified")
201
# Here we need to get the zone info for the instance. This covers situation where
202
# instance is specified but zone isn't.
203
# Useful for playbooks chaining instance launch with volume create + attach and where the
204
# zone doesn't matter to the user.
207
reservation = ec2.get_all_instances(instance_ids=instance)
208
inst = reservation[0].instances[0]
209
zone = inst.placement
211
# Check if there is a volume already mounted there.
213
if device_name in inst.block_device_mapping:
214
module.exit_json(msg="Volume mapping for %s already exists on instance %s" % (device_name, instance),
218
# If custom iops is defined we use volume_type "io1" rather than the default of "standard"
223
volume_type = 'standard'
225
# If no instance supplied, try volume creation based on module parameters.
228
volume = ec2.create_volume(volume_size, zone, None, volume_type, iops)
229
while volume.status != 'available':
232
except boto.exception.BotoServerError, e:
233
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
235
# Attach the created volume.
237
if device_name and instance:
239
attach = volume.attach(inst.id, device_name)
240
while volume.attachment_state() != 'attached':
243
except boto.exception.BotoServerError, e:
244
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
246
# If device_name isn't set, make a choice based on best practices here:
247
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
249
# In future this needs to be more dynamic but combining block device mapping best practices
250
# (bounds for devices, as above) with instance.block_device_mapping data would be tricky. For me ;)
252
# Use password data attribute to tell whether the instance is Windows or Linux
254
if device_name is None and instance:
256
if not ec2.get_password_data(inst.id):
257
device_name = '/dev/sdf'
258
attach = volume.attach(inst.id, device_name)
259
while volume.attachment_state() != 'attached':
263
device_name = '/dev/xvdf'
264
attach = volume.attach(inst.id, device_name)
265
while volume.attachment_state() != 'attached':
268
except boto.exception.BotoServerError, e:
269
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
272
"volume_id": volume.id,
273
"device": device_name
277
# this is magic, see lib/ansible/module_common.py
278
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>