Zabbix: Handle KeyError in zabbix_host module (#65392)

Fixes: #65304
This commit is contained in:
sky-joker 2019-12-16 17:02:12 +09:00 committed by Abhijeet Kasurde
parent 06d997b2b2
commit 7b2cfdacd0
3 changed files with 40 additions and 4 deletions

View file

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

View file

@ -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'])

View file

@ -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 }}"