Zabbix facts (#41084)

* added validate_certs option to zabbix_*_facts modules and fixed documentation to use doc fragment

* removed code duplication from zabbix_host_facts module
This commit is contained in:
Dusan Matejka 2018-06-04 22:41:09 +02:00 committed by ansibot
parent 457c813d46
commit 6ef2ffe310
2 changed files with 26 additions and 82 deletions

View file

@ -34,38 +34,13 @@ requirements:
- "python >= 2.6"
- zabbix-api
options:
server_url:
description:
- Url of Zabbix server, with protocol (http or https).
required: true
aliases: [ "url" ]
login_user:
description:
- Zabbix user name, used to authenticate against the server.
required: true
login_password:
description:
- Zabbix user password.
required: true
http_login_user:
description:
- Basic Auth login
required: false
default: null
http_login_password:
description:
- Basic Auth password
required: false
default: null
hostgroup_name:
description:
- Name of the hostgroup in Zabbix.
- hostgroup is the unique identifier used and cannot be updated using this module.
required: true
timeout:
description:
- The timeout of API request (seconds).
default: 10
extends_documentation_fragment:
- zabbix
'''
EXAMPLES = '''
@ -92,8 +67,8 @@ try:
class ZabbixAPIExtends(ZabbixAPI):
hostinterface = None
def __init__(self, server, timeout, user, passwd, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
def __init__(self, server, timeout, user, passwd, validate_certs, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd, validate_certs=validate_certs)
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
HAS_ZABBIX_API = True
@ -122,6 +97,7 @@ def main():
hostgroup_name=dict(type='list', required=True),
http_login_user=dict(type='str', required=False, default=None),
http_login_password=dict(type='str', required=False, default=None, no_log=True),
validate_certs=dict(type='bool', required=False, default=True),
timeout=dict(type='int', default=10)
),
supports_check_mode=True
@ -135,13 +111,15 @@ def main():
login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
validate_certs = module.params['validate_certs']
hostgroup_name = module.params['hostgroup_name']
timeout = module.params['timeout']
zbx = None
# login to zabbix
try:
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs)
zbx.login(login_user, login_password)
except Exception as e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

View file

@ -35,29 +35,6 @@ requirements:
- "python >= 2.6"
- zabbix-api
options:
server_url:
description:
- Url of Zabbix server, with protocol (http or https).
required: true
aliases: [ "url" ]
login_user:
description:
- Zabbix user name, used to authenticate against the server.
required: true
login_password:
description:
- Zabbix user password.
required: true
http_login_user:
description:
- Basic Auth login.
required: false
default: null
http_login_password:
description:
- Basic Auth password.
required: false
default: null
host_name:
description:
- Name of the host in Zabbix.
@ -67,10 +44,6 @@ options:
description:
- Host interface IP of the host in Zabbix.
required: false
timeout:
description:
- The timeout of API request (seconds).
default: 10
exact_match:
description:
- Find the exact match
@ -81,6 +54,8 @@ options:
- Remove duplicate host from host result
type: bool
default: yes
extends_documentation_fragment:
- zabbix
'''
EXAMPLES = '''
@ -109,8 +84,8 @@ try:
class ZabbixAPIExtends(ZabbixAPI):
hostinterface = None
def __init__(self, server, timeout, user, passwd, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
def __init__(self, server, timeout, user, passwd, validate_certs, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd, validate_certs=validate_certs)
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
HAS_ZABBIX_API = True
@ -123,14 +98,6 @@ class Host(object):
self._module = module
self._zapi = zbx
def is_host_exist(self, host_name, exact_match):
""" Check host exists """
search_key = 'search'
if exact_match:
search_key = 'filter'
result = self._zapi.host.get({search_key: {'host': host_name}})
return result
def get_hosts_by_host_name(self, host_name, exact_match):
""" Get host by host name """
search_key = 'search'
@ -186,6 +153,7 @@ def main():
host_ip=dict(type='list', default=[], required=False),
http_login_user=dict(type='str', required=False, default=None),
http_login_password=dict(type='str', required=False, default=None, no_log=True),
validate_certs=dict(type='bool', required=False, default=True),
timeout=dict(type='int', default=10),
exact_match=dict(type='bool', required=False, default=False),
remove_duplicate=dict(type='bool', required=False, default=True)
@ -201,6 +169,7 @@ def main():
login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
validate_certs = module.params['validate_certs']
host_name = module.params['host_name']
host_ips = module.params['host_ip']
timeout = module.params['timeout']
@ -210,7 +179,8 @@ def main():
zbx = None
# login to zabbix
try:
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs)
zbx.login(login_user, login_password)
except Exception as e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)
@ -218,21 +188,17 @@ def main():
host = Host(module, zbx)
if host_name:
is_host_exist = host.is_host_exist(host_name, exact_match)
hosts = host.get_hosts_by_host_name(host_name, exact_match)
if is_remove_duplicate:
hosts = host.delete_duplicate_hosts(hosts)
extended_hosts = []
for zabbix_host in hosts:
zabbix_host['hostinterfaces'] = host._zapi.hostinterface.get({
'output': 'extend', 'hostids': zabbix_host['hostid']
})
extended_hosts.append(zabbix_host)
module.exit_json(ok=True, hosts=extended_hosts)
if is_host_exist:
hosts = host.get_hosts_by_host_name(host_name, exact_match)
if is_remove_duplicate:
hosts = host.delete_duplicate_hosts(hosts)
extended_hosts = []
for zabbix_host in hosts:
zabbix_host['hostinterfaces'] = host._zapi.hostinterface.get({
'output': 'extend', 'hostids': zabbix_host['hostid']
})
extended_hosts.append(zabbix_host)
module.exit_json(ok=True, hosts=extended_hosts)
else:
module.exit_json(ok=False, hosts=[], result="No Host present")
elif host_ips:
extended_hosts = host.get_hosts_by_ip(host_ips)
if is_remove_duplicate: