diff --git a/changelogs/fragments/65304-fix_zabbix_host_inventory_mode_key_error.yml b/changelogs/fragments/65304-fix_zabbix_host_inventory_mode_key_error.yml new file mode 100644 index 00000000000..afe6744c76b --- /dev/null +++ b/changelogs/fragments/65304-fix_zabbix_host_inventory_mode_key_error.yml @@ -0,0 +1,2 @@ +bugfixes: + - zabbix_host - fixed inventory_mode key error, which occurs with Zabbix 4.4.1 or more (https://github.com/ansible/ansible/issues/65304). diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_host.py b/lib/ansible/modules/monitoring/zabbix/zabbix_host.py index 69f669d10fa..6d9d08d703a 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_host.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_host.py @@ -287,6 +287,7 @@ except ImportError: ZBX_IMP_ERR = traceback.format_exc() HAS_ZABBIX_API = False +from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule, missing_required_lib @@ -294,6 +295,7 @@ class Host(object): def __init__(self, module, zbx): self._module = module self._zapi = zbx + self._zbx_api_version = zbx.api_version()[:5] # exist host def is_host_exist(self, host_name): @@ -542,11 +544,15 @@ class Host(object): return True if inventory_mode: - if host['inventory']: - if int(host['inventory']['inventory_mode']) != self.inventory_mode_numeric(inventory_mode): + if LooseVersion(self._zbx_api_version) <= LooseVersion('4.4.0'): + if host['inventory']: + if int(host['inventory']['inventory_mode']) != self.inventory_mode_numeric(inventory_mode): + return True + elif inventory_mode != 'disabled': + return True + else: + if int(host['inventory_mode']) != self.inventory_mode_numeric(inventory_mode): return True - elif inventory_mode != 'disabled': - return True if inventory_zabbix: proposed_inventory = copy.deepcopy(host['inventory']) diff --git a/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml b/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml index 1f51677bea3..62f0cb5189c 100644 --- a/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml +++ b/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml @@ -804,6 +804,34 @@ that: - "zabbix_host1 is not changed" +- name: "test: change host inventory mode to disabled" + zabbix_host: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + host_name: ExampleHost + inventory_mode: disabled + register: zabbix_host1 + +- name: expect to succeed and that things have changed + assert: + that: + - "zabbix_host1 is changed" + +- name: "test: change host inventory mode to manual" + zabbix_host: + server_url: "{{ zabbix_server_url }}" + login_user: "{{ zabbix_login_user }}" + login_password: "{{ zabbix_login_password }}" + host_name: ExampleHost + inventory_mode: manual + register: zabbix_host1 + +- name: expect to succeed and that things have changed + assert: + that: + - "zabbix_host1 is changed" + - name: "test: attempt to delete host created earlier" zabbix_host: server_url: "{{ zabbix_server_url }}"