Recognize when volume attach/detach is not necessary
Do not attempt to attach an already attached volume. Likewise, do not attempt to detach a volume that is not attached. This version adds support for check mode.
This commit is contained in:
parent
f6cf1411a1
commit
4a233a5a8f
1 changed files with 29 additions and 8 deletions
|
@ -36,8 +36,8 @@ description:
|
||||||
options:
|
options:
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Indicate desired state of the resource
|
- Should the resource be present or absent.
|
||||||
choices: ['present', 'absent']
|
choices: [present, absent]
|
||||||
default: present
|
default: present
|
||||||
server:
|
server:
|
||||||
description:
|
description:
|
||||||
|
@ -53,11 +53,6 @@ options:
|
||||||
- Device you want to attach. Defaults to auto finding a device name.
|
- Device you want to attach. Defaults to auto finding a device name.
|
||||||
required: false
|
required: false
|
||||||
default: None
|
default: None
|
||||||
state:
|
|
||||||
description:
|
|
||||||
- Should the resource be present or absent.
|
|
||||||
choices: [present, absent]
|
|
||||||
default: present
|
|
||||||
requirements: ["shade"]
|
requirements: ["shade"]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -76,6 +71,18 @@ EXAMPLES = '''
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def _system_state_change(state, device):
|
||||||
|
"""Check if system state would change."""
|
||||||
|
if state == 'present':
|
||||||
|
if device:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
if state == 'absent':
|
||||||
|
if device:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = openstack_full_argument_spec(
|
argument_spec = openstack_full_argument_spec(
|
||||||
server=dict(required=True),
|
server=dict(required=True),
|
||||||
|
@ -85,7 +92,9 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
module_kwargs = openstack_module_kwargs()
|
module_kwargs = openstack_module_kwargs()
|
||||||
module = AnsibleModule(argument_spec, **module_kwargs)
|
module = AnsibleModule(argument_spec,
|
||||||
|
supports_check_mode=True,
|
||||||
|
**module_kwargs)
|
||||||
|
|
||||||
if not HAS_SHADE:
|
if not HAS_SHADE:
|
||||||
module.fail_json(msg='shade is required for this module')
|
module.fail_json(msg='shade is required for this module')
|
||||||
|
@ -98,8 +107,16 @@ def main():
|
||||||
cloud = shade.openstack_cloud(**module.params)
|
cloud = shade.openstack_cloud(**module.params)
|
||||||
server = cloud.get_server(module.params['server'])
|
server = cloud.get_server(module.params['server'])
|
||||||
volume = cloud.get_volume(module.params['volume'])
|
volume = cloud.get_volume(module.params['volume'])
|
||||||
|
dev = cloud.get_volume_attach_device(volume, server.id)
|
||||||
|
|
||||||
|
if module.check_mode:
|
||||||
|
module.exit_json(changed=_system_state_change(state, dev))
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
|
if dev:
|
||||||
|
# Volume is already attached to this server
|
||||||
|
module.exit_json(changed=False)
|
||||||
|
|
||||||
cloud.attach_volume(server, volume, module.params['device'],
|
cloud.attach_volume(server, volume, module.params['device'],
|
||||||
wait=wait, timeout=timeout)
|
wait=wait, timeout=timeout)
|
||||||
|
|
||||||
|
@ -115,6 +132,10 @@ def main():
|
||||||
)
|
)
|
||||||
|
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
|
if not dev:
|
||||||
|
# Volume is not attached to this server
|
||||||
|
module.exit_json(changed=False)
|
||||||
|
|
||||||
cloud.detach_volume(server, volume, wait=wait, timeout=timeout)
|
cloud.detach_volume(server, volume, wait=wait, timeout=timeout)
|
||||||
module.exit_json(
|
module.exit_json(
|
||||||
changed=True,
|
changed=True,
|
||||||
|
|
Loading…
Reference in a new issue