Skip to content

Commit

Permalink
fixing bug for a empty interfaces list (#827)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Mawiguk0 and Markus Kohn authored Oct 24, 2022
1 parent e078990 commit f91d5fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 16 additions & 10 deletions scripts/inventory/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f91d5fa

Please sign in to comment.