From f91d5fa813dbf36ec6fe46ee9aaf74996b1a2eff Mon Sep 17 00:00:00 2001 From: Mawiguk0 Date: Mon, 24 Oct 2022 09:32:29 +0200 Subject: [PATCH] fixing bug for a empty interfaces list (#827) * fixing bug for a empty interfaces list * sanatized the possibilty that zabbix api will return no interfaces element * Adding Changelog Fragment for conformity with the ansible development cycle * added issue for reference Co-authored-by: Markus Kohn --- ...7-fixing-bug-for-empty-interfaces-list.yml | 3 +++ scripts/inventory/zabbix.py | 26 ++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/827-fixing-bug-for-empty-interfaces-list.yml diff --git a/changelogs/fragments/827-fixing-bug-for-empty-interfaces-list.yml b/changelogs/fragments/827-fixing-bug-for-empty-interfaces-list.yml new file mode 100644 index 000000000..bdba877fd --- /dev/null +++ b/changelogs/fragments/827-fixing-bug-for-empty-interfaces-list.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - The inventory script had insufficient error handling in case the Zabbix API provided an empty interfaces list. This bugfix checks for an exisiting interfaces element, then for the minimal length of 1 so that the first interface will only be accessed when it really exists in the api response. (https://github.com/ansible-collections/community.zabbix/issues/826) diff --git a/scripts/inventory/zabbix.py b/scripts/inventory/zabbix.py index e455a3765..767012848 100644 --- a/scripts/inventory/zabbix.py +++ b/scripts/inventory/zabbix.py @@ -103,12 +103,15 @@ def get_host(self, api, name): if self.use_host_interface or self.read_host_inventory: try: hosts_data = api.host.get(api_query)[0] + # check if zabbix api returned a interfaces element if 'interfaces' in hosts_data: - # use first interface only - if hosts_data['interfaces'][0]['useip'] == 0: - data['ansible_ssh_host'] = hosts_data['interfaces'][0]['dns'] - else: - data['ansible_ssh_host'] = hosts_data['interfaces'][0]['ip'] + # check for a interfaces list that contains at least interface + if len(hosts_data['interfaces']) >= 1: + # use first interface only + if hosts_data['interfaces'][0]['useip'] == 0: + data['ansible_ssh_host'] = hosts_data['interfaces'][0]['dns'] + else: + data['ansible_ssh_host'] = hosts_data['interfaces'][0]['ip'] if ('inventory' in hosts_data) and (hosts_data['inventory']): data.update(hosts_data['inventory']) except IndexError: @@ -139,12 +142,15 @@ def get_list(self, api): data[groupname] = self.hoststub() data[groupname]['hosts'].append(hostname) + # check if zabbix api returned a interfaces element if 'interfaces' in host: - # use first interface only - if host['interfaces'][0]['useip'] == 0: - hostvars['ansible_ssh_host'] = host['interfaces'][0]['dns'] - else: - hostvars['ansible_ssh_host'] = host['interfaces'][0]['ip'] + # check for a interfaces list that contains at least interface + if len(host['interfaces']) >= 1: + # use first interface only + if host['interfaces'][0]['useip'] == 0: + hostvars['ansible_ssh_host'] = host['interfaces'][0]['dns'] + else: + hostvars['ansible_ssh_host'] = host['interfaces'][0]['ip'] if ('inventory' in host) and (host['inventory']): hostvars.update(host['inventory']) data['_meta']['hostvars'][hostname] = hostvars