From 22e1f33f5e6be3cfb900e52a20454de11192aaa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20K=C3=A4mmerling?= <4281581+LKaemmerling@users.noreply.github.com> Date: Fri, 5 Apr 2019 21:31:31 +0200 Subject: [PATCH] Add rebuild server functionality (#54899) --- .../modules/cloud/hcloud/hcloud_server.py | 23 +++++++++++++++++-- .../targets/hcloud_server/tasks/main.yml | 22 ++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/cloud/hcloud/hcloud_server.py b/lib/ansible/modules/cloud/hcloud/hcloud_server.py index 02df1beb5b..f769bb3a73 100644 --- a/lib/ansible/modules/cloud/hcloud/hcloud_server.py +++ b/lib/ansible/modules/cloud/hcloud/hcloud_server.py @@ -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()) diff --git a/test/integration/targets/hcloud_server/tasks/main.yml b/test/integration/targets/hcloud_server/tasks/main.yml index 1d5d6011ec..ce0524d971 100644 --- a/test/integration/targets/hcloud_server/tasks/main.yml +++ b/test/integration/targets/hcloud_server/tasks/main.yml @@ -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 }}"