gce_pd: add delete_on_termination option (#20201)

Add delete_on_termination option to gce_pd, which will delete the disk
when the attached instance is terminated.
This commit is contained in:
Laurent Goujon 2017-01-18 10:52:23 -08:00 committed by Ryan Brown
commit d3c17d632b

View file

@ -128,6 +128,12 @@ options:
default: "pd-standard" default: "pd-standard"
choices: ["pd-standard", "pd-ssd"] choices: ["pd-standard", "pd-ssd"]
aliases: [] aliases: []
delete_on_termination:
version_added: "2.3"
description:
- If yes, deletes the volume when instance is terminated
default: no
choices: ["yes", "no"]
requirements: requirements:
- "python >= 2.6" - "python >= 2.6"
@ -158,6 +164,7 @@ except ImportError:
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
delete_on_termination = dict(type='bool'),
detach_only = dict(type='bool'), detach_only = dict(type='bool'),
instance_name = dict(), instance_name = dict(),
mode = dict(default='READ_ONLY', choices=['READ_WRITE', 'READ_ONLY']), mode = dict(default='READ_ONLY', choices=['READ_WRITE', 'READ_ONLY']),
@ -179,6 +186,7 @@ def main():
gce = gce_connect(module) gce = gce_connect(module)
delete_on_termination = module.params.get('delete_on_termination')
detach_only = module.params.get('detach_only') detach_only = module.params.get('detach_only')
instance_name = module.params.get('instance_name') instance_name = module.params.get('instance_name')
mode = module.params.get('mode') mode = module.params.get('mode')
@ -190,6 +198,11 @@ def main():
state = module.params.get('state') state = module.params.get('state')
zone = module.params.get('zone') zone = module.params.get('zone')
if delete_on_termination and not instance_name:
module.fail_json(
msg='Must specify an instance name when requesting delete on termination',
changed=False)
if detach_only and not instance_name: if detach_only and not instance_name:
module.fail_json( module.fail_json(
msg='Must specify an instance name when detaching a disk', msg='Must specify an instance name when detaching a disk',
@ -273,11 +286,14 @@ def main():
changed = True changed = True
if inst and not is_attached: if inst and not is_attached:
try: try:
gce.attach_volume(inst, disk, device=name, ex_mode=mode) gce.attach_volume(inst, disk, device=name, ex_mode=mode,
ex_auto_delete=delete_on_termination)
except Exception as e: except Exception as e:
module.fail_json(msg=unexpected_error_msg(e), changed=False) module.fail_json(msg=unexpected_error_msg(e), changed=False)
json_output['attached_to_instance'] = inst.name json_output['attached_to_instance'] = inst.name
json_output['attached_mode'] = mode json_output['attached_mode'] = mode
if delete_on_termination:
json_output['delete_on_termination'] = True
changed = True changed = True
# user wants to delete a disk (or perhaps just detach it). # user wants to delete a disk (or perhaps just detach it).