Added the ability to remove snapshots

Added state option, and accompanying snapshot_id option for
when state=absent.
pull/18777/head
willthames 11 years ago committed by Matt Clay
parent f875cc5197
commit ef88ff6dd8

@ -62,6 +62,19 @@ options:
required: false required: false
default: 0 default: 0
version_added: "1.5.1" version_added: "1.5.1"
state:
description:
- whether to add or create a snapshot
required: false
default: present
choices: ['absent', 'present']
version_added: "1.9"
snapshot_id:
description:
- snapshot id to remove
required: false
version_added: "1.9"
author: Will Thames author: Will Thames
extends_documentation_fragment: aws extends_documentation_fragment: aws
''' '''
@ -85,6 +98,12 @@ EXAMPLES = '''
snapshot_tags: snapshot_tags:
frequency: hourly frequency: hourly
source: /data source: /data
# Remove a snapshot
- local_action:
module: ec2_snapshot
snapshot_id: snap-abcd1234
state: absent
''' '''
import sys import sys
@ -103,24 +122,28 @@ def main():
volume_id = dict(), volume_id = dict(),
description = dict(), description = dict(),
instance_id = dict(), instance_id = dict(),
snapshot_id = dict(),
device_name = dict(), device_name = dict(),
wait = dict(type='bool', default='true'), wait = dict(type='bool', default='true'),
wait_timeout = dict(default=0), wait_timeout = dict(default=0),
snapshot_tags = dict(type='dict', default=dict()), snapshot_tags = dict(type='dict', default=dict()),
state = dict(choices=['absent','present'], default='present'),
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
volume_id = module.params.get('volume_id') volume_id = module.params.get('volume_id')
snapshot_id = module.params.get('snapshot_id')
description = module.params.get('description') description = module.params.get('description')
instance_id = module.params.get('instance_id') instance_id = module.params.get('instance_id')
device_name = module.params.get('device_name') device_name = module.params.get('device_name')
wait = module.params.get('wait') wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout') wait_timeout = module.params.get('wait_timeout')
snapshot_tags = module.params.get('snapshot_tags') snapshot_tags = module.params.get('snapshot_tags')
state = module.params.get('state')
if not volume_id and not instance_id or volume_id and instance_id: if not volume_id and not instance_id and not snapshot_id or volume_id and instance_id and snapshot_id:
module.fail_json('One and only one of volume_id or instance_id must be specified') module.fail_json('One and only one of volume_id or instance_id or snapshot_id must be specified')
if instance_id and not device_name or device_name and not instance_id: if instance_id and not device_name or device_name and not instance_id:
module.fail_json('Instance ID and device name must both be specified') module.fail_json('Instance ID and device name must both be specified')
@ -135,6 +158,20 @@ def main():
except boto.exception.BotoServerError, e: except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message)) module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
if state == 'absent':
if not snapshot_id:
module.fail_json(msg = 'snapshot_id must be set when state is absent')
try:
snapshots = ec2.get_all_snapshots([snapshot_id])
ec2.delete_snapshot(snapshot_id)
module.exit_json(changed=True)
except boto.exception.BotoServerError, e:
# exception is raised if snapshot does not exist
if e.error_code == 'InvalidSnapshot.NotFound':
module.exit_json(changed=False)
else:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
try: try:
snapshot = ec2.create_snapshot(volume_id, description=description) snapshot = ec2.create_snapshot(volume_id, description=description)
time_waited = 0 time_waited = 0

Loading…
Cancel
Save