From b2978de8b13b1a00bee63f1c5974f36b6d69c017 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 19:26:37 +0100 Subject: [PATCH 01/76] init: draft --- plugins/modules/host.py | 542 +++++++++++++++++++++++++--------------- 1 file changed, 345 insertions(+), 197 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 1be564dd0..8dda18d18 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -36,26 +36,35 @@ B(Attention! This option OVERWRITES all existing attributes!) If you are using custom tags, make sure to prepend the attribute with C(tag_). type: raw - default: {} + required: false update_attributes: description: - The update_attributes of your host as described in the API documentation. This will only update the given attributes. If you are using custom tags, make sure to prepend the attribute with C(tag_). type: raw - default: {} + required: false remove_attributes: description: - The remove_attributes of your host as described in the API documentation. + B(If a list of strings is supplied, the listed attributes are removed.) + B(If extended_functionality and a dict is supplied, the attributes that exactly match + the passed attributes are removed.) This will only remove the given attributes. If you are using custom tags, make sure to prepend the attribute with C(tag_). + As of Check MK v2.2.0p7 and v2.3.0b1, simultaneous use of I(attributes), + I(remove_attributes), and I(update_attributes) is no longer supported. type: raw - default: [] + required: false state: description: The state of your host. type: str default: present choices: [present, absent] + extended_functionality: + description: Allow extended functionality instead of the expected REST API behavior. + type: bool + default: true author: - Robin Gierse (@robin-checkmk) @@ -163,149 +172,350 @@ import json from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.dict_transformations import dict_merge +from ansible.module_utils.common.dict_transformations import dict_merge, recursive_diff from ansible.module_utils.common.validation import check_type_list -from ansible.module_utils.urls import fetch_url +from ansible_collections.checkmk.general.plugins.module_utils.api import CheckmkAPI +from ansible_collections.checkmk.general.plugins.module_utils.types import RESULT +from ansible_collections.checkmk.general.plugins.module_utils.utils import ( + result_as_dict, +) +from ansible_collections.checkmk.general.plugins.module_utils.version import ( + CheckmkVersion, +) + +HOST = ( + "customer", + "attributes", + "update_attributes", + "remove_attributes", +) + +HOST_PARENTS_PARSE = ( + "attributes", + "update_attributes", +) + + +class HostHTTPCodes: + # http_code: (changed, failed, "Message") + get = { + 200: (False, False, "Host found, nothing changed"), + 404: (False, False, "Host not found"), + } + + create = {200: (True, False, "Host created")} + edit = {200: (True, False, "Host modified")} + edit = {200: (True, False, "Host moved")} + delete = {204: (True, False, "Host deleted")} + + +class HostEndpoints: + default = "/objects/host_config" + create = "/domain-types/host_config/collections/all" + + +class HostAPI(CheckmkAPI): + def __init__(self, module): + super().__init__(module) + + self.extended_functionality = self.params.get("extended_functionality", True) + self.desired = {} -def exit_failed(module, msg): - result = {"msg": msg, "changed": False, "failed": True} - module.fail_json(**result) + self.desired["folder"] = self.params.get("folder", "/") + self.desired["host_name"] = self.params.get("name") -def exit_changed(module, msg): - result = {"msg": msg, "changed": True, "failed": False} - module.exit_json(**result) + for key in HOST: + if self.params.get(key): + self.desired[key] = self.params.get(key) + for key in HOST_PARENTS_PARSE: + if self.desired.get(key): + if self.desired.get(key).get("parents"): + self.desired[key]["parents"] = check_type_list( + self.desired.get(key).get("parents") + ) -def exit_ok(module, msg): - result = {"msg": msg, "changed": False, "failed": False} - module.exit_json(**result) + # Get the current host from the API and set some parameters + self._get_current() + self._changed_items = self._detect_changes() + self._verify_compatibility() -def get_current_host_state(module, base_url, headers): - current_state = "unknown" - current_explicit_attributes = {} - current_folder = "/" - etag = "" + def _verify_compatibility(self): + # Check if parameters are compatible with CMK version + if ( + sum( + [ + 1 + for el in ["attributes", "remove_attributes", "update_attributes"] + if self.module.params.get(el) + ] + ) + > 1 + ): + + ver = self.getversion() + msg = ( + "As of Check MK v2.2.0p7 and v2.3.0b1, simultaneous use of" + " attributes, remove_attributes, and update_attributes is no longer supported." + ) - api_endpoint = "/objects/host_config/" + module.params.get("name") - parameters = "?effective_attributes=true" - url = base_url + api_endpoint + parameters + if ver >= CheckmkVersion("2.2.0p7"): + result = RESULT( + http_code=0, + msg=msg, + content="", + etag="", + failed=True, + changed=False, + ) + self.module.exit_json(**result_as_dict(result)) + else: + self.module.warn(msg) - response, info = fetch_url(module, url, data=None, headers=headers, method="GET") + def _normalize_folder(self, folder): + if folder in ["", " ", "/", "//"]: + return "/" - if info["status"] == 200: - body = json.loads(response.read()) - current_state = "present" - etag = info.get("etag", "") - extensions = body.get("extensions", {}) - current_explicit_attributes = extensions.get("attributes", {}) - current_folder = "%s" % extensions.get("folder", "/") - if "meta_data" in current_explicit_attributes: - del current_explicit_attributes["meta_data"] + if not folder.startswith("/"): + folder = "/%s" % folder - elif info["status"] == 404: - current_state = "absent" + if folder.endswith("/"): + folder = folder.rstrip("/") - else: - exit_failed( - module, - "Error calling API. HTTP code %d. Details: %s." - % (info["status"], info.get("body", "N/A")), + return folder + + def _build_default_endpoint(self): + return "%s/%s" % ( + HostEndpoints.default, + self.desired["host_name"], ) - return current_state, current_explicit_attributes, current_folder, etag + def _build_move_endpoint(self): + return "%s/%s/actions/move/invoke" % ( + HostEndpoints.default, + self.desired["host_name"], + ) + ### + def _detect_changes(self): + current_attributes = self.current.get("attributes", {}) + desired_attributes = self.desired.copy() + changes = [] -def set_host_attributes(module, attributes, base_url, headers, update_method): - api_endpoint = "/objects/host_config/" + module.params.get("name") - params = { - update_method: attributes, - } - url = base_url + api_endpoint + if desired_attributes.get("update_attributes"): + merged_attributes = dict_merge( + current_attributes, desired_attributes.get("update_attributes") + ) - response, info = fetch_url( - module, url, module.jsonify(params), headers=headers, method="PUT" - ) + if merged_attributes != current_attributes: + try: + (c_m, m_c) = recursive_diff(current_attributes, merged_attributes) + changes.append("update attributes: %s" % json.dumps(m_c)) + except Exception as e: + changes.append("update attributes") + desired_attributes["update_attributes"] = merged_attributes + + if desired_attributes.get( + "attributes" + ) and current_attributes != desired_attributes.get("attributes"): + changes.append("attributes") + + if self.current.get("folder") != desired_attributes.get("folder"): + changes.append("folder") + + if desired_attributes.get("remove_attributes"): + tmp_remove_attributes = desired_attributes.get("remove_attributes") + + if isinstance(tmp_remove_attributes, list): + removes_which = [ + a for a in tmp_remove_attributes if current_attributes.get(a) + ] + if len(removes_which) > 0: + changes.append("remove attributes: %s" % " ".join(removes_which)) + elif isinstance(tmp_remove_attributes, dict): + if not self.extended_functionality: + self.module.fail_json( + msg="ERROR: The parameter remove_attributes of dict type is not supported for the paramter extended_functionality: false!", + ) + + (tmp_remove, tmp_rest) = (current_attributes, {}) + if current_attributes != tmp_remove_attributes: + try: + (c_m, m_c) = recursive_diff( + current_attributes, tmp_remove_attributes + ) + + if c_m: + # if nothing to remove + if current_attributes == c_m: + (tmp_remove, tmp_rest) = ({}, current_attributes) + else: + (c_c_m, c_m_c) = recursive_diff(current_attributes, c_m) + (tmp_remove, tmp_rest) = (c_c_m, c_m) + except Exception as e: + self.module.fail_json( + msg="ERROR: incompatible parameter: remove_attributes!", + exception=e, + ) + + desired_attributes.pop("remove_attributes") + if tmp_remove != {}: + changes.append("remove attributes: %s" % json.dumps(tmp_remove)) + if tmp_rest != {}: + desired_attributes["update_attributes"] = tmp_rest + else: + self.module.fail_json( + msg="ERROR: The parameter remove_attributes can be a list of strings or a dictionary!", + exception=e, + ) + + if self.extended_functionality: + self.desired = desired_attributes.copy() - if info["status"] == 400 and update_method == "remove_attributes": - return "Host attributes allready removed." - elif info["status"] != 200: - exit_failed( - module, - "Error calling API. HTTP code %d. Details: %s, " - % (info["status"], info["body"]), + # self.module.fail_json(json.dumps(desired_attributes)) + + return changes + + ### + def _get_current(self): + result = self._fetch( + code_mapping=HostHTTPCodes.get, + endpoint=self._build_default_endpoint(), + method="GET", ) + if result.http_code == 200: + self.state = "present" -def move_host(module, base_url, headers): - api_endpoint = "/objects/host_config/%s/actions/move/invoke" % module.params.get( - "name" - ) - params = { - "target_folder": module.params.get("folder", "/"), - } - url = base_url + api_endpoint + content = json.loads(result.content) - response, info = fetch_url( - module, url, module.jsonify(params), headers=headers, method="POST" - ) + extensions = content["extensions"] + self.current["folder"] = extentions.pop("folder", "/") + for key, value in extensions.items(): + if key == "attributes": + value.pop("meta_data") + if "network_scan_results" in value: + value.pop("network_scan_results") + self.current[key] = value - if info["status"] != 200: - exit_failed( - module, - "Error calling API. HTTP code %d. Details: %s, " - % (info["status"], info["body"]), + self.etag = result.etag + + else: + self.state = "absent" + + def _check_output(self, mode): + return RESULT( + http_code=0, + msg="Running in check mode. Would have done an %s" % mode, + content="", + etag="", + failed=False, + changed=False, ) + ### + def needs_update(self): + return len(self._changed_items) > 0 -def create_host(module, attributes, base_url, headers): - api_endpoint = "/domain-types/host_config/collections/all" - params = { - "folder": module.params.get("folder", "/"), - "host_name": module.params.get("name"), - "attributes": attributes, - } - url = base_url + api_endpoint + ### + def create(self): + data = self.desired.copy() + if data.get("attributes", {}) == {}: + data["attributes"] = data.pop("update_attributes", {}) - response, info = fetch_url( - module, url, module.jsonify(params), headers=headers, method="POST" - ) + if data.get("remove_attributes"): + data.pop("remove_attributes") + + if self.module.check_mode: + return self._check_output("create") - if info["status"] != 200: - exit_failed( - module, - "Error calling API. HTTP code %d. Details: %s, " - % (info["status"], info["body"]), + result = self._fetch( + code_mapping=HostHTTPCodes.create, + endpoint=HostEndpoints.create, + data=data, + method="POST", ) + return result -def delete_host(module, base_url, headers): - api_endpoint = "/objects/host_config/" + module.params.get("name") - url = base_url + api_endpoint + def edit(self): + data = self.desired.copy() + data.pop("host_name") - response, info = fetch_url(module, url, data=None, headers=headers, method="DELETE") + tmp = {} + tmp["target_folder"] = data.pop("folder", "/") + self.headers["if-Match"] = self.etag - if info["status"] != 204: - exit_failed( - module, - "Error calling API. HTTP code %d. Details: %s, " - % (info["status"], info["body"]), + if self.module.check_mode: + return self._check_output("edit") + + result = self._fetch( + code_mapping=HostHTTPCodes.move, + endpoint=self._build_move_endpoint(), + data=tmp, + method="POST", + ) + + result._replace( + msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") ) + if self.module.check_mode: + return self._check_output("edit") + + if data.get("update_attributes") == {}: + data.pop("update_attributes") + + if data.get("remove_attributes") == []: + data.pop("remove_attributes") + + if data.get("update_attributes") and data.get("remove_attributes"): + tmp = data.copy() + tmp.pop("remove_attributes") + self.module.fail_json(json.dumps(tmp)) + result = self._fetch( + code_mapping=HostHTTPCodes.edit, + endpoint=self._build_default_endpoint(), + data=tmp, + method="PUT", + ) + + tmp = data.copy() + tmp.pop("update_attributes") + result = self._fetch( + code_mapping=HostHTTPCodes.edit, + endpoint=self._build_default_endpoint(), + data=tmp, + method="PUT", + ) + else: + data["update_method"] = data.pop("update_attributes") -def normalize_folder(folder): - if folder in ["", " ", "/", "//"]: - return "/" + result = self._fetch( + code_mapping=FolderHTTPCodes.edit, + endpoint=self._build_default_endpoint(), + data=data, + method="PUT", + ) - if not folder.startswith("/"): - folder = "/%s" % folder + return result._replace( + msg=result.msg + ". Changed: %s" % ", ".join(self._changed_items) + ) + + def delete(self): + if self.module.check_mode: + return self._check_output("delete") - if folder.endswith("/"): - folder = folder.rstrip("/") + result = self._fetch( + code_mapping=HostHTTPCodes.delete, + endpoint=self._build_default_endpoint(), + method="DELETE", + ) - return folder + return result def run_module(): @@ -320,106 +530,44 @@ def run_module(): type="str", required=True, ), - attributes=dict(type="raw", default={}), - remove_attributes=dict(type="raw", default=[]), - update_attributes=dict(type="raw", default={}), + attributes=dict(type="raw", required=False), + remove_attributes=dict(type="raw", required=False), + update_attributes=dict(type="raw", required=False), folder=dict(type="str", required=False), state=dict(type="str", default="present", choices=["present", "absent"]), + extended_functionality=dict(type="bool", required=False, default=True), ) - module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) + module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) - # Use the parameters to initialize some common variables - headers = { - "Accept": "application/json", - "Content-Type": "application/json", - "Authorization": "Bearer %s %s" - % ( - module.params.get("automation_user", ""), - module.params.get("automation_secret", ""), - ), - } + # Create an API object that contains the current and desired state + current_host = HostAPI(module) - base_url = "%s/%s/check_mk/api/1.0" % ( - module.params.get("server_url", ""), - module.params.get("site", ""), + result = RESULT( + http_code=0, + msg="No changes needed.", + content="", + etag="", + failed=False, + changed=False, ) - # Determine desired state and attributes - attributes = module.params.get("attributes", {}) - remove_attributes = module.params.get("remove_attributes", []) - update_attributes = module.params.get("update_attributes", {}) - state = module.params.get("state", "present") - - for par in [attributes, update_attributes]: - if par != {}: - if par.get("parents"): - par["parents"] = check_type_list(par.get("parents")) - - if module.params["folder"]: - module.params["folder"] = normalize_folder(module.params["folder"]) - - # Determine the current state of this particular host - ( - current_state, - current_explicit_attributes, - current_folder, - etag, - ) = get_current_host_state(module, base_url, headers) - - # Handle the host accordingly to above findings and desired state - if state == "present" and current_state == "present": - headers["If-Match"] = etag - msg_tokens = [] - - current_folder = normalize_folder(current_folder) - merged_attributes = dict_merge(current_explicit_attributes, update_attributes) - - if module.params["folder"] and current_folder != module.params["folder"]: - move_host(module, base_url, headers) - msg_tokens.append("Host was moved.") - - if attributes != {} and current_explicit_attributes != attributes: - set_host_attributes(module, attributes, base_url, headers, "attributes") - msg_tokens.append("Host attributes replaced.") - - if update_attributes != {} and current_explicit_attributes != merged_attributes: - set_host_attributes( - module, merged_attributes, base_url, headers, "attributes" - ) - msg_tokens.append("Host attributes updated.") - - if remove_attributes != []: - msg = set_host_attributes( - module, remove_attributes, base_url, headers, "remove_attributes" - ) - if msg == "Host attributes allready removed.": - exit_ok(module, msg) - else: - msg_tokens.append("Host attributes removed.") - - if len(msg_tokens) >= 1: - exit_changed(module, " ".join(msg_tokens)) - else: - exit_ok(module, "Host already present. All explicit attributes as desired.") - - elif state == "present" and current_state == "absent": - if update_attributes != {} and attributes == {}: - attributes = update_attributes - if not module.params["folder"]: - module.params["folder"] = "/" - create_host(module, attributes, base_url, headers) - exit_changed(module, "Host created.") - - elif state == "absent" and current_state == "absent": - exit_ok(module, "Host already absent.") - - elif state == "absent" and current_state == "present": - delete_host(module, base_url, headers) - exit_changed(module, "Host deleted.") - - else: - exit_failed(module, "Unknown error") + desired_state = current_host.params.get("state") + if current_host.state == "present": + result = result._replace( + msg="Host already exists with the desired parameters." + ) + if desired_state == "absent": + result = current_host.delete() + elif current_host.needs_update(): + move + result = current_host.edit() + elif current_host.state == "absent": + result = result._replace(msg="Folder already absent.") + if desired_state in ("present"): + result = current_host.create() + + module.exit_json(**result_as_dict(result)) def main(): From 6e4b799f125bcfd262663c1c41bb1ebd08c4f998 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 19:32:30 +0100 Subject: [PATCH 02/76] fix: --- plugins/modules/host.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 8dda18d18..c25543525 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -393,7 +393,7 @@ def _get_current(self): content = json.loads(result.content) extensions = content["extensions"] - self.current["folder"] = extentions.pop("folder", "/") + self.current["folder"] = extensions.pop("folder", "/") for key, value in extensions.items(): if key == "attributes": value.pop("meta_data") @@ -459,9 +459,7 @@ def edit(self): method="POST", ) - result._replace( - msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") - ) + result._replace(msg=result.msg + ". Moved to: %s" % tmp.get("target_folder")) if self.module.check_mode: return self._check_output("edit") @@ -495,7 +493,7 @@ def edit(self): data["update_method"] = data.pop("update_attributes") result = self._fetch( - code_mapping=FolderHTTPCodes.edit, + code_mapping=HostHTTPCodes.edit, endpoint=self._build_default_endpoint(), data=data, method="PUT", @@ -554,13 +552,10 @@ def run_module(): desired_state = current_host.params.get("state") if current_host.state == "present": - result = result._replace( - msg="Host already exists with the desired parameters." - ) + result = result._replace(msg="Host already exists with the desired parameters.") if desired_state == "absent": result = current_host.delete() elif current_host.needs_update(): - move result = current_host.edit() elif current_host.state == "absent": result = result._replace(msg="Folder already absent.") From d113f6ddcb661cc7b06b8eaa79c3938c12bfd6ea Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 19:48:15 +0100 Subject: [PATCH 03/76] fix: --- plugins/modules/host.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index c25543525..3da6380f0 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -393,7 +393,7 @@ def _get_current(self): content = json.loads(result.content) extensions = content["extensions"] - self.current["folder"] = extensions.pop("folder", "/") + self.current["folder"] = extensions.get("folder", "/") for key, value in extensions.items(): if key == "attributes": value.pop("meta_data") @@ -446,7 +446,7 @@ def edit(self): data.pop("host_name") tmp = {} - tmp["target_folder"] = data.pop("folder", "/") + tmp["target_folder"] = data.get("folder", "/") self.headers["if-Match"] = self.etag if self.module.check_mode: From 19dbad02ad854ff5d0139b46c5968175fec891f3 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 20:00:21 +0100 Subject: [PATCH 04/76] fix: --- plugins/modules/host.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 3da6380f0..aa7504b93 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -222,8 +222,6 @@ def __init__(self, module): self.desired = {} - self.desired["folder"] = self.params.get("folder", "/") - self.desired["host_name"] = self.params.get("name") for key in HOST: @@ -239,6 +237,7 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() + self.desired["folder"] = self.params.get("folder", self.desired.get("folder", "/")) self._changed_items = self._detect_changes() self._verify_compatibility() From b47ba1032ddef93d83babe4accfe7bbabf315966 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 20:01:26 +0100 Subject: [PATCH 05/76] fix: style --- plugins/modules/host.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index aa7504b93..1691da299 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -237,7 +237,9 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - self.desired["folder"] = self.params.get("folder", self.desired.get("folder", "/")) + self.desired["folder"] = self.params.get( + "folder", self.desired.get("folder", "/") + ) self._changed_items = self._detect_changes() self._verify_compatibility() From 4ace5cd9a5bc90a1ff92bba15f46ad8e5bc96624 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 20:15:11 +0100 Subject: [PATCH 06/76] fix: folder --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 1691da299..4992b0908 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -238,7 +238,7 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() self.desired["folder"] = self.params.get( - "folder", self.desired.get("folder", "/") + "folder", self.current.get("folder", "/") ) self._changed_items = self._detect_changes() From 1762edbab4f13ad0cbfab9b73d2f739b644595dd Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 20:48:42 +0100 Subject: [PATCH 07/76] fix: folder --- plugins/modules/host.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 4992b0908..a94ca9a3f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -184,7 +184,6 @@ ) HOST = ( - "customer", "attributes", "update_attributes", "remove_attributes", @@ -237,9 +236,11 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - self.desired["folder"] = self.params.get( - "folder", self.current.get("folder", "/") - ) + tmp_folder = self.params.get("folder") + if not tmp_folder: + tmp_folder = self.current.get("folder", "/") + self.desired["folder"] = tmp_folder + module.fail_json(json.dumps(self.desired)) self._changed_items = self._detect_changes() self._verify_compatibility() From fb805a4515d3113c5f26ba6d3fda23eced8b6b39 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:11:16 +0100 Subject: [PATCH 08/76] rempove: printout --- plugins/modules/host.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index a94ca9a3f..8f74b6eca 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -240,7 +240,6 @@ def __init__(self, module): if not tmp_folder: tmp_folder = self.current.get("folder", "/") self.desired["folder"] = tmp_folder - module.fail_json(json.dumps(self.desired)) self._changed_items = self._detect_changes() self._verify_compatibility() From f4426e8cdcbb3c0ec172de7a9eccd2e9a451ba25 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:16:29 +0100 Subject: [PATCH 09/76] fix: --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 8f74b6eca..a67dc3d32 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -204,7 +204,7 @@ class HostHTTPCodes: create = {200: (True, False, "Host created")} edit = {200: (True, False, "Host modified")} - edit = {200: (True, False, "Host moved")} + move = {200: (True, False, "Host moved")} delete = {204: (True, False, "Host deleted")} From fb60e85e32500997246dbe3da0968bece02c5e3c Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:26:07 +0100 Subject: [PATCH 10/76] test: concept --- plugins/modules/host.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index a67dc3d32..d52d47fb1 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -300,7 +300,6 @@ def _build_move_endpoint(self): self.desired["host_name"], ) - ### def _detect_changes(self): current_attributes = self.current.get("attributes", {}) desired_attributes = self.desired.copy() @@ -380,7 +379,6 @@ def _detect_changes(self): return changes - ### def _get_current(self): result = self._fetch( code_mapping=HostHTTPCodes.get, @@ -417,11 +415,9 @@ def _check_output(self, mode): changed=False, ) - ### def needs_update(self): return len(self._changed_items) > 0 - ### def create(self): data = self.desired.copy() if data.get("attributes", {}) == {}: @@ -446,21 +442,23 @@ def edit(self): data = self.desired.copy() data.pop("host_name") - tmp = {} - tmp["target_folder"] = data.get("folder", "/") self.headers["if-Match"] = self.etag if self.module.check_mode: return self._check_output("edit") - result = self._fetch( - code_mapping=HostHTTPCodes.move, - endpoint=self._build_move_endpoint(), - data=tmp, - method="POST", - ) + if self.current.get("folder") != data.get("folder", "/"): + tmp = {} + tmp["target_folder"] = data.get("folder", "/") + + result = self._fetch( + code_mapping=HostHTTPCodes.move, + endpoint=self._build_move_endpoint(), + data=tmp, + method="POST", + ) - result._replace(msg=result.msg + ". Moved to: %s" % tmp.get("target_folder")) + result._replace(msg=result.msg + ". Moved to: %s" % tmp.get("target_folder")) if self.module.check_mode: return self._check_output("edit") From 109356f5d6f886c7d581100aacf7102d464e94cd Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:36:02 +0100 Subject: [PATCH 11/76] fix: --- plugins/modules/host.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index d52d47fb1..e0a540c88 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -458,7 +458,9 @@ def edit(self): method="POST", ) - result._replace(msg=result.msg + ". Moved to: %s" % tmp.get("target_folder")) + result._replace( + msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") + ) if self.module.check_mode: return self._check_output("edit") @@ -489,8 +491,6 @@ def edit(self): method="PUT", ) else: - data["update_method"] = data.pop("update_attributes") - result = self._fetch( code_mapping=HostHTTPCodes.edit, endpoint=self._build_default_endpoint(), From 06fbf286538818b31a35f13aaf4c042412f0f6e5 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:43:41 +0100 Subject: [PATCH 12/76] fix: remove folder --- plugins/modules/host.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e0a540c88..98f812291 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -392,7 +392,7 @@ def _get_current(self): content = json.loads(result.content) extensions = content["extensions"] - self.current["folder"] = extensions.get("folder", "/") + self.current["folder"] = extensions.pop("folder", "/") for key, value in extensions.items(): if key == "attributes": value.pop("meta_data") @@ -449,7 +449,7 @@ def edit(self): if self.current.get("folder") != data.get("folder", "/"): tmp = {} - tmp["target_folder"] = data.get("folder", "/") + tmp["target_folder"] = data.pop("folder", "/") result = self._fetch( code_mapping=HostHTTPCodes.move, From 68613fa6ff11be34e4af9b92ef49d83dff7f4a71 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 21:49:32 +0100 Subject: [PATCH 13/76] fix: remove folder --- plugins/modules/host.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 98f812291..f9013bd1a 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -461,6 +461,8 @@ def edit(self): result._replace( msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") ) + else: + data.pop("folder") if self.module.check_mode: return self._check_output("edit") From 3f372ac0ad6d4c378926bf09e73016f808922b99 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 22:05:20 +0100 Subject: [PATCH 14/76] refactor: --- plugins/modules/host.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index f9013bd1a..51dfc2d00 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -223,6 +223,8 @@ def __init__(self, module): self.desired["host_name"] = self.params.get("name") + tmp_folder = self.params.get("folder", "/") + for key in HOST: if self.params.get(key): self.desired[key] = self.params.get(key) @@ -236,10 +238,10 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - tmp_folder = self.params.get("folder") - if not tmp_folder: - tmp_folder = self.current.get("folder", "/") - self.desired["folder"] = tmp_folder + + if tmp_folder != self.current.get("folder", "/") + self.desired["folder"] = tmp_folder + self._changed_items = self._detect_changes() self._verify_compatibility() @@ -323,7 +325,7 @@ def _detect_changes(self): ) and current_attributes != desired_attributes.get("attributes"): changes.append("attributes") - if self.current.get("folder") != desired_attributes.get("folder"): + if desired_attributes.get("folder") and self.current.get("folder") != desired_attributes.get("folder"): changes.append("folder") if desired_attributes.get("remove_attributes"): @@ -447,10 +449,10 @@ def edit(self): if self.module.check_mode: return self._check_output("edit") - if self.current.get("folder") != data.get("folder", "/"): - tmp = {} - tmp["target_folder"] = data.pop("folder", "/") + if data.get("folder"): + tmp = {} + tmp["target_folder"] = data.pop("folder") result = self._fetch( code_mapping=HostHTTPCodes.move, endpoint=self._build_move_endpoint(), @@ -461,8 +463,6 @@ def edit(self): result._replace( msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") ) - else: - data.pop("folder") if self.module.check_mode: return self._check_output("edit") From 009a9810f8166799935010c1d6ba565eb4aa08fa Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 22:06:35 +0100 Subject: [PATCH 15/76] fix: --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 51dfc2d00..0e1248998 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -239,7 +239,7 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if tmp_folder != self.current.get("folder", "/") + if tmp_folder != self.current.get("folder", "/"): self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() From 6ca18ec88e3c90489db743e21e05722fe8dfa896 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 22:15:05 +0100 Subject: [PATCH 16/76] add: default folder --- plugins/modules/host.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 0e1248998..f5da93c95 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -325,7 +325,9 @@ def _detect_changes(self): ) and current_attributes != desired_attributes.get("attributes"): changes.append("attributes") - if desired_attributes.get("folder") and self.current.get("folder") != desired_attributes.get("folder"): + if desired_attributes.get("folder") and self.current.get( + "folder" + ) != desired_attributes.get("folder"): changes.append("folder") if desired_attributes.get("remove_attributes"): @@ -428,6 +430,9 @@ def create(self): if data.get("remove_attributes"): data.pop("remove_attributes") + if data.get("folder"): + data["folder"] = "/" + if self.module.check_mode: return self._check_output("create") @@ -449,7 +454,6 @@ def edit(self): if self.module.check_mode: return self._check_output("edit") - if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") From bc62d81d89584d5234571add96aae6357009e9b3 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 22:33:33 +0100 Subject: [PATCH 17/76] fix: folder field --- plugins/modules/host.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index f5da93c95..a3dc7c2eb 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -223,8 +223,6 @@ def __init__(self, module): self.desired["host_name"] = self.params.get("name") - tmp_folder = self.params.get("folder", "/") - for key in HOST: if self.params.get(key): self.desired[key] = self.params.get(key) @@ -239,8 +237,10 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if tmp_folder != self.current.get("folder", "/"): - self.desired["folder"] = tmp_folder + if not self.current.get("folder"): + self.desired["folder"] = self.params.get("folder", '/') + elif self.params.get("folder") and self.params.get("folder") != self.current.get("folder"): + self.desired["folder"] = self.params.get("folder") self._changed_items = self._detect_changes() From 94181a7294d74863224478314e76c84f66bfc9da Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 22:47:35 +0100 Subject: [PATCH 18/76] fix: folder field --- plugins/modules/host.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index a3dc7c2eb..e334b047f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -238,8 +238,13 @@ def __init__(self, module): self._get_current() if not self.current.get("folder"): - self.desired["folder"] = self.params.get("folder", '/') - elif self.params.get("folder") and self.params.get("folder") != self.current.get("folder"): + if self.params.get("folder"): + self.desired["folder"] = self.params.get("folder") + else: + self.desired["folder"] = "/" + elif self.params.get("folder") and self.params.get( + "folder" + ) != self.current.get("folder"): self.desired["folder"] = self.params.get("folder") self._changed_items = self._detect_changes() From 96d28f9a6ad4e923a51e6da2b239c65fa6ed9557 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 23:21:31 +0100 Subject: [PATCH 19/76] fix: folder field --- plugins/modules/host.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e334b047f..0ada51813 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -237,15 +237,15 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() + if self.params.get("folder"): + tmp_folder = self._normalize_folder(self.params.get("folder")) + else: + tmp_folder = "/" + if not self.current.get("folder"): - if self.params.get("folder"): - self.desired["folder"] = self.params.get("folder") - else: - self.desired["folder"] = "/" - elif self.params.get("folder") and self.params.get( - "folder" - ) != self.current.get("folder"): - self.desired["folder"] = self.params.get("folder") + self.desired["folder"] = tmp_folder + elif self.params.get("folder") and tmp_folder != self.current.get("folder"): + self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() @@ -284,7 +284,7 @@ def _verify_compatibility(self): self.module.warn(msg) def _normalize_folder(self, folder): - if folder in ["", " ", "/", "//"]: + if folder and folder in ["", " ", "/", "//"]: return "/" if not folder.startswith("/"): From b1a461dcd814bd622e4796cda23db42abbae6ce8 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 23:35:21 +0100 Subject: [PATCH 20/76] test --- plugins/modules/host.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 0ada51813..8fa6e5107 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -462,16 +462,17 @@ def edit(self): if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") - result = self._fetch( - code_mapping=HostHTTPCodes.move, - endpoint=self._build_move_endpoint(), - data=tmp, - method="POST", - ) + if self.current.get(folder) != tmp.get("target_folder"): + result = self._fetch( + code_mapping=HostHTTPCodes.move, + endpoint=self._build_move_endpoint(), + data=tmp, + method="POST", + ) - result._replace( - msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") - ) + result._replace( + msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") + ) if self.module.check_mode: return self._check_output("edit") From f4e5e2a3da3ffae2bfa7e95e786992f75ec22253 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 23:37:14 +0100 Subject: [PATCH 21/76] test --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 8fa6e5107..e2c7344f7 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -462,7 +462,7 @@ def edit(self): if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") - if self.current.get(folder) != tmp.get("target_folder"): + if self.current.get("folder") != tmp.get("target_folder"): result = self._fetch( code_mapping=HostHTTPCodes.move, endpoint=self._build_move_endpoint(), From f6eac7fcb6d64f617a6336c5f1bfb89b54b4e991 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sat, 17 Feb 2024 23:59:17 +0100 Subject: [PATCH 22/76] fix: folder treatment --- plugins/modules/host.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e2c7344f7..bbba8707e 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -242,9 +242,7 @@ def __init__(self, module): else: tmp_folder = "/" - if not self.current.get("folder"): - self.desired["folder"] = tmp_folder - elif self.params.get("folder") and tmp_folder != self.current.get("folder"): + if not self.current.get("folder") or (self.params.get("folder") and tmp_folder != self.current.get("folder")): self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() @@ -330,7 +328,7 @@ def _detect_changes(self): ) and current_attributes != desired_attributes.get("attributes"): changes.append("attributes") - if desired_attributes.get("folder") and self.current.get( + if desired_attributes.get("folder") and self.current.get("folder") and self.current.get( "folder" ) != desired_attributes.get("folder"): changes.append("folder") From 8045eddf3e1634b41c606f9f566b1749c405b4d2 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 00:26:48 +0100 Subject: [PATCH 23/76] fix: folder treatment --- plugins/modules/host.py | 47 ++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index bbba8707e..7f77eb08f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -237,12 +237,23 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if self.params.get("folder"): - tmp_folder = self._normalize_folder(self.params.get("folder")) + if self.current.get("folder"): + if self.params.get("folder"): + tmp_folder = self._normalize_folder(self.params.get("folder")) + if tmp_folder != self.current.get("folder"): + self.desired["folder"] = tmp_folder + else: + if self.current.get("folder") != "/": + self.desired["folder"] = "/" else: - tmp_folder = "/" + if self.params.get("folder"): + self.desired["folder"] = self._normalize_folder(self.params.get("folder")) + else: + self.desired["folder"] = "/" - if not self.current.get("folder") or (self.params.get("folder") and tmp_folder != self.current.get("folder")): + if not self.current.get("folder") or ( + self.params.get("folder") and tmp_folder != self.current.get("folder") + ): self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() @@ -328,9 +339,11 @@ def _detect_changes(self): ) and current_attributes != desired_attributes.get("attributes"): changes.append("attributes") - if desired_attributes.get("folder") and self.current.get("folder") and self.current.get( - "folder" - ) != desired_attributes.get("folder"): + if ( + desired_attributes.get("folder") + and self.current.get("folder") + and self.current.get("folder") != desired_attributes.get("folder") + ): changes.append("folder") if desired_attributes.get("remove_attributes"): @@ -460,17 +473,17 @@ def edit(self): if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") - if self.current.get("folder") != tmp.get("target_folder"): - result = self._fetch( - code_mapping=HostHTTPCodes.move, - endpoint=self._build_move_endpoint(), - data=tmp, - method="POST", - ) - result._replace( - msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") - ) + result = self._fetch( + code_mapping=HostHTTPCodes.move, + endpoint=self._build_move_endpoint(), + data=tmp, + method="POST", + ) + + result._replace( + msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") + ) if self.module.check_mode: return self._check_output("edit") From 02f16e90b58c3f02f368ff9b768a5aa25f07fbef Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 00:31:40 +0100 Subject: [PATCH 24/76] fix: cleanup --- plugins/modules/host.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 7f77eb08f..d5c6a569f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -247,15 +247,12 @@ def __init__(self, module): self.desired["folder"] = "/" else: if self.params.get("folder"): - self.desired["folder"] = self._normalize_folder(self.params.get("folder")) + self.desired["folder"] = self._normalize_folder( + self.params.get("folder") + ) else: self.desired["folder"] = "/" - if not self.current.get("folder") or ( - self.params.get("folder") and tmp_folder != self.current.get("folder") - ): - self.desired["folder"] = tmp_folder - self._changed_items = self._detect_changes() self._verify_compatibility() From 8e3f13660bb5509d0fd5a70b4200dad23eb9661a Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 00:46:09 +0100 Subject: [PATCH 25/76] check: --- plugins/modules/host.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index d5c6a569f..d96b76fdc 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -240,7 +240,7 @@ def __init__(self, module): if self.current.get("folder"): if self.params.get("folder"): tmp_folder = self._normalize_folder(self.params.get("folder")) - if tmp_folder != self.current.get("folder"): + if tmp_folder != self._normalize_folder(self.current.get("folder")): self.desired["folder"] = tmp_folder else: if self.current.get("folder") != "/": @@ -249,7 +249,7 @@ def __init__(self, module): if self.params.get("folder"): self.desired["folder"] = self._normalize_folder( self.params.get("folder") - ) + ) else: self.desired["folder"] = "/" From f7267befe4c8c0578e33ef2a759cfc9115d8c8d0 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 00:58:56 +0100 Subject: [PATCH 26/76] test: workaround --- plugins/modules/host.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index d96b76fdc..4376a89cd 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -204,7 +204,10 @@ class HostHTTPCodes: create = {200: (True, False, "Host created")} edit = {200: (True, False, "Host modified")} - move = {200: (True, False, "Host moved")} + move = { + 200: (True, False, "Host moved"), + 400: (False, False, "The host is already part of the specified target folder"), + } delete = {204: (True, False, "Host deleted")} From 2da16650e3fb3ec42f9d928252b5c3ae6a559c6f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 01:06:16 +0100 Subject: [PATCH 27/76] cleanup: --- plugins/modules/host.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 4376a89cd..236392acf 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -412,7 +412,6 @@ def _get_current(self): content = json.loads(result.content) extensions = content["extensions"] - self.current["folder"] = extensions.pop("folder", "/") for key, value in extensions.items(): if key == "attributes": value.pop("meta_data") From 6b9d11a2ce92057cb57d662bb915e462e1cb12b7 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 01:22:42 +0100 Subject: [PATCH 28/76] refactor: remove workaround --- plugins/modules/host.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 236392acf..906722bfa 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -204,10 +204,7 @@ class HostHTTPCodes: create = {200: (True, False, "Host created")} edit = {200: (True, False, "Host modified")} - move = { - 200: (True, False, "Host moved"), - 400: (False, False, "The host is already part of the specified target folder"), - } + move = {200: (True, False, "Host moved")} delete = {204: (True, False, "Host deleted")} @@ -246,7 +243,7 @@ def __init__(self, module): if tmp_folder != self._normalize_folder(self.current.get("folder")): self.desired["folder"] = tmp_folder else: - if self.current.get("folder") != "/": + if self._normalize_folder(self.current.get("folder")) != "/": self.desired["folder"] = "/" else: if self.params.get("folder"): From 4637a397faf0860156b2d05eb875327bcabf2383 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 01:33:22 +0100 Subject: [PATCH 29/76] cleanup: --- plugins/modules/host.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 906722bfa..17d6cf314 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -240,10 +240,10 @@ def __init__(self, module): if self.current.get("folder"): if self.params.get("folder"): tmp_folder = self._normalize_folder(self.params.get("folder")) - if tmp_folder != self._normalize_folder(self.current.get("folder")): + if tmp_folder != self.current.get("folder"): self.desired["folder"] = tmp_folder else: - if self._normalize_folder(self.current.get("folder")) != "/": + if self.current.get("folder") != "/": self.desired["folder"] = "/" else: if self.params.get("folder"): @@ -415,6 +415,7 @@ def _get_current(self): if "network_scan_results" in value: value.pop("network_scan_results") self.current[key] = value + self.current["folder"] = self._normalize_folder(self.current.get("folder", "/")) self.etag = result.etag From eb13ab1ac768619a44dd1140dc437a6468c0bb5f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 02:16:15 +0100 Subject: [PATCH 30/76] cleanup: --- plugins/modules/host.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 17d6cf314..87068ddc5 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -334,7 +334,7 @@ def _detect_changes(self): if desired_attributes.get( "attributes" ) and current_attributes != desired_attributes.get("attributes"): - changes.append("attributes") + changes.append("attributes: %s" % json.dumps(desired_attributes.get("attributes"))) if ( desired_attributes.get("folder") @@ -415,7 +415,9 @@ def _get_current(self): if "network_scan_results" in value: value.pop("network_scan_results") self.current[key] = value - self.current["folder"] = self._normalize_folder(self.current.get("folder", "/")) + self.current["folder"] = self._normalize_folder( + self.current.get("folder", "/") + ) self.etag = result.etag From fb6f4421b9fe7ab5b15926977d0fe40182a9e13e Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 02:18:53 +0100 Subject: [PATCH 31/76] add: ~ as a possible default folder --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 87068ddc5..e71833074 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -290,7 +290,7 @@ def _verify_compatibility(self): self.module.warn(msg) def _normalize_folder(self, folder): - if folder and folder in ["", " ", "/", "//"]: + if folder and folder in ["", " ", "/", "//", "~"]: return "/" if not folder.startswith("/"): From 4cd22d3b947c2c0d87e921110a63f66f4f93a197 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 02:32:38 +0100 Subject: [PATCH 32/76] add: effective_attributes --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e71833074..f8629851c 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -399,7 +399,7 @@ def _detect_changes(self): def _get_current(self): result = self._fetch( code_mapping=HostHTTPCodes.get, - endpoint=self._build_default_endpoint(), + endpoint=self._build_default_endpoint() + "?effective_attributes=true", method="GET", ) From 56689a0ace048e7ae297cde29fc0808de8aff33a Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 02:47:16 +0100 Subject: [PATCH 33/76] fix: style --- plugins/modules/host.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index f8629851c..e772be96a 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -334,7 +334,9 @@ def _detect_changes(self): if desired_attributes.get( "attributes" ) and current_attributes != desired_attributes.get("attributes"): - changes.append("attributes: %s" % json.dumps(desired_attributes.get("attributes"))) + changes.append( + "attributes: %s" % json.dumps(desired_attributes.get("attributes")) + ) if ( desired_attributes.get("folder") From 7079bf5e45a092520fbc84b15531d3c544c29152 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 04:16:54 +0100 Subject: [PATCH 34/76] refactor: --- plugins/modules/host.py | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e772be96a..905f41a4b 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -237,21 +237,16 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if self.current.get("folder"): - if self.params.get("folder"): - tmp_folder = self._normalize_folder(self.params.get("folder")) - if tmp_folder != self.current.get("folder"): - self.desired["folder"] = tmp_folder - else: - if self.current.get("folder") != "/": - self.desired["folder"] = "/" + if self.params.get("folder"): + tmp_folder = self._normalize_folder(self.params.get("folder")) else: - if self.params.get("folder"): - self.desired["folder"] = self._normalize_folder( - self.params.get("folder") - ) - else: - self.desired["folder"] = "/" + tmp_folder = self._normalize_folder("/") + + if current_host.state == "present": + if tmp_folder != self.current.get("folder"): + self.desired["folder"] = tmp_folder + else: + self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() From 6e467324875a875a9086d77fbb32c8ec4ea7cd8f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 04:22:59 +0100 Subject: [PATCH 35/76] fix: copy paste --- plugins/modules/host.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 905f41a4b..e4f4911db 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -219,6 +219,11 @@ def __init__(self, module): self.extended_functionality = self.params.get("extended_functionality", True) + if self.params.get("folder"): + tmp_folder = self._normalize_folder(self.params.get("folder")) + else: + tmp_folder = self._normalize_folder("/") + self.desired = {} self.desired["host_name"] = self.params.get("name") @@ -237,12 +242,7 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if self.params.get("folder"): - tmp_folder = self._normalize_folder(self.params.get("folder")) - else: - tmp_folder = self._normalize_folder("/") - - if current_host.state == "present": + if self.current.state == "present": if tmp_folder != self.current.get("folder"): self.desired["folder"] = tmp_folder else: From 7afe850ec77bc80eb6929b850e05914579465ff4 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 04:27:28 +0100 Subject: [PATCH 36/76] fix: copy paste --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e4f4911db..5ce72c35e 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -242,7 +242,7 @@ def __init__(self, module): # Get the current host from the API and set some parameters self._get_current() - if self.current.state == "present": + if self.state == "present": if tmp_folder != self.current.get("folder"): self.desired["folder"] = tmp_folder else: From cce2eb36deaa1ad00cf6561d66c4748fb0533947 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 04:43:14 +0100 Subject: [PATCH 37/76] refactor: --- plugins/modules/host.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 5ce72c35e..fb0d0bfea 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -424,7 +424,9 @@ def _get_current(self): def _check_output(self, mode): return RESULT( http_code=0, - msg="Running in check mode. Would have done an %s" % mode, + msg="Running in check mode. Would have done an %s. Changed: %s" % ( + mode, ", ".join(self._changed_items) + ), content="", etag="", failed=False, @@ -442,9 +444,6 @@ def create(self): if data.get("remove_attributes"): data.pop("remove_attributes") - if data.get("folder"): - data["folder"] = "/" - if self.module.check_mode: return self._check_output("create") @@ -463,6 +462,12 @@ def edit(self): self.headers["if-Match"] = self.etag + if data.get("update_attributes") == {}: + data.pop("update_attributes") + + if data.get("remove_attributes") == []: + data.pop("remove_attributes") + if self.module.check_mode: return self._check_output("edit") @@ -481,15 +486,6 @@ def edit(self): msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") ) - if self.module.check_mode: - return self._check_output("edit") - - if data.get("update_attributes") == {}: - data.pop("update_attributes") - - if data.get("remove_attributes") == []: - data.pop("remove_attributes") - if data.get("update_attributes") and data.get("remove_attributes"): tmp = data.copy() tmp.pop("remove_attributes") From e4966e233606174e44bd9dd081834b1d102cf430 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 04:54:45 +0100 Subject: [PATCH 38/76] cleanup: --- plugins/modules/host.py | 43 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index fb0d0bfea..1c8c7f8b9 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -424,9 +424,8 @@ def _get_current(self): def _check_output(self, mode): return RESULT( http_code=0, - msg="Running in check mode. Would have done an %s. Changed: %s" % ( - mode, ", ".join(self._changed_items) - ), + msg="Running in check mode. Would have done an %s. Changed: %s" + % (mode, ", ".join(self._changed_items)), content="", etag="", failed=False, @@ -462,12 +461,6 @@ def edit(self): self.headers["if-Match"] = self.etag - if data.get("update_attributes") == {}: - data.pop("update_attributes") - - if data.get("remove_attributes") == []: - data.pop("remove_attributes") - if self.module.check_mode: return self._check_output("edit") @@ -486,32 +479,12 @@ def edit(self): msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") ) - if data.get("update_attributes") and data.get("remove_attributes"): - tmp = data.copy() - tmp.pop("remove_attributes") - self.module.fail_json(json.dumps(tmp)) - result = self._fetch( - code_mapping=HostHTTPCodes.edit, - endpoint=self._build_default_endpoint(), - data=tmp, - method="PUT", - ) - - tmp = data.copy() - tmp.pop("update_attributes") - result = self._fetch( - code_mapping=HostHTTPCodes.edit, - endpoint=self._build_default_endpoint(), - data=tmp, - method="PUT", - ) - else: - result = self._fetch( - code_mapping=HostHTTPCodes.edit, - endpoint=self._build_default_endpoint(), - data=data, - method="PUT", - ) + result = self._fetch( + code_mapping=HostHTTPCodes.edit, + endpoint=self._build_default_endpoint(), + data=data, + method="PUT", + ) return result._replace( msg=result.msg + ". Changed: %s" % ", ".join(self._changed_items) From 9f21b919327f38debc726f719a5948e48d703fff Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 05:09:40 +0100 Subject: [PATCH 39/76] add: extend edit msg --- plugins/modules/host.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 1c8c7f8b9..e8278a599 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -464,19 +464,20 @@ def edit(self): if self.module.check_mode: return self._check_output("edit") + result_move = {} if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") - result = self._fetch( + result_move = self._fetch( code_mapping=HostHTTPCodes.move, endpoint=self._build_move_endpoint(), data=tmp, method="POST", ) - result._replace( - msg=result.msg + ". Moved to: %s" % tmp.get("target_folder") + result_move._replace( + msg=result_move.msg + ". Moved to: %s" % tmp.get("target_folder") ) result = self._fetch( @@ -487,7 +488,7 @@ def edit(self): ) return result._replace( - msg=result.msg + ". Changed: %s" % ", ".join(self._changed_items) + msg=result_move.get("msg", "") + result.msg + ". Changed: %s" % ", ".join(self._changed_items) ) def delete(self): From ab07411b6df0dba11909db25d6c3e565162c3295 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 05:22:16 +0100 Subject: [PATCH 40/76] fix: --- plugins/modules/host.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e8278a599..5022d6907 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -488,7 +488,9 @@ def edit(self): ) return result._replace( - msg=result_move.get("msg", "") + result.msg + ". Changed: %s" % ", ".join(self._changed_items) + msg=(result_move.msg if result_move != {} else "") + + result.msg + + ". Changed: %s" % ", ".join(self._changed_items) ) def delete(self): From 2bea0f44a444f59ac838f9444853300f4edf124d Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 05:36:45 +0100 Subject: [PATCH 41/76] fix: --- plugins/modules/host.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 5022d6907..1a73badd3 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -476,7 +476,7 @@ def edit(self): method="POST", ) - result_move._replace( + result_move = result_move._replace( msg=result_move.msg + ". Moved to: %s" % tmp.get("target_folder") ) @@ -488,7 +488,7 @@ def edit(self): ) return result._replace( - msg=(result_move.msg if result_move != {} else "") + msg=((result_move.msg + ". ") if result_move != {} else "") + result.msg + ". Changed: %s" % ", ".join(self._changed_items) ) From 3d6fbcab3303db2601ee31efd4ba11948e3c5447 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 05:51:26 +0100 Subject: [PATCH 42/76] test: --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 1a73badd3..df2797ca0 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -477,7 +477,7 @@ def edit(self): ) result_move = result_move._replace( - msg=result_move.msg + ". Moved to: %s" % tmp.get("target_folder") + msg=result_move.msg + ". Moved from % to: %s" % (self.current.get("folder"), tmp.get("target_folder")) ) result = self._fetch( From 22cc4437099b551a06f63185d76fed3fec7cb58f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 05:59:57 +0100 Subject: [PATCH 43/76] fix: test --- plugins/modules/host.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index df2797ca0..3fb89d9c6 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -477,7 +477,8 @@ def edit(self): ) result_move = result_move._replace( - msg=result_move.msg + ". Moved from % to: %s" % (self.current.get("folder"), tmp.get("target_folder")) + msg=result_move.msg + + ". Moved from %s to: %s" % (self.current.get("folder"), tmp.get("target_folder")) ) result = self._fetch( From c198850a65cdd69609c73d4aa06df0d61739427e Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:14:52 +0100 Subject: [PATCH 44/76] fix: refactor logic of folder field --- plugins/modules/host.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 3fb89d9c6..04a1e663f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -219,10 +219,8 @@ def __init__(self, module): self.extended_functionality = self.params.get("extended_functionality", True) - if self.params.get("folder"): - tmp_folder = self._normalize_folder(self.params.get("folder")) - else: - tmp_folder = self._normalize_folder("/") + if self.params["folder"]: + self.params["folder"] = self._normalize_folder(self.params.get("folder")) self.desired = {} @@ -243,10 +241,8 @@ def __init__(self, module): self._get_current() if self.state == "present": - if tmp_folder != self.current.get("folder"): + if self.params["folder"] and self.current["folder"] != self.params["folder"] self.desired["folder"] = tmp_folder - else: - self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() @@ -443,6 +439,9 @@ def create(self): if data.get("remove_attributes"): data.pop("remove_attributes") + if not data["folder"]: + data["folder"] = self._normalize_folder("/") + if self.module.check_mode: return self._check_output("create") From 4fb4876bcff56e5e2e437f1b66896e6cc83c8d7b Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:16:37 +0100 Subject: [PATCH 45/76] fix: ups : --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 04a1e663f..4ea5a771f 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -241,7 +241,7 @@ def __init__(self, module): self._get_current() if self.state == "present": - if self.params["folder"] and self.current["folder"] != self.params["folder"] + if self.params["folder"] and self.current["folder"] != self.params["folder"]: self.desired["folder"] = tmp_folder self._changed_items = self._detect_changes() From aee96fa9dda93bf89324fb45dbd1f6bfb9c79506 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:23:28 +0100 Subject: [PATCH 46/76] fix: --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 4ea5a771f..5d52ce250 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -439,7 +439,7 @@ def create(self): if data.get("remove_attributes"): data.pop("remove_attributes") - if not data["folder"]: + if not data.get("folder"): data["folder"] = self._normalize_folder("/") if self.module.check_mode: From bc9ebdd40bfda5cd04bf4dc9e6d57b9d7eac1917 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:28:55 +0100 Subject: [PATCH 47/76] fix: cleanup --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 5d52ce250..e98c69e71 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -242,7 +242,7 @@ def __init__(self, module): if self.state == "present": if self.params["folder"] and self.current["folder"] != self.params["folder"]: - self.desired["folder"] = tmp_folder + self.desired["folder"] = self.params["folder"] self._changed_items = self._detect_changes() From 3a26e222084366135d5d908290a1d0825b09d1a5 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:38:40 +0100 Subject: [PATCH 48/76] cleanup: --- plugins/modules/host.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e98c69e71..f0755f348 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -219,7 +219,7 @@ def __init__(self, module): self.extended_functionality = self.params.get("extended_functionality", True) - if self.params["folder"]: + if self.params.get("folder"): self.params["folder"] = self._normalize_folder(self.params.get("folder")) self.desired = {} @@ -241,7 +241,7 @@ def __init__(self, module): self._get_current() if self.state == "present": - if self.params["folder"] and self.current["folder"] != self.params["folder"]: + if self.params.get("folder") and self.current["folder"] != self.params["folder"]: self.desired["folder"] = self.params["folder"] self._changed_items = self._detect_changes() @@ -281,7 +281,7 @@ def _verify_compatibility(self): self.module.warn(msg) def _normalize_folder(self, folder): - if folder and folder in ["", " ", "/", "//", "~"]: + if folder in ["", " ", "/", "//", "~"]: return "/" if not folder.startswith("/"): @@ -476,8 +476,7 @@ def edit(self): ) result_move = result_move._replace( - msg=result_move.msg - + ". Moved from %s to: %s" % (self.current.get("folder"), tmp.get("target_folder")) + msg=result_move.msg + ". Moved from to: %s" % tmp.get("target_folder") ) result = self._fetch( From cf73d22d13f86622889f6ad816398e2a43b9c28e Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 14:41:27 +0100 Subject: [PATCH 49/76] fix: style --- plugins/modules/host.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index f0755f348..249cebf7c 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -241,7 +241,10 @@ def __init__(self, module): self._get_current() if self.state == "present": - if self.params.get("folder") and self.current["folder"] != self.params["folder"]: + if ( + self.params.get("folder") + and self.current["folder"] != self.params["folder"] + ): self.desired["folder"] = self.params["folder"] self._changed_items = self._detect_changes() From 450877cdc6c48898451264ee148ba1f9d9287bd1 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 21:55:05 +0100 Subject: [PATCH 50/76] add: author --- plugins/modules/host.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 249cebf7c..2834fe4b9 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -70,6 +70,7 @@ - Robin Gierse (@robin-checkmk) - Lars Getwan (@lgetwan) - Oliver Gaida (@ogaida) + - Michael Sekania (@msekania) """ EXAMPLES = r""" From 4653caa9d3dafe57d28c7cde6739a7a84c0db9e1 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 22:17:34 +0100 Subject: [PATCH 51/76] fix: copy pase error --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 2834fe4b9..e13815bda 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -551,7 +551,7 @@ def run_module(): elif current_host.needs_update(): result = current_host.edit() elif current_host.state == "absent": - result = result._replace(msg="Folder already absent.") + result = result._replace(msg="Host already absent.") if desired_state in ("present"): result = current_host.create() From f15a4aa0b17b308f79ba61ba2e543b43adece87f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Sun, 18 Feb 2024 22:43:13 +0100 Subject: [PATCH 52/76] refactor: --- plugins/modules/host.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index e13815bda..697678d79 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -310,6 +310,7 @@ def _build_move_endpoint(self): def _detect_changes(self): current_attributes = self.current.get("attributes", {}) + current_folder = self.current.get("folder") desired_attributes = self.desired.copy() changes = [] @@ -335,8 +336,8 @@ def _detect_changes(self): if ( desired_attributes.get("folder") - and self.current.get("folder") - and self.current.get("folder") != desired_attributes.get("folder") + and current_folder + and current_folder != desired_attributes.get("folder") ): changes.append("folder") From 0cfecf3b90e2559f6948254e6edb323c2a48c1e0 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Wed, 21 Feb 2024 23:26:47 +0100 Subject: [PATCH 53/76] fix: folder title treatment --- plugins/modules/folder.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/modules/folder.py b/plugins/modules/folder.py index ea2500b79..8a50d5fc2 100644 --- a/plugins/modules/folder.py +++ b/plugins/modules/folder.py @@ -31,7 +31,7 @@ required: true type: str name: - description: The name of your folder. If omitted defaults to the folder name. + description: The name (title) of your folder. If omitted defaults to the folder-name from path. type: str aliases: [title] attributes: @@ -220,7 +220,11 @@ def __init__(self, module): (self.desired["parent"], self.desired["name"]) = self._normalize_path( self.params.get("path") ) - self.desired["title"] = self.params.get("title", self.desired["name"]) + + if self.params.get("title"): + self.desired["title"] = self.params.get("name") + else: + self.desired["title"] = self.desired.get("name") for key in FOLDER: if self.params.get(key): From 8b8392b7bbfdc2391bb3409e4db54655111e7ee4 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 10:00:34 +0100 Subject: [PATCH 54/76] extend: test suite --- .../integration/targets/folder/tasks/test.yml | 68 +++++++++++++++++++ .../integration/targets/folder/vars/main.yml | 10 +++ 2 files changed, 78 insertions(+) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index 65eb9016f..f07b19597 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -158,3 +158,71 @@ - "{{ outer_item.site }}" delegate_to: localhost run_once: true # noqa run-once[task] + +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Create folders for defaults test." + folder: + server_url: "{{ checkmk_var_server_url }}" + site: "{{ outer_item.site }}" + automation_user: "{{ checkmk_var_automation_user }}" + automation_secret: "{{ checkmk_var_automation_secret }}" + path: "{{ item.path }}" + name: "{{ item.name | default(omit) }}" + state: "present" + loop: "{{ checkmk_var_folders_defaults_test }}" + delegate_to: localhost + run_once: true # noqa run-once[task] + +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Activate." + activation: + server_url: "{{ checkmk_var_server_url }}" + site: "{{ outer_item.site }}" + automation_user: "{{ checkmk_var_automation_user }}" + automation_secret: "{{ checkmk_var_automation_secret }}" + force_foreign_changes: true + sites: + - "{{ outer_item.site }}" + delegate_to: localhost + run_once: true # noqa run-once[task] + +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." + ansible.builtin.assert: + that: "results.title == item.verify_name" + vars: + results: "{{ lookup('checkmk.general.folder', + item.path, + server_url=checkmk_var_server_url, + site=outer_item.site, + validate_certs=False, + automation_user=checkmk_var_automation_user, + automation_secret=checkmk_var_automation_secret) + }}" + loop: "{{ checkmk_var_folders_defaults_test }}" + fail_msg: "Creted folders title does not match to the expected one!" + delegate_to: localhost + run_once: true # noqa run-once[task] + +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Delete folders." + folder: + server_url: "{{ checkmk_var_server_url }}" + site: "{{ outer_item.site }}" + automation_user: "{{ checkmk_var_automation_user }}" + automation_secret: "{{ checkmk_var_automation_secret }}" + path: "{{ item.path }}" + name: "{{ item.name | default(omit) }}" + state: "absent" + loop: "{{ checkmk_var_folders_defaults_test }}" + delegate_to: localhost + run_once: true # noqa run-once[task] + loop: "{{ checkmk_var_folders }}" + +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Activate." + activation: + server_url: "{{ checkmk_var_server_url }}" + site: "{{ outer_item.site }}" + automation_user: "{{ checkmk_var_automation_user }}" + automation_secret: "{{ checkmk_var_automation_secret }}" + force_foreign_changes: true + sites: + - "{{ outer_item.site }}" + delegate_to: localhost + run_once: true # noqa run-once[task] diff --git a/tests/integration/targets/folder/vars/main.yml b/tests/integration/targets/folder/vars/main.yml index 30d015abd..958cea01b 100644 --- a/tests/integration/targets/folder/vars/main.yml +++ b/tests/integration/targets/folder/vars/main.yml @@ -35,3 +35,13 @@ checkmk_folder_attr_test: attributes: tag_criticality: "test" tag_networking: "dmz" + +checkmk_var_folders_defaults_test: + - path: /foo + verify_name: "foo" + - path: /foo/bar + name: Bar + verify_name: "Bar" + - path: /foo/bar/tresure + verify_name: "treasure" + From d67e0213457999f4b0680a793bcde905f24da817 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 10:06:41 +0100 Subject: [PATCH 55/76] extend: test suite --- tests/integration/targets/folder/tasks/test.yml | 1 - tests/integration/targets/folder/vars/main.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index f07b19597..afbff5114 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -213,7 +213,6 @@ loop: "{{ checkmk_var_folders_defaults_test }}" delegate_to: localhost run_once: true # noqa run-once[task] - loop: "{{ checkmk_var_folders }}" - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Activate." activation: diff --git a/tests/integration/targets/folder/vars/main.yml b/tests/integration/targets/folder/vars/main.yml index 958cea01b..8e9503e33 100644 --- a/tests/integration/targets/folder/vars/main.yml +++ b/tests/integration/targets/folder/vars/main.yml @@ -44,4 +44,3 @@ checkmk_var_folders_defaults_test: verify_name: "Bar" - path: /foo/bar/tresure verify_name: "treasure" - From 3533228c8fdc39dc68ee304177c5f22c21a08f6a Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 10:15:38 +0100 Subject: [PATCH 56/76] fix: --- tests/integration/targets/folder/tasks/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index afbff5114..d667103ba 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -197,7 +197,6 @@ automation_secret=checkmk_var_automation_secret) }}" loop: "{{ checkmk_var_folders_defaults_test }}" - fail_msg: "Creted folders title does not match to the expected one!" delegate_to: localhost run_once: true # noqa run-once[task] From f7a74a2c20a0f33c7d7fbd2431a627c2513374f4 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 10:40:27 +0100 Subject: [PATCH 57/76] fix: --- tests/integration/targets/folder/tasks/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index d667103ba..dd649b40e 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,7 +186,7 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "results.title == item.verify_name" + that: "results.name == item.verify_name" vars: results: "{{ lookup('checkmk.general.folder', item.path, From 127864682e7910ea0f313a925a33fb8983ea0158 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 11:38:47 +0100 Subject: [PATCH 58/76] fix: with folders lookup --- tests/integration/targets/folder/tasks/test.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index dd649b40e..d95b2d249 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,16 +186,17 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "results.name == item.verify_name" + that: "folder_title[item.path | regex_replace('/', '~')] == item.verify_name" vars: - results: "{{ lookup('checkmk.general.folder', - item.path, + results: "{{ lookup('checkmk.general.folders', + '~', server_url=checkmk_var_server_url, site=outer_item.site, validate_certs=False, automation_user=checkmk_var_automation_user, automation_secret=checkmk_var_automation_secret) }}" + folder_title: "{{ dict((results | map(attribute='id')) | zip(results | map(attribute='title'))) }}" loop: "{{ checkmk_var_folders_defaults_test }}" delegate_to: localhost run_once: true # noqa run-once[task] From c5ed153ed836bb83079b855ba9d0f9e25dab3b8c Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 11:53:22 +0100 Subject: [PATCH 59/76] fix: add hack --- tests/integration/targets/folder/tasks/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index d95b2d249..d297f7a7c 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,7 +186,7 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "folder_title[item.path | regex_replace('/', '~')] == item.verify_name" + that: "folder_title['~' + (item.path | regex_replace('/', '~') | split('~') | last)] == item.verify_name" vars: results: "{{ lookup('checkmk.general.folders', '~', From 2f2ec07d390e90515636aed28b265c46b8b37c4f Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 13:48:00 +0100 Subject: [PATCH 60/76] fix: --- tests/integration/targets/folder/tasks/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index d297f7a7c..dfc72cd4f 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,7 +186,7 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "folder_title['~' + (item.path | regex_replace('/', '~') | split('~') | last)] == item.verify_name" + that: "folder_title[item.path] == item.verify_name" vars: results: "{{ lookup('checkmk.general.folders', '~', @@ -196,7 +196,7 @@ automation_user=checkmk_var_automation_user, automation_secret=checkmk_var_automation_secret) }}" - folder_title: "{{ dict((results | map(attribute='id')) | zip(results | map(attribute='title'))) }}" + folder_title: "{{ dict((results | map(attribute='id') | map('regex_replace', '/', '~')) | zip(results | map(attribute='title'))) }}" loop: "{{ checkmk_var_folders_defaults_test }}" delegate_to: localhost run_once: true # noqa run-once[task] From aab269b6443117dfb9dea31e055df6051c1df624 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 13:54:32 +0100 Subject: [PATCH 61/76] fix: --- tests/integration/targets/folder/tasks/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index dfc72cd4f..905b6f99d 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,7 +186,7 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "folder_title[item.path] == item.verify_name" + that: "folder_title[item.path | regex_replace('/', '~') ] == item.verify_name" vars: results: "{{ lookup('checkmk.general.folders', '~', From 29f58119980f0efaeb169488af657fd3c7f34550 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 14:00:53 +0100 Subject: [PATCH 62/76] fix: logic --- tests/integration/targets/folder/tasks/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index 905b6f99d..9290d1ef4 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -186,7 +186,7 @@ - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: - that: "folder_title[item.path | regex_replace('/', '~') ] == item.verify_name" + that: "folder_title[item.path | regex_replace('~', '/') ] == item.verify_name" vars: results: "{{ lookup('checkmk.general.folders', '~', @@ -196,7 +196,7 @@ automation_user=checkmk_var_automation_user, automation_secret=checkmk_var_automation_secret) }}" - folder_title: "{{ dict((results | map(attribute='id') | map('regex_replace', '/', '~')) | zip(results | map(attribute='title'))) }}" + folder_title: "{{ dict((results | map(attribute='id') | map('regex_replace', '~', '/')) | zip(results | map(attribute='title'))) }}" loop: "{{ checkmk_var_folders_defaults_test }}" delegate_to: localhost run_once: true # noqa run-once[task] From 21fd8c843cb0cca1d6b42ac329437f7ea5780ce2 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 14:08:26 +0100 Subject: [PATCH 63/76] refactor: simplify test --- tests/integration/targets/folder/vars/main.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/integration/targets/folder/vars/main.yml b/tests/integration/targets/folder/vars/main.yml index 8e9503e33..3b406f7f4 100644 --- a/tests/integration/targets/folder/vars/main.yml +++ b/tests/integration/targets/folder/vars/main.yml @@ -39,8 +39,6 @@ checkmk_folder_attr_test: checkmk_var_folders_defaults_test: - path: /foo verify_name: "foo" - - path: /foo/bar + - path: /bar name: Bar verify_name: "Bar" - - path: /foo/bar/tresure - verify_name: "treasure" From 1a5800d37af163c91d7da25161a2b29e837c43d2 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 14:17:45 +0100 Subject: [PATCH 64/76] test: --- tests/integration/targets/folder/tasks/test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/integration/targets/folder/tasks/test.yml b/tests/integration/targets/folder/tasks/test.yml index 9290d1ef4..b425d7f01 100644 --- a/tests/integration/targets/folder/tasks/test.yml +++ b/tests/integration/targets/folder/tasks/test.yml @@ -184,6 +184,22 @@ delegate_to: localhost run_once: true # noqa run-once[task] +- name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Print created folders." + ansible.builtin.debug: + var: folder_title + vars: + results: "{{ lookup('checkmk.general.folders', + '~', + server_url=checkmk_var_server_url, + site=outer_item.site, + validate_certs=False, + automation_user=checkmk_var_automation_user, + automation_secret=checkmk_var_automation_secret) + }}" + folder_title: "{{ dict((results | map(attribute='id') | map('regex_replace', '~', '/')) | zip(results | map(attribute='title'))) }}" + delegate_to: localhost + run_once: true # noqa run-once[task] + - name: "{{ outer_item.version }} - {{ outer_item.edition | upper }} - Verify created folders." ansible.builtin.assert: that: "folder_title[item.path | regex_replace('~', '/') ] == item.verify_name" From 66302786e8088662d797be35d61e28f3af1d8871 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Thu, 22 Feb 2024 14:30:05 +0100 Subject: [PATCH 65/76] fix: again copy pase error --- plugins/modules/folder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/folder.py b/plugins/modules/folder.py index 8a50d5fc2..139e2a14b 100644 --- a/plugins/modules/folder.py +++ b/plugins/modules/folder.py @@ -221,7 +221,7 @@ def __init__(self, module): self.params.get("path") ) - if self.params.get("title"): + if self.params.get("name"): self.desired["title"] = self.params.get("name") else: self.desired["title"] = self.desired.get("name") From 6fff122a34f7e03a2514b9f00ca8c5e0e3f806a2 Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Fri, 1 Mar 2024 14:01:35 +0100 Subject: [PATCH 66/76] change: changed status for check mode --- plugins/modules/folder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/folder.py b/plugins/modules/folder.py index 139e2a14b..d955c0e66 100644 --- a/plugins/modules/folder.py +++ b/plugins/modules/folder.py @@ -403,7 +403,7 @@ def _check_output(self, mode): content="", etag="", failed=False, - changed=False, + changed=True, ) def needs_update(self): From 7795b1c38e9f93fc0fafe2b76c7810d7f4482d0c Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Fri, 1 Mar 2024 14:02:25 +0100 Subject: [PATCH 67/76] change: changed status for check mode --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 697678d79..9d9908d51 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -430,7 +430,7 @@ def _check_output(self, mode): content="", etag="", failed=False, - changed=False, + changed=True, ) def needs_update(self): From b70ea20db0eed34ef965488a1e3731ba81583fdd Mon Sep 17 00:00:00 2001 From: Sebastien Laithier Date: Tue, 12 Mar 2024 09:45:22 +0100 Subject: [PATCH 68/76] Updating lookup exemple --- docs/version_lookup.rst | 5 +++-- plugins/lookup/version.py | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/version_lookup.rst b/docs/version_lookup.rst index 7d815c5a4..3dd9b6419 100644 --- a/docs/version_lookup.rst +++ b/docs/version_lookup.rst @@ -385,13 +385,14 @@ Examples .. code-block:: yaml+jinja - + - name: "Show Checkmk version" debug: msg: "Server version is {{ version }}" vars: version: "{{ lookup('checkmk.general.version', - server_url + '/' + site, + server_url=my_url, + site=my_site, validate_certs=False, automation_user=my_user, automation_secret=my_secret diff --git a/plugins/lookup/version.py b/plugins/lookup/version.py index 389876af2..a6949ef22 100644 --- a/plugins/lookup/version.py +++ b/plugins/lookup/version.py @@ -89,7 +89,8 @@ msg: "Server version is {{ version }}" vars: version: "{{ lookup('checkmk.general.version', - server_url + '/' + site, + server_url=my_url, + site=my_site, validate_certs=False, automation_user=my_user, automation_secret=my_secret From d66822e2ab26c54b1eef707e05a459978c409f47 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 20 Mar 2024 16:59:36 +0000 Subject: [PATCH 69/76] Bump black from 22.3.0 to 24.3.0 Bumps [black](https://github.com/psf/black) from 22.3.0 to 24.3.0. - [Release notes](https://github.com/psf/black/releases) - [Changelog](https://github.com/psf/black/blob/main/CHANGES.md) - [Commits](https://github.com/psf/black/compare/22.3.0...24.3.0) --- updated-dependencies: - dependency-name: black dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements-qa.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-qa.txt b/requirements-qa.txt index ef2b610e8..dba4b97be 100644 --- a/requirements-qa.txt +++ b/requirements-qa.txt @@ -1,4 +1,4 @@ -black==22.3.0 +black==24.3.0 click==8.1.3 isort==5.10.1 mypy-extensions==0.4.3 From b0fa7a91c2f104c015b51622e13ec4e8de64e290 Mon Sep 17 00:00:00 2001 From: thorian93 Date: Sat, 23 Mar 2024 15:32:53 -0400 Subject: [PATCH 70/76] Clean up release.yaml. --- .github/workflows/release.yaml | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index c8ad39b7b..32f1d7bfb 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,14 +1,15 @@ --- - -env: - NAMESPACE: checkmk - COLLECTION_NAME: general - name: Release Collection on: # yamllint disable-line rule:truthy workflow_dispatch: +env: + NAMESPACE: checkmk + COLLECTION_NAME: general + FILES: "ansible.cfg CHANGELOG.rst galaxy.yml LICENSE README.md" + DIRS: "changelogs docs meta playbooks plugins roles" + jobs: # @@ -83,8 +84,8 @@ jobs: cp $files build/src cp -rf $directories build/src env: - files: "ansible.cfg CHANGELOG.rst galaxy.yml LICENSE README.md" - directories: "changelogs docs meta playbooks plugins roles" + files: ${{env.FILES}} + directories: ${{env.DIRS}} - name: Build Ansible Collection run: ansible-galaxy collection build build/src --force @@ -103,7 +104,7 @@ jobs: with: commit-message: Update Docs and Changelogs upon Release signoff: false - branch: changelogs-docs-update-devel + branch: changelogs-docs-update-${{ steps.current_version.outputs.version }} base: devel delete-branch: true title: '[Auto] Update changelogs and docs upon release' @@ -124,8 +125,8 @@ jobs: cp -rf $directories build/src rm -rf build/src/roles/*/molecule env: - files: "CHANGELOG.rst LICENSE README.md ansible.cfg galaxy.yml" - directories: "changelogs docs meta playbooks plugins roles" + files: ${{env.FILES}} + directories: ${{env.DIRS}} - name: Build Ansible Collection run: ansible-galaxy collection build build/src --force From 953fc23636d1f05be2251a8e03be60749513c0ab Mon Sep 17 00:00:00 2001 From: thorian93 Date: Sat, 23 Mar 2024 15:33:17 -0400 Subject: [PATCH 71/76] Comment unnecessary tests. --- roles/agent/molecule/2.0.0/molecule.yml | 2 +- roles/agent/molecule/2.1.0/molecule.yml | 2 +- roles/agent/molecule/2.2.0/molecule.yml | 2 +- roles/server/molecule/2.0.0/molecule.yml | 2 +- roles/server/molecule/2.1.0/molecule.yml | 2 +- roles/server/molecule/2.2.0/molecule.yml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/roles/agent/molecule/2.0.0/molecule.yml b/roles/agent/molecule/2.0.0/molecule.yml index 855cb0840..363d0b3ea 100644 --- a/roles/agent/molecule/2.0.0/molecule.yml +++ b/roles/agent/molecule/2.0.0/molecule.yml @@ -97,7 +97,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy diff --git a/roles/agent/molecule/2.1.0/molecule.yml b/roles/agent/molecule/2.1.0/molecule.yml index 47f80c03e..8e4540820 100644 --- a/roles/agent/molecule/2.1.0/molecule.yml +++ b/roles/agent/molecule/2.1.0/molecule.yml @@ -72,7 +72,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy diff --git a/roles/agent/molecule/2.2.0/molecule.yml b/roles/agent/molecule/2.2.0/molecule.yml index 47f80c03e..8e4540820 100644 --- a/roles/agent/molecule/2.2.0/molecule.yml +++ b/roles/agent/molecule/2.2.0/molecule.yml @@ -72,7 +72,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy diff --git a/roles/server/molecule/2.0.0/molecule.yml b/roles/server/molecule/2.0.0/molecule.yml index 29383e10f..333a2f95b 100644 --- a/roles/server/molecule/2.0.0/molecule.yml +++ b/roles/server/molecule/2.0.0/molecule.yml @@ -74,7 +74,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy diff --git a/roles/server/molecule/2.1.0/molecule.yml b/roles/server/molecule/2.1.0/molecule.yml index 47f80c03e..8e4540820 100644 --- a/roles/server/molecule/2.1.0/molecule.yml +++ b/roles/server/molecule/2.1.0/molecule.yml @@ -72,7 +72,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy diff --git a/roles/server/molecule/2.2.0/molecule.yml b/roles/server/molecule/2.2.0/molecule.yml index 47f80c03e..8e4540820 100644 --- a/roles/server/molecule/2.2.0/molecule.yml +++ b/roles/server/molecule/2.2.0/molecule.yml @@ -72,7 +72,7 @@ scenario: - prepare - converge # - idempotence # disable for now, as we have non-idempotent tasks - - side_effect + # - side_effect - verify - cleanup - destroy From e426afae8ab66c6d6e16e690c1b395c72b2d9457 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 25 Mar 2024 06:45:32 +0000 Subject: [PATCH 72/76] Bump contributor-assistant/github-action from 2.3.1 to 2.3.2 Bumps [contributor-assistant/github-action](https://github.com/contributor-assistant/github-action) from 2.3.1 to 2.3.2. - [Release notes](https://github.com/contributor-assistant/github-action/releases) - [Commits](https://github.com/contributor-assistant/github-action/compare/v2.3.1...v2.3.2) --- updated-dependencies: - dependency-name: contributor-assistant/github-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- .github/workflows/cla.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cla.yaml b/.github/workflows/cla.yaml index fc25f319a..1b195aeb3 100644 --- a/.github/workflows/cla.yaml +++ b/.github/workflows/cla.yaml @@ -18,7 +18,7 @@ jobs: steps: - name: 'CLA Assistant' if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I hereby sign the CLA or my organization already has a signed CLA.') || github.event_name == 'pull_request_target' - uses: contributor-assistant/github-action@v2.3.1 + uses: contributor-assistant/github-action@v2.3.2 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # the below token should have repo scope and must be manually added by you in the repository's secret From e57d8f8589d86b1dd88b7bbf819035d0e615964b Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Mon, 25 Mar 2024 20:50:00 +0100 Subject: [PATCH 73/76] modify: move and update are exclusive operation --- plugins/modules/host.py | 61 +++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 9d9908d51..68f80d79a 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -28,18 +28,25 @@ required: true type: str folder: - description: The folder your host is located in. On create it defaults to C(/). + description: + - The folder your host is located in. + On create it defaults to C(/). + B(For existing host, host is moved to the specified folder if different + and this procedue is mutualy exclusive with specified + I(attributes), I(update_attributes), and I(remove_attributes)). type: str attributes: description: - The attributes of your host as described in the API documentation. B(Attention! This option OVERWRITES all existing attributes!) + B(Attention! I(folder) should match the folder where host is residing) If you are using custom tags, make sure to prepend the attribute with C(tag_). type: raw required: false update_attributes: description: - The update_attributes of your host as described in the API documentation. + B(Attention! I(folder) should match the folder where host is residing) This will only update the given attributes. If you are using custom tags, make sure to prepend the attribute with C(tag_). type: raw @@ -47,6 +54,7 @@ remove_attributes: description: - The remove_attributes of your host as described in the API documentation. + B(Attention! I(folder) should match the folder where host is residing) B(If a list of strings is supplied, the listed attributes are removed.) B(If extended_functionality and a dict is supplied, the attributes that exactly match the passed attributes are removed.) @@ -334,13 +342,6 @@ def _detect_changes(self): "attributes: %s" % json.dumps(desired_attributes.get("attributes")) ) - if ( - desired_attributes.get("folder") - and current_folder - and current_folder != desired_attributes.get("folder") - ): - changes.append("folder") - if desired_attributes.get("remove_attributes"): tmp_remove_attributes = desired_attributes.get("remove_attributes") @@ -387,6 +388,21 @@ def _detect_changes(self): exception=e, ) + if ( + desired_attributes.get("folder") + and current_folder + and current_folder != desired_attributes.get("folder") + ): + if self.state == "present" and len(changes) > 0: + self.module.fail_json( + msg="ERROR: The folder parameter is different from the folder in which the host is located, while other parameters are also specified!\n \ + If you want to move the host to a specific folder, please omit the other parameters: \ + 'attributes', 'update_attributes' and 'remove_attributes'". + exception=e, + ) + else: + changes.append("folder") + if self.extended_functionality: self.desired = desired_attributes.copy() @@ -468,34 +484,31 @@ def edit(self): if self.module.check_mode: return self._check_output("edit") - result_move = {} if data.get("folder"): tmp = {} tmp["target_folder"] = data.pop("folder") - result_move = self._fetch( + result = self._fetch( code_mapping=HostHTTPCodes.move, endpoint=self._build_move_endpoint(), data=tmp, method="POST", ) - result_move = result_move._replace( - msg=result_move.msg + ". Moved from to: %s" % tmp.get("target_folder") + return result._replace( + msg=result.msg + ". Moved from to: %s" % tmp.get("target_folder") + ) + else: + result = self._fetch( + code_mapping=HostHTTPCodes.edit, + endpoint=self._build_default_endpoint(), + data=data, + method="PUT", ) - result = self._fetch( - code_mapping=HostHTTPCodes.edit, - endpoint=self._build_default_endpoint(), - data=data, - method="PUT", - ) - - return result._replace( - msg=((result_move.msg + ". ") if result_move != {} else "") - + result.msg - + ". Changed: %s" % ", ".join(self._changed_items) - ) + return result._replace( + msg=result.msg + ". Changed: %s" % ", ".join(self._changed_items) + ) def delete(self): if self.module.check_mode: From 5044c03e06b1bcf73cb16639d7b8a5a52ebc65cf Mon Sep 17 00:00:00 2001 From: Michael Sekania Date: Mon, 25 Mar 2024 20:51:58 +0100 Subject: [PATCH 74/76] fix: typo --- plugins/modules/host.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/host.py b/plugins/modules/host.py index 68f80d79a..e232d9887 100644 --- a/plugins/modules/host.py +++ b/plugins/modules/host.py @@ -397,7 +397,7 @@ def _detect_changes(self): self.module.fail_json( msg="ERROR: The folder parameter is different from the folder in which the host is located, while other parameters are also specified!\n \ If you want to move the host to a specific folder, please omit the other parameters: \ - 'attributes', 'update_attributes' and 'remove_attributes'". + 'attributes', 'update_attributes' and 'remove_attributes'.", exception=e, ) else: From b7caa61fa6f8ec906ab8285d3917ddb08bad7ddd Mon Sep 17 00:00:00 2001 From: Robin Gierse Date: Tue, 26 Mar 2024 15:50:07 -0400 Subject: [PATCH 75/76] Bugfix changelog. --- changelogs/archive/4.3.0/lookups.yml | 2 +- changelogs/changelog.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelogs/archive/4.3.0/lookups.yml b/changelogs/archive/4.3.0/lookups.yml index 1c8d0172f..f1006663a 100644 --- a/changelogs/archive/4.3.0/lookups.yml +++ b/changelogs/archive/4.3.0/lookups.yml @@ -6,4 +6,4 @@ minor_changes: known_issues: - Lookup modules - When using inventory variables to configure e.g., the server_url, it is not possible to assign other variables to these variables. - This is a limitation of Ansble itself. + This is a limitation of Ansible itself. diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index f4e51c674..cbbe09c18 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -837,7 +837,7 @@ releases: known_issues: - Lookup modules - When using inventory variables to configure e.g., the server_url, it is not possible to assign other variables to these variables. This is a - limitation of Ansble itself. + limitation of Ansible itself. minor_changes: - Folder module - Extend attribute management. Please refer to the module documentation for more details. From 687d90ead2d81bf086aa6551508ca06cb99b912f Mon Sep 17 00:00:00 2001 From: Robin Gierse Date: Tue, 26 Mar 2024 15:50:37 -0400 Subject: [PATCH 76/76] Bugfix lookup module examples. --- plugins/lookup/README.md | 12 ++++++------ plugins/lookup/bakery.py | 12 ++++++------ plugins/lookup/folder.py | 8 ++++---- plugins/lookup/folders.py | 8 ++++---- plugins/lookup/host.py | 8 ++++---- plugins/lookup/hosts.py | 8 ++++---- plugins/lookup/rule.py | 8 ++++---- plugins/lookup/rules.py | 8 ++++---- plugins/lookup/ruleset.py | 8 ++++---- plugins/lookup/rulesets.py | 8 ++++---- plugins/lookup/version.py | 8 ++++---- roles/server/README.md | 6 +++--- roles/server/defaults/main.yml | 2 +- 13 files changed, 52 insertions(+), 52 deletions(-) diff --git a/plugins/lookup/README.md b/plugins/lookup/README.md index c5b855619..e711ed070 100644 --- a/plugins/lookup/README.md +++ b/plugins/lookup/README.md @@ -6,8 +6,8 @@ This way, they do not need to be provided at task level. ### Method 1: Environment variables ```bash -export ANSIBLE_LOOKUP_CHECKMK_SERVER_URL="https://myserver" -export ANSIBLE_LOOKUP_CHECKMK_SITE=mysite +export ANSIBLE_LOOKUP_CHECKMK_SERVER_URL="https://my_server" +export ANSIBLE_LOOKUP_CHECKMK_SITE=my_site export ANSIBLE_LOOKUP_AUTOMATION_USER=automation export ANSIBLE_LOOKUP_AUTOMATION_SECRET=mysecret export ANSIBLE_LOOKUP_VALIDATE_CERTS=False @@ -16,8 +16,8 @@ export ANSIBLE_LOOKUP_VALIDATE_CERTS=False ### Method 2: In `ansible.cfg` ```ini [checkmk_lookup] -server_url = https://myserver -site = mysite +server_url = https://my_server +site = my_site automation_user = automation automation_secret = mysecret validate_certs = False @@ -30,8 +30,8 @@ validate_certs = False hosts: localhost gather_facts: false vars: - ansible_lookup_checkmk_server_url: "https://myserver" - ansible_lookup_checkmk_site: "mysite" + ansible_lookup_checkmk_server_url: "https://my_server" + ansible_lookup_checkmk_site: "my_site" ansible_lookup_automation_user: "automation" ansible_lookup_automation_secret: "mysecret" ansible_lookup_validate_certs: false diff --git a/plugins/lookup/bakery.py b/plugins/lookup/bakery.py index 6308bb351..bf56d9c36 100644 --- a/plugins/lookup/bakery.py +++ b/plugins/lookup/bakery.py @@ -89,8 +89,8 @@ msg: "Bakery status is {{ bakery }}" vars: bakery: "{{ lookup('checkmk.general.bakery', - server_url=http://myserver, - site=mysite, + server_url=http://my_server, + site=my_site, validate_certs=False, automation_user=automation_user, automation_secret=automation_secret @@ -100,10 +100,10 @@ ansible.builtin.debug: msg: "Bakery status is {{ bakery }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false bakery: "{{ lookup('checkmk.general.bakery') }}" """ diff --git a/plugins/lookup/folder.py b/plugins/lookup/folder.py index a2b5db1cd..b7c849da4 100644 --- a/plugins/lookup/folder.py +++ b/plugins/lookup/folder.py @@ -107,10 +107,10 @@ ansible.builtin.debug: msg: "Attributes of folder /network: {{ attributes }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false attributes: "{{ lookup('checkmk.general.folder', '~tests') }}" """ diff --git a/plugins/lookup/folders.py b/plugins/lookup/folders.py index 4a18d7aaf..aafcee525 100644 --- a/plugins/lookup/folders.py +++ b/plugins/lookup/folders.py @@ -143,10 +143,10 @@ ansible.builtin.debug: msg: "Folder tree: {{ item.id }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false loop: "{{ lookup('checkmk.general.folders', diff --git a/plugins/lookup/host.py b/plugins/lookup/host.py index af9a191fc..e395c95d1 100644 --- a/plugins/lookup/host.py +++ b/plugins/lookup/host.py @@ -114,10 +114,10 @@ ansible.builtin.debug: msg: "Attributes of host example: {{ attributes }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false attributes: "{{ lookup('checkmk.general.host', 'example.com', effective_attributes=True) }}" """ diff --git a/plugins/lookup/hosts.py b/plugins/lookup/hosts.py index 50756e84a..e1acfd9c6 100644 --- a/plugins/lookup/hosts.py +++ b/plugins/lookup/hosts.py @@ -111,10 +111,10 @@ ansible.builtin.debug: msg: "Host: {{ item.id }} in folder {{ item.extensions.folder }}, IP: {{ item.extensions.effective_attributes.ipaddress }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false loop: "{{ lookup('checkmk.general.hosts', effective_attributes=True) }}" diff --git a/plugins/lookup/rule.py b/plugins/lookup/rule.py index 105f39034..95cd0926a 100644 --- a/plugins/lookup/rule.py +++ b/plugins/lookup/rule.py @@ -107,10 +107,10 @@ ansible.builtin.debug: msg: "Rule: {{ extensions }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false attributes: "{{ lookup('checkmk.general.rule', rule_id='a9285bc1-dcaf-45e0-a3ba-ad398ef06a49') }}" """ diff --git a/plugins/lookup/rules.py b/plugins/lookup/rules.py index c36349ea7..1395964b8 100644 --- a/plugins/lookup/rules.py +++ b/plugins/lookup/rules.py @@ -136,10 +136,10 @@ ansible.builtin.debug: msg: "Rule: {{ item.extensions }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false loop: "{{ lookup('checkmk.general.rules', ruleset='host_groups') }}" diff --git a/plugins/lookup/ruleset.py b/plugins/lookup/ruleset.py index 790fd7db9..47f358bf9 100644 --- a/plugins/lookup/ruleset.py +++ b/plugins/lookup/ruleset.py @@ -107,10 +107,10 @@ ansible.builtin.debug: msg: "Ruleset: {{ extensions }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false extensions: "{{ lookup('checkmk.general.ruleset', ruleset='host_groups') }}" """ diff --git a/plugins/lookup/rulesets.py b/plugins/lookup/rulesets.py index 10acc985d..6594360b5 100644 --- a/plugins/lookup/rulesets.py +++ b/plugins/lookup/rulesets.py @@ -146,10 +146,10 @@ ansible.builtin.debug: msg: "Ruleset {{ item.extension.name }} is deprecated." vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false loop: "{{ lookup('checkmk.general.rulesets', regex='', rulesets_deprecated=True, rulesets_used=True) }}" diff --git a/plugins/lookup/version.py b/plugins/lookup/version.py index a6949ef22..294df07be 100644 --- a/plugins/lookup/version.py +++ b/plugins/lookup/version.py @@ -100,10 +100,10 @@ ansible.builtin.debug: msg: "Server version is {{ version }}" vars: - ansible_lookup_checkmk_server_url: "{{ checkmk_var_server_url }}" - ansible_lookup_checkmk_site: "{{ outer_item.site }}" - ansible_lookup_checkmk_automation_user: "{{ checkmk_var_automation_user }}" - ansible_lookup_checkmk_automation_secret: "{{ checkmk_var_automation_secret }}" + ansible_lookup_checkmk_server_url: "http://my_server/" + ansible_lookup_checkmk_site: "my_site" + ansible_lookup_checkmk_automation_user: "my_user + ansible_lookup_checkmk_automation_secret: "my_secret" ansible_lookup_checkmk_validate_certs: false attributes: "{{ lookup('checkmk.general.version') }}" """ diff --git a/roles/server/README.md b/roles/server/README.md index f4e69f6aa..864eda075 100644 --- a/roles/server/README.md +++ b/roles/server/README.md @@ -42,7 +42,7 @@ The edition you want to use. Valid values are `cre`, `cfe`, `cee`, `cce` and `cm For details about the editions see: https://checkmk.com/product/editions -Note, that you need credentials, to download the following editions: `cee` and `cme`. +Note, that you need credentials, to download the following editions: `cee` and `cme`. See below variables, to set those. checkmk_server_download_user: [] @@ -73,7 +73,7 @@ Whether to allow downgrading a site's version. Note: this is not a recommended procedure, and will not be supported for enterprise customers. checkmk_server_sites: - - name: mysite + - name: my_site version: "{{ checkmk_server_version }}" update_conflict_resolution: abort state: started @@ -99,7 +99,7 @@ Valid choices include `install`, `keepold` and `abort`. Site configuration can be passed with the `omd_config` keyword. The format can be seen above, for a list of variables run `omd show` -on an existing site. +on an existing site. **Pay special attention to the `omd_auto_restart` variable!** As site configuration needs the site to be stopped, this needs to be handled. By default the variable is set to `false` to avoid unexpected restarting. However, no configuration will be performed if the site is started. checkmk_server_backup_on_update: 'true' diff --git a/roles/server/defaults/main.yml b/roles/server/defaults/main.yml index a7a084093..b9b7c2edd 100644 --- a/roles/server/defaults/main.yml +++ b/roles/server/defaults/main.yml @@ -32,7 +32,7 @@ checkmk_server_download_user: [] checkmk_server_download_pass: [] checkmk_server_sites: [] -# - name: mysite +# - name: my_site # version: "{{ checkmk_server_version }}" # state: started # admin_pw: "{{ automation_secret | default(omit) }}"