zabbix inventory: options to read per each host and set ansible_ssh_host (#44107)

This commit is contained in:
Filippo125 2018-12-01 01:49:57 +01:00 committed by René Moser
parent 45e41f5a3d
commit 238786c0d3
3 changed files with 63 additions and 9 deletions

View file

@ -0,0 +1,4 @@
---
minor_changes:
- Add option to read zabbix inventory per each host
- Add option to set ansible_ssh_host based on first interface settings

View file

@ -11,4 +11,10 @@ username = admin
password = zabbix
# Verify the server's SSL certificate
validate_certs = True
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

View file

@ -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"
hosts_data = api.host.get(api_query)
data = {'_meta': {'hostvars': {}}}
data = {}
data[self.defaultgroup] = self.hoststub()
for host in hostsData:
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()