diff options
author | Filippo125 <filippo.ferrazini@gmail.com> | 2018-12-01 01:49:57 +0100 |
---|---|---|
committer | René Moser <mail@renemoser.net> | 2018-12-01 01:49:57 +0100 |
commit | 238786c0d397edc8a7cafd8e332a720cb765a245 (patch) | |
tree | f3f4c7d3386e9385f40656a9b5b5c54c7d4dd16e /contrib | |
parent | 45e41f5a3de1a918290d8fce4e5b1470d75c0961 (diff) | |
download | ansible-238786c0d397edc8a7cafd8e332a720cb765a245.tar.gz |
zabbix inventory: options to read per each host and set ansible_ssh_host (#44107)
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/inventory/zabbix.ini | 8 | ||||
-rwxr-xr-x | contrib/inventory/zabbix.py | 60 |
2 files changed, 59 insertions, 9 deletions
diff --git a/contrib/inventory/zabbix.ini b/contrib/inventory/zabbix.ini index ac12c1b497..ead19b62d5 100644 --- a/contrib/inventory/zabbix.ini +++ b/contrib/inventory/zabbix.ini @@ -11,4 +11,10 @@ username = admin password = zabbix # Verify the server's SSL certificate -validate_certs = True
\ No newline at end of file +validate_certs = True + +# Read zabbix inventory per host +read_host_inventory = True + +# Set ansible_ssh_host based on first interface settings +use_host_interface = True
\ No newline at end of file diff --git a/contrib/inventory/zabbix.py b/contrib/inventory/zabbix.py index 86769ea778..943a53bd03 100755 --- a/contrib/inventory/zabbix.py +++ b/contrib/inventory/zabbix.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # (c) 2013, Greg Buehler +# (c) 2018, Filippo Ferrazini # # This file is part of Ansible, # @@ -29,7 +30,7 @@ name, use asterisk. For example --limit="Linux*servers". Configuration is read from `zabbix.ini`. -Tested with Zabbix Server 2.0.6 and 3.2.3. +Tested with Zabbix Server 2.0.6, 3.2.3 and 3.4. """ from __future__ import print_function @@ -74,6 +75,14 @@ class ZabbixInventory(object): if config.has_option('zabbix', 'validate_certs'): if config.get('zabbix', 'validate_certs') in ['false', 'False', False]: self.validate_certs = False + # host inventory + if config.has_option('zabbix', 'read_host_inventory'): + if config.get('zabbix', 'read_host_inventory') in ['true', 'True', True]: + self.read_host_inventory = True + # host interface + if config.has_option('zabbix', 'use_host_interface'): + if config.get('zabbix', 'use_host_interface') in ['false', 'False', False]: + self.use_host_interface = False def read_cli(self): parser = argparse.ArgumentParser() @@ -87,17 +96,43 @@ class ZabbixInventory(object): } def get_host(self, api, name): + api_query = {'output': 'extend', 'selectGroups': 'extend', "filter": {"host": [name]}} + if self.use_host_interface: + api_query['selectInterfaces'] = ['useip', 'ip', 'dns'] + if self.read_host_inventory: + api_query['selectInventory'] = "extend" + data = {'ansible_ssh_host': name} + if self.use_host_interface or self.read_host_inventory: + try: + hosts_data = api.host.get(api_query)[0] + 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'] + if ('inventory' in hosts_data) and (hosts_data['inventory']): + data.update(hosts_data['inventory']) + except IndexError: + # Host not found in zabbix + pass return data def get_list(self, api): - hostsData = api.host.get({'output': 'extend', 'selectGroups': 'extend'}) + api_query = {'output': 'extend', 'selectGroups': 'extend'} + if self.use_host_interface: + api_query['selectInterfaces'] = ['useip', 'ip', 'dns'] + if self.read_host_inventory: + api_query['selectInventory'] = "extend" - data = {} - data[self.defaultgroup] = self.hoststub() + hosts_data = api.host.get(api_query) + data = {'_meta': {'hostvars': {}}} - for host in hostsData: + data[self.defaultgroup] = self.hoststub() + for host in hosts_data: hostname = host['name'] + hostvars = dict() data[self.defaultgroup]['hosts'].append(hostname) for group in host['groups']: @@ -107,9 +142,15 @@ class ZabbixInventory(object): data[groupname] = self.hoststub() data[groupname]['hosts'].append(hostname) - - # Prevents Ansible from calling this script for each server with --host - data['_meta'] = {'hostvars': self.meta} + 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'] + if ('inventory' in host) and (host['inventory']): + hostvars.update(host['inventory']) + data['_meta']['hostvars'][hostname] = hostvars return data @@ -120,6 +161,9 @@ class ZabbixInventory(object): self.zabbix_username = None self.zabbix_password = None self.validate_certs = True + self.read_host_inventory = False + self.use_host_interface = True + self.meta = {} self.read_settings() |