diff --git a/plugins/module_utils/hcloud.py b/plugins/module_utils/hcloud.py index 5a9864d..1174a9b 100644 --- a/plugins/module_utils/hcloud.py +++ b/plugins/module_utils/hcloud.py @@ -22,7 +22,7 @@ from .vendor.hcloud import ( HCloudException, exponential_backoff_function, ) -from .vendor.hcloud.actions import ActionException +from .vendor.hcloud.actions import ActionException, BoundAction from .version import version @@ -36,6 +36,9 @@ class AnsibleHCloud: module: AnsibleModule + client: Client + actions: list[BoundAction] + def __init__(self, module: AnsibleModule): if not self.represent: raise NotImplementedError(f"represent property is not defined for {self.__class__.__name__}") @@ -50,6 +53,9 @@ class AnsibleHCloud: self._build_client() + # Save actions and wait for them using self._wait_actions() + self.actions = [] + def fail_json_hcloud( self, exception: HCloudException, @@ -104,6 +110,14 @@ class AnsibleHCloud: except ClientException as exception: self.module.fail_json(msg=to_native(exception)) + def _wait_actions(self): + """ + Wait for all pending actions and flush the list once completed. + """ + for a in self.actions: + a.wait_until_finished() + self.actions = [] + def _mark_as_changed(self) -> None: self.result["changed"] = True