Zabbix: Handle KeyError in zabbix_host module (#65392)

Fixes: #65304
pull/65868/head
sky-joker 5 years ago committed by Abhijeet Kasurde
parent 06d997b2b2
commit 7b2cfdacd0

@ -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).

@ -287,6 +287,7 @@ except ImportError:
ZBX_IMP_ERR = traceback.format_exc() ZBX_IMP_ERR = traceback.format_exc()
HAS_ZABBIX_API = False HAS_ZABBIX_API = False
from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule, missing_required_lib
@ -294,6 +295,7 @@ class Host(object):
def __init__(self, module, zbx): def __init__(self, module, zbx):
self._module = module self._module = module
self._zapi = zbx self._zapi = zbx
self._zbx_api_version = zbx.api_version()[:5]
# exist host # exist host
def is_host_exist(self, host_name): def is_host_exist(self, host_name):
@ -542,11 +544,15 @@ class Host(object):
return True return True
if inventory_mode: if inventory_mode:
if LooseVersion(self._zbx_api_version) <= LooseVersion('4.4.0'):
if host['inventory']: if host['inventory']:
if int(host['inventory']['inventory_mode']) != self.inventory_mode_numeric(inventory_mode): if int(host['inventory']['inventory_mode']) != self.inventory_mode_numeric(inventory_mode):
return True return True
elif inventory_mode != 'disabled': elif inventory_mode != 'disabled':
return True return True
else:
if int(host['inventory_mode']) != self.inventory_mode_numeric(inventory_mode):
return True
if inventory_zabbix: if inventory_zabbix:
proposed_inventory = copy.deepcopy(host['inventory']) proposed_inventory = copy.deepcopy(host['inventory'])

@ -804,6 +804,34 @@
that: that:
- "zabbix_host1 is not changed" - "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" - name: "test: attempt to delete host created earlier"
zabbix_host: zabbix_host:
server_url: "{{ zabbix_server_url }}" server_url: "{{ zabbix_server_url }}"

Loading…
Cancel
Save