Add rebuild server functionality (#54899)

This commit is contained in:
Lukas Kämmerling 2019-04-05 21:31:31 +02:00 committed by René Moser
parent 730176db81
commit 22e1f33f5e
2 changed files with 43 additions and 2 deletions

View file

@ -97,7 +97,7 @@ options:
description:
- State of the server.
default: present
choices: [ absent, present, restarted, started, stopped ]
choices: [ absent, present, restarted, started, stopped, rebuild ]
type: str
extends_documentation_fragment: hcloud
"""
@ -146,6 +146,12 @@ EXAMPLES = """
hcloud_server:
name: my-server
state: restarted
- name: Ensure the server is rebuild
hcloud_server:
name: my-server
image: ubuntu-18.04
state: rebuild
"""
RETURN = """
@ -322,6 +328,16 @@ class AnsibleHcloudServer(Hcloud):
self._mark_as_changed()
self._get_server()
def rebuild_server(self):
self.module.fail_on_missing_params(
required_params=["image"]
)
if not self.module.check_mode:
self.client.servers.rebuild(self.hcloud_server, self.client.images.get_by_name(self.module.params.get("image"))).wait_until_finished()
self._mark_as_changed()
self._get_server()
def present_server(self):
self._get_server()
if self.hcloud_server is None:
@ -355,7 +371,7 @@ class AnsibleHcloudServer(Hcloud):
upgrade_disk={"type": "bool", "default": False},
force_upgrade={"type": "bool", "default": False},
state={
"choices": ["absent", "present", "restarted", "started", "stopped"],
"choices": ["absent", "present", "restarted", "started", "stopped", "rebuild"],
"default": "present",
},
**Hcloud.base_module_arguments()
@ -385,6 +401,9 @@ def main():
hcloud.present_server()
hcloud.stop_server()
hcloud.start_server()
elif state == "rebuild":
hcloud.present_server()
hcloud.rebuild_server()
module.exit_json(**hcloud.get_result())

View file

@ -236,6 +236,28 @@
- result is not changed
- result.hcloud_server.backup_window != ""
- name: test rebuild server
hcloud_server:
name: "{{ hcloud_server_name }}"
state: rebuild
register: result_after_test
- name: verify rebuild server
assert:
that:
- result is changed
- result.hcloud_server.id == result_after_test.hcloud_server.id
- name: test rebuild server with check mode
hcloud_server:
name: "{{ hcloud_server_name }}"
state: rebuild
register: result_after_test
check_mode: true
- name: verify rebuild server with check mode
assert:
that:
- result is changed
- name: absent server
hcloud_server:
name: "{{ hcloud_server_name }}"