diff options
author | Ricardo Carrillo Cruz <ricardo.carrillo.cruz@gmail.com> | 2016-11-03 02:21:41 +0100 |
---|---|---|
committer | David Shrewsbury <Shrews@users.noreply.github.com> | 2016-11-02 21:21:41 -0400 |
commit | 872a3822c25db6cbb40e35feae1dae0cd56e49e8 (patch) | |
tree | 4161beb33b46b22a17b531d32af3ecdefcd8a0e3 | |
parent | 375aa92315961a75032d13d02315e94ae6a4377e (diff) | |
download | ansible-modules-core-872a3822c25db6cbb40e35feae1dae0cd56e49e8.tar.gz |
Add rebuild support to os_server_actions (#4289)
Fixes #2714
-rw-r--r-- | cloud/openstack/os_server_actions.py | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/cloud/openstack/os_server_actions.py b/cloud/openstack/os_server_actions.py index 44ff6afc..e298fb9e 100644 --- a/cloud/openstack/os_server_actions.py +++ b/cloud/openstack/os_server_actions.py @@ -35,6 +35,7 @@ author: "Jesse Keating (@j2sol)" description: - Perform server actions on an existing compute instance from OpenStack. This module does not return any data other than changed true/false. + When I(action) is 'rebuild', then I(image) parameter is required. options: server: description: @@ -55,8 +56,14 @@ options: description: - Perform the given action. The lock and unlock actions always return changed as the servers API does not provide lock status. - choices: [stop, start, pause, unpause, lock, unlock, suspend, resume] + choices: [stop, start, pause, unpause, lock, unlock, suspend, resume, + rebuild] default: present + image: + description: + - Image the server should be rebuilt with + default: null + version_added: "2.3" requirements: - "python >= 2.6" - "shade" @@ -82,7 +89,8 @@ _action_map = {'stop': 'SHUTOFF', 'lock': 'ACTIVE', # API doesn't show lock/unlock status 'unlock': 'ACTIVE', 'suspend': 'SUSPENDED', - 'resume': 'ACTIVE',} + 'resume': 'ACTIVE', + 'rebuild': 'ACTIVE'} _admin_actions = ['pause', 'unpause', 'suspend', 'resume', 'lock', 'unlock'] @@ -113,11 +121,15 @@ def main(): argument_spec = openstack_full_argument_spec( server=dict(required=True), action=dict(required=True, choices=['stop', 'start', 'pause', 'unpause', - 'lock', 'unlock', 'suspend', 'resume']), + 'lock', 'unlock', 'suspend', 'resume', + 'rebuild']), + image=dict(required=False), ) module_kwargs = openstack_module_kwargs() - module = AnsibleModule(argument_spec, supports_check_mode=True, **module_kwargs) + module = AnsibleModule(argument_spec, supports_check_mode=True, + required_if=[('action', 'rebuild', ['image'])], + **module_kwargs) if not HAS_SHADE: module.fail_json(msg='shade is required for this module') @@ -125,6 +137,7 @@ def main(): action = module.params['action'] wait = module.params['wait'] timeout = module.params['timeout'] + image = module.params['image'] try: if action in _admin_actions: @@ -203,6 +216,18 @@ def main(): _wait(timeout, cloud, server, action) module.exit_json(changed=True) + elif action == 'rebuild': + image = cloud.get_image(image) + + if image is None: + module.fail_json(msg="Image does not exist") + + # rebuild doesn't set a state, just do it + cloud.nova_client.servers.rebuild(server=server.id, image=image.id) + if wait: + _wait(timeout, cloud, server, action) + module.exit_json(changed=True) + except shade.OpenStackCloudException as e: module.fail_json(msg=str(e), extra_data=e.extra_data) |