diff options
author | Dusan Matejka <matejkadusan32@gmail.com> | 2020-08-27 19:12:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-27 12:12:40 -0500 |
commit | aa698ba9b6b6b39c5bb75ace3a155d57f4524401 (patch) | |
tree | 90f011dcb85cc0d8ef66c4ae5959a73870aca967 | |
parent | 36fbd2d9fd8bd8ffb3da6732a4ca49956956f2e2 (diff) | |
download | ansible-aa698ba9b6b6b39c5bb75ace3a155d57f4524401.tar.gz |
Zabbix: Handle KeyError in zabbix_host module (#65392) (#71288)
Fixes: #65304
(cherry picked from commit 7b2cfdacd00ddf907247270d228a6bf5f72258a1)
Co-authored-by: sky-joker <sky.jokerxx@gmail.com>
3 files changed, 40 insertions, 4 deletions
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 0000000000..afe6744c76 --- /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 0b30edcc48..197a8970da 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_host.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_host.py @@ -310,6 +310,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 @@ -317,6 +318,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): @@ -565,11 +567,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 af95251908..be44e1ba14 100644 --- a/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml +++ b/test/integration/targets/zabbix_host/tasks/zabbix_host_tests.yml @@ -680,6 +680,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 }}" |