Add option to use ZabbixApi via auth basic protection

This commit is contained in:
Julien Recurt 2016-03-25 19:00:17 +01:00
parent 6978984244
commit cd1114a2bd
5 changed files with 97 additions and 9 deletions

View file

@ -49,6 +49,18 @@ options:
description: description:
- Zabbix user password. - Zabbix user password.
required: true required: true
http_login_user:
description:
- Basic Auth login
required: false
default: None
version_added: "2.1"
http_login_password:
description:
- Basic Auth password
required: false
default: None
version_added: "2.1"
state: state:
description: description:
- Create or delete host group. - Create or delete host group.
@ -153,6 +165,8 @@ def main():
server_url=dict(type='str', required=True, aliases=['url']), server_url=dict(type='str', required=True, aliases=['url']),
login_user=dict(type='str', required=True), login_user=dict(type='str', required=True),
login_password=dict(type='str', required=True, no_log=True), login_password=dict(type='str', required=True, no_log=True),
http_login_user=dict(type='str',required=False, default=None),
http_login_password=dict(type='str',required=False, default=None, no_log=True),
host_groups=dict(type='list', required=True, aliases=['host_group']), host_groups=dict(type='list', required=True, aliases=['host_group']),
state=dict(default="present", choices=['present','absent']), state=dict(default="present", choices=['present','absent']),
timeout=dict(type='int', default=10) timeout=dict(type='int', default=10)
@ -166,6 +180,8 @@ def main():
server_url = module.params['server_url'] server_url = module.params['server_url']
login_user = module.params['login_user'] login_user = module.params['login_user']
login_password = module.params['login_password'] login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
host_groups = module.params['host_groups'] host_groups = module.params['host_groups']
state = module.params['state'] state = module.params['state']
timeout = module.params['timeout'] timeout = module.params['timeout']
@ -174,7 +190,7 @@ def main():
# login to zabbix # login to zabbix
try: try:
zbx = ZabbixAPI(server_url, timeout=timeout) zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
except Exception, e: except Exception, e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

View file

@ -47,6 +47,18 @@ options:
description: description:
- Zabbix user password. - Zabbix user password.
required: true required: true
http_login_user:
description:
- Basic Auth login
required: false
default: None
version_added: "2.1"
http_login_password:
description:
- Basic Auth password
required: false
default: None
version_added: "2.1"
host_name: host_name:
description: description:
- Name of the host in Zabbix. - Name of the host in Zabbix.
@ -158,8 +170,8 @@ except ImportError:
class ZabbixAPIExtends(ZabbixAPI): class ZabbixAPIExtends(ZabbixAPI):
hostinterface = None hostinterface = None
def __init__(self, server, timeout, **kwargs): def __init__(self, server, timeout, user, passwd, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout) ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs)) self.hostinterface = ZabbixAPISubClass(self, dict({"prefix": "hostinterface"}, **kwargs))
@ -405,6 +417,8 @@ def main():
login_user=dict(rtype='str', equired=True), login_user=dict(rtype='str', equired=True),
login_password=dict(type='str', required=True, no_log=True), login_password=dict(type='str', required=True, no_log=True),
host_name=dict(type='str', required=True), host_name=dict(type='str', 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),
host_groups=dict(type='list', required=False), host_groups=dict(type='list', required=False),
link_templates=dict(type='list', required=False), link_templates=dict(type='list', required=False),
status=dict(default="enabled", choices=['enabled', 'disabled']), status=dict(default="enabled", choices=['enabled', 'disabled']),
@ -424,6 +438,8 @@ def main():
server_url = module.params['server_url'] server_url = module.params['server_url']
login_user = module.params['login_user'] login_user = module.params['login_user']
login_password = module.params['login_password'] login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
host_name = module.params['host_name'] host_name = module.params['host_name']
host_groups = module.params['host_groups'] host_groups = module.params['host_groups']
link_templates = module.params['link_templates'] link_templates = module.params['link_templates']
@ -441,7 +457,7 @@ def main():
zbx = None zbx = None
# login to zabbix # login to zabbix
try: try:
zbx = ZabbixAPIExtends(server_url, timeout=timeout) zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
except Exception, e: except Exception, e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

View file

@ -46,6 +46,18 @@ options:
description: description:
- Zabbix user password. - Zabbix user password.
required: true required: true
http_login_user:
description:
- Basic Auth login
required: false
default: None
version_added: "2.1"
http_login_password:
description:
- Basic Auth password
required: false
default: None
version_added: "2.1"
host_name: host_name:
description: description:
- Name of the host. - Name of the host.
@ -171,6 +183,8 @@ def main():
server_url=dict(type='str', required=True, aliases=['url']), server_url=dict(type='str', required=True, aliases=['url']),
login_user=dict(type='str', required=True), login_user=dict(type='str', required=True),
login_password=dict(type='str', required=True, no_log=True), login_password=dict(type='str', required=True, no_log=True),
http_login_user=dict(type='str', required=False, default=None),
http_login_password=dict(type='str', required=False, default=None, no_log=True),
host_name=dict(type='str', required=True), host_name=dict(type='str', required=True),
macro_name=dict(type='str', required=True), macro_name=dict(type='str', required=True),
macro_value=dict(type='str', required=True), macro_value=dict(type='str', required=True),
@ -186,6 +200,8 @@ def main():
server_url = module.params['server_url'] server_url = module.params['server_url']
login_user = module.params['login_user'] login_user = module.params['login_user']
login_password = module.params['login_password'] login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
host_name = module.params['host_name'] host_name = module.params['host_name']
macro_name = (module.params['macro_name']).upper() macro_name = (module.params['macro_name']).upper()
macro_value = module.params['macro_value'] macro_value = module.params['macro_value']
@ -195,7 +211,7 @@ def main():
zbx = None zbx = None
# login to zabbix # login to zabbix
try: try:
zbx = ZabbixAPIExtends(server_url, timeout=timeout) zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
except Exception, e: except Exception, e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

View file

@ -52,6 +52,18 @@ options:
description: description:
- Zabbix user password. - Zabbix user password.
required: true required: true
http_login_user:
description:
- Basic Auth login
required: false
default: None
version_added: "2.1"
http_login_password:
description:
- Basic Auth password
required: false
default: None
version_added: "2.1"
host_names: host_names:
description: description:
- Hosts to manage maintenance window for. - Hosts to manage maintenance window for.
@ -91,6 +103,11 @@ options:
- Type of maintenance. With data collection, or without. - Type of maintenance. With data collection, or without.
required: false required: false
default: "true" default: "true"
timeout:
description:
- The timeout of API request (seconds).
default: 10
version_added: "2.1"
notes: notes:
- Useful for setting hosts in maintenance mode before big update, - Useful for setting hosts in maintenance mode before big update,
and removing maintenance window after update. and removing maintenance window after update.
@ -260,9 +277,12 @@ def main():
host_groups=dict(type='list', required=False, default=None, aliases=['host_group']), host_groups=dict(type='list', required=False, default=None, aliases=['host_group']),
login_user=dict(type='str', required=True), login_user=dict(type='str', required=True),
login_password=dict(type='str', required=True, no_log=True), login_password=dict(type='str', required=True, no_log=True),
http_login_user=dict(type='str', required=False, default=None),
http_login_password=dict(type='str', required=False, default=None, no_log=True),
name=dict(type='str', required=True), name=dict(type='str', required=True),
desc=dict(type='str', required=False, default="Created by Ansible"), desc=dict(type='str', required=False, default="Created by Ansible"),
collect_data=dict(type='bool', required=False, default=True), collect_data=dict(type='bool', required=False, default=True),
timeout=dict(type='int', default=10),
), ),
supports_check_mode=True, supports_check_mode=True,
) )
@ -275,18 +295,22 @@ def main():
state = module.params['state'] state = module.params['state']
login_user = module.params['login_user'] login_user = module.params['login_user']
login_password = module.params['login_password'] login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
minutes = module.params['minutes'] minutes = module.params['minutes']
name = module.params['name'] name = module.params['name']
desc = module.params['desc'] desc = module.params['desc']
server_url = module.params['server_url'] server_url = module.params['server_url']
collect_data = module.params['collect_data'] collect_data = module.params['collect_data']
timeout = module.params['timeout']
if collect_data: if collect_data:
maintenance_type = 0 maintenance_type = 0
else: else:
maintenance_type = 1 maintenance_type = 1
try: try:
zbx = ZabbixAPI(server_url) zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
except BaseException as e: except BaseException as e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)

View file

@ -48,6 +48,18 @@ options:
description: description:
- Zabbix user password. - Zabbix user password.
required: true required: true
http_login_user:
description:
- Basic Auth login
required: false
default: None
version_added: "2.1"
http_login_password:
description:
- Basic Auth password
required: false
default: None
version_added: "2.1"
timeout: timeout:
description: description:
- The timeout of API request (seconds). - The timeout of API request (seconds).
@ -142,8 +154,8 @@ except ImportError:
class ZabbixAPIExtends(ZabbixAPI): class ZabbixAPIExtends(ZabbixAPI):
screenitem = None screenitem = None
def __init__(self, server, timeout, **kwargs): def __init__(self, server, timeout, user, passwd, **kwargs):
ZabbixAPI.__init__(self, server, timeout=timeout) ZabbixAPI.__init__(self, server, timeout=timeout, user=user, passwd=passwd)
self.screenitem = ZabbixAPISubClass(self, dict({"prefix": "screenitem"}, **kwargs)) self.screenitem = ZabbixAPISubClass(self, dict({"prefix": "screenitem"}, **kwargs))
@ -318,6 +330,8 @@ def main():
server_url=dict(type='str', required=True, aliases=['url']), server_url=dict(type='str', required=True, aliases=['url']),
login_user=dict(type='str', required=True), login_user=dict(type='str', required=True),
login_password=dict(type='str', required=True, no_log=True), login_password=dict(type='str', required=True, no_log=True),
http_login_user=dict(type='str', required=False, default=None),
http_login_password=dict(type='str', required=False, default=None, no_log=True),
timeout=dict(type='int', default=10), timeout=dict(type='int', default=10),
screens=dict(type='list', required=True) screens=dict(type='list', required=True)
), ),
@ -330,13 +344,15 @@ def main():
server_url = module.params['server_url'] server_url = module.params['server_url']
login_user = module.params['login_user'] login_user = module.params['login_user']
login_password = module.params['login_password'] login_password = module.params['login_password']
http_login_user = module.params['http_login_user']
http_login_password = module.params['http_login_password']
timeout = module.params['timeout'] timeout = module.params['timeout']
screens = module.params['screens'] screens = module.params['screens']
zbx = None zbx = None
# login to zabbix # login to zabbix
try: try:
zbx = ZabbixAPIExtends(server_url, timeout=timeout) zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
except Exception, e: except Exception, e:
module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)