From 93b1fc1c124c4dd245c1f354f175b2614a00ab39 Mon Sep 17 00:00:00 2001 From: ONODERA Masaru <46081939+masa-orca@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:06:33 +0900 Subject: [PATCH] Don't reset IPMI setting when update inventory (#1162) * Don't reset IPMI setting when update inventory * Update changelog comment and modify module --- .../1162-do_not_update_ipmi_options.yml | 2 + plugins/modules/zabbix_host.py | 57 ++++++------------- .../tasks/zabbix_host_tests.yml | 55 ++++++++++++++++++ 3 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 changelogs/fragments/1162-do_not_update_ipmi_options.yml diff --git a/changelogs/fragments/1162-do_not_update_ipmi_options.yml b/changelogs/fragments/1162-do_not_update_ipmi_options.yml new file mode 100644 index 000000000..f85b7a77a --- /dev/null +++ b/changelogs/fragments/1162-do_not_update_ipmi_options.yml @@ -0,0 +1,2 @@ +bugfixes: + - zabbix_host - Don't reset IPMI setting when update inventory data of a host diff --git a/plugins/modules/zabbix_host.py b/plugins/modules/zabbix_host.py index 22e14d17d..4f8df8779 100644 --- a/plugins/modules/zabbix_host.py +++ b/plugins/modules/zabbix_host.py @@ -513,7 +513,7 @@ def add_host(self, host_name, group_ids, status, interfaces, proxy_id, visible_n def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_interface_list, proxy_id, visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, - tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, discovered_host): + tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, discovered_host, zabbix_host_obj): try: if self._module.check_mode: self._module.exit_json(changed=True) @@ -523,31 +523,31 @@ def update_host(self, host_name, group_ids, status, host_id, interfaces, exist_i else: # A "plain" host parameters = {"hostid": host_id, "groups": group_ids, "status": status} - if proxy_id >= 0: + if (proxy_id >= 0 and proxy_id != zabbix_host_obj["proxy_hostid"]): parameters["proxy_hostid"] = proxy_id - if visible_name: + if (visible_name is not None and visible_name != zabbix_host_obj["name"]): parameters["name"] = visible_name - if tls_connect: + if (tls_connect is not None and tls_connect != zabbix_host_obj["tls_connect"]): parameters["tls_connect"] = tls_connect - if tls_accept: + if (tls_accept is not None and tls_accept != zabbix_host_obj["tls_accept"]): parameters["tls_accept"] = tls_accept if tls_psk_identity: parameters["tls_psk_identity"] = tls_psk_identity if tls_psk: parameters["tls_psk"] = tls_psk - if tls_issuer: + if (tls_issuer is not None and tls_issuer != zabbix_host_obj["tls_issuer"]): parameters["tls_issuer"] = tls_issuer - if tls_subject: + if (tls_subject is not None and tls_subject != zabbix_host_obj["tls_subject"]): parameters["tls_subject"] = tls_subject - if description: + if (description is not None and description != zabbix_host_obj["description"]): parameters["description"] = description - if ipmi_authtype: + if (ipmi_authtype is not None and ipmi_authtype != zabbix_host_obj["ipmi_authtype"]): parameters["ipmi_authtype"] = ipmi_authtype - if ipmi_privilege: + if (ipmi_privilege is not None and ipmi_privilege != zabbix_host_obj["ipmi_privilege"]): parameters["ipmi_privilege"] = ipmi_privilege - if ipmi_username: + if (ipmi_username is not None and ipmi_username != zabbix_host_obj["ipmi_username"]): parameters["ipmi_username"] = ipmi_username - if ipmi_password: + if (ipmi_password is not None and ipmi_password != zabbix_host_obj["ipmi_password"]): parameters["ipmi_password"] = ipmi_password if interfaces: parameters["interfaces"] = interfaces @@ -809,8 +809,7 @@ def check_all_properties(self, host_id, group_ids, status, interfaces, template_ return False # link or clear template of the host - def link_or_clear_template(self, host_id, template_id_list, tls_connect, tls_accept, tls_psk_identity, tls_psk, - tls_issuer, tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, discovered_host): + def link_or_clear_template(self, host_id, template_id_list): # get host's exist template ids exist_template_id_list = self.get_host_templates_by_host_id(host_id) @@ -821,25 +820,7 @@ def link_or_clear_template(self, host_id, template_id_list, tls_connect, tls_acc # get unlink and clear templates templates_clear = exist_template_ids.difference(template_ids) templates_clear_list = list(templates_clear) - if discovered_host: - # The host was discovered via Discovery Rule - request_str = {"hostid": host_id, "templates": template_id_list, "templates_clear": templates_clear_list} - else: - # A "plain" host - request_str = {"hostid": host_id, "templates": template_id_list, "templates_clear": templates_clear_list, - "ipmi_authtype": ipmi_authtype, "ipmi_privilege": ipmi_privilege, "ipmi_username": ipmi_username, "ipmi_password": ipmi_password} - if tls_connect: - request_str["tls_connect"] = tls_connect - if tls_accept: - request_str["tls_accept"] = tls_accept - if tls_psk_identity is not None: - request_str["tls_psk_identity"] = tls_psk_identity - if tls_psk is not None: - request_str["tls_psk"] = tls_psk - if tls_issuer is not None: - request_str["tls_issuer"] = tls_issuer - if tls_subject is not None: - request_str["tls_subject"] = tls_subject + request_str = {"hostid": host_id, "templates": template_id_list, "templates_clear": templates_clear_list} try: if self._module.check_mode: self._module.exit_json(changed=True) @@ -1204,11 +1185,9 @@ def main(): host.update_host( host_name, group_ids, status, host_id, interfaces, exist_interfaces, proxy_id, visible_name, description, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, - ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, discovered_host) + ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags, discovered_host, zabbix_host_obj) - host.link_or_clear_template( - host_id, template_ids, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, - tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, discovered_host) + host.link_or_clear_template(host_id, template_ids) host.update_inventory_mode(host_id, inventory_mode) host.update_inventory_zabbix(host_id, inventory_zabbix) @@ -1235,9 +1214,7 @@ def main(): tls_psk_identity, tls_psk, tls_issuer, tls_subject, ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, macros, tags) - host.link_or_clear_template( - host_id, template_ids, tls_connect, tls_accept, tls_psk_identity, tls_psk, tls_issuer, tls_subject, - ipmi_authtype, ipmi_privilege, ipmi_username, ipmi_password, discovered_host) + host.link_or_clear_template(host_id, template_ids) host.update_inventory_mode(host_id, inventory_mode) host.update_inventory_zabbix(host_id, inventory_zabbix) diff --git a/tests/integration/targets/test_zabbix_host/tasks/zabbix_host_tests.yml b/tests/integration/targets/test_zabbix_host/tasks/zabbix_host_tests.yml index a4829a051..1f3318764 100644 --- a/tests/integration/targets/test_zabbix_host/tasks/zabbix_host_tests.yml +++ b/tests/integration/targets/test_zabbix_host/tasks/zabbix_host_tests.yml @@ -1348,6 +1348,61 @@ ansible.builtin.assert: that: zbx_host_create_interfaceless is not changed +- name: "test: attempt to delete host created earlier" + community.zabbix.zabbix_host: + host_name: ExampleHost + state: absent + register: zabbix_host1 + +- name: deleting a host is a change, right? + ansible.builtin.assert: + that: + - "zabbix_host1 is changed" + +- name: "test: create host with IPMI values" + community.zabbix.zabbix_host: + host_name: ExampleHost + host_groups: + - Linux servers + - Zabbix servers + ipmi_authtype: 1 + ipmi_privilege: 1 + ipmi_username: "test" + ipmi_password: "test" + register: zabbix_ipmi_host + +- name: expect to succeed and that things have changed + ansible.builtin.assert: + that: + - "zabbix_ipmi_host is changed" + +- name: "test: update inventory of the created host" + community.zabbix.zabbix_host: + host_name: ExampleHost + inventory_mode: manual + inventory_zabbix: + notes: "Update inventory" + register: zabbix_ipmi_host + +- name: expect to succeed and that things have changed + ansible.builtin.assert: + that: + - "zabbix_ipmi_host is changed" + +- name: "test: create host with IPMI values without changes" + community.zabbix.zabbix_host: + host_name: ExampleHost + ipmi_authtype: 1 + ipmi_privilege: 1 + ipmi_username: "test" + ipmi_password: "test" + register: zabbix_ipmi_host + +- name: expect to succeed and that things have not changed + ansible.builtin.assert: + that: + - "zabbix_ipmi_host is not changed" + - name: "cleanup" community.zabbix.zabbix_host: host_name: ExampleHost