fix: only rebuild existing servers, skip rebuild if it was just created (#581)

##### SUMMARY

Fixes #580

A freshly created server must not be rebuild.

##### ISSUE TYPE

- Bugfix Pull Request


##### COMPONENT NAME

server
This commit is contained in:
Jonas L. 2024-11-11 17:09:37 +01:00 committed by GitHub
parent 5221828cba
commit 06718d0db0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 12 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- hcloud_server - Only rebuild existing servers, skip rebuild if the server was just created.

View file

@ -869,18 +869,25 @@ class AnsibleHCloudServer(AnsibleHCloud):
return None
def rebuild_server(self):
self.module.fail_on_missing_params(required_params=["image"])
try:
if not self.module.check_mode:
image = self._get_image(self.hcloud_server.server_type)
resp = self.client.servers.rebuild(self.hcloud_server, image)
# When we rebuild the server progress takes some more time.
resp.action.wait_until_finished(max_retries=202) # 202 retries >= 1002 seconds
self._mark_as_changed()
self._get_server()
if self.hcloud_server is None:
self._create_server()
else:
self._update_server()
self._get_server()
except HCloudException as exception:
self.fail_json_hcloud(exception)
# Only rebuild the server if it already existed.
self.module.fail_on_missing_params(required_params=["image"])
try:
if not self.module.check_mode:
image = self._get_image(self.hcloud_server.server_type)
resp = self.client.servers.rebuild(self.hcloud_server, image)
# When we rebuild the server progress takes some more time.
resp.action.wait_until_finished(max_retries=202) # 202 retries >= 1002 seconds
self._mark_as_changed()
self._get_server()
except HCloudException as exception:
self.fail_json_hcloud(exception)
def present_server(self):
self._get_server()
@ -969,7 +976,6 @@ def main():
hcloud.stop_server()
hcloud.start_server()
elif state == "rebuild":
hcloud.present_server()
hcloud.rebuild_server()
module.exit_json(**hcloud.get_result())