From d582efcc516857d80f425cfb2db48b25e2fbc526 Mon Sep 17 00:00:00 2001 From: Mikhail Naletov <36846182+okgolove@users.noreply.github.com> Date: Sun, 30 Jun 2019 20:35:05 +0300 Subject: [PATCH] Use atexit to logout after zabbix module run (#58525) --- .../modules/monitoring/zabbix/zabbix_action.py | 2 ++ .../modules/monitoring/zabbix/zabbix_group.py | 2 ++ .../modules/monitoring/zabbix/zabbix_group_info.py | 2 ++ lib/ansible/modules/monitoring/zabbix/zabbix_host.py | 1 + .../modules/monitoring/zabbix/zabbix_host_info.py | 2 ++ .../modules/monitoring/zabbix/zabbix_hostmacro.py | 2 ++ .../modules/monitoring/zabbix/zabbix_maintenance.py | 2 ++ lib/ansible/modules/monitoring/zabbix/zabbix_map.py | 2 ++ .../modules/monitoring/zabbix/zabbix_mediatype.py | 2 ++ .../modules/monitoring/zabbix/zabbix_proxy.py | 12 ++---------- .../modules/monitoring/zabbix/zabbix_screen.py | 2 ++ .../modules/monitoring/zabbix/zabbix_template.py | 2 ++ 12 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_action.py b/lib/ansible/modules/monitoring/zabbix/zabbix_action.py index 79f0520d821..d6a8840e72e 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_action.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_action.py @@ -440,6 +440,7 @@ msg: ''' +import atexit import traceback try: @@ -2002,6 +2003,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_group.py b/lib/ansible/modules/monitoring/zabbix/zabbix_group.py index 29a3cd852f3..d0f3e95d7e3 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_group.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_group.py @@ -76,6 +76,7 @@ EXAMPLES = ''' ''' +import atexit import traceback try: @@ -169,6 +170,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_group_info.py b/lib/ansible/modules/monitoring/zabbix/zabbix_group_info.py index 6665f6098e2..b1a12d4b624 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_group_info.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_group_info.py @@ -57,6 +57,7 @@ EXAMPLES = ''' ''' +import atexit import traceback from ansible.module_utils.basic import AnsibleModule, missing_required_lib @@ -128,6 +129,7 @@ def main(): zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_host.py b/lib/ansible/modules/monitoring/zabbix/zabbix_host.py index a4ee833becb..ffd2b5bbdbf 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_host.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_host.py @@ -276,6 +276,7 @@ EXAMPLES = ''' ''' +import atexit import copy import traceback diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_host_info.py b/lib/ansible/modules/monitoring/zabbix/zabbix_host_info.py index b384340f004..f0a6ecea9d5 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_host_info.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_host_info.py @@ -96,6 +96,7 @@ EXAMPLES = ''' ''' +import atexit import traceback from ansible.module_utils.basic import AnsibleModule, missing_required_lib @@ -218,6 +219,7 @@ def main(): zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_hostmacro.py b/lib/ansible/modules/monitoring/zabbix/zabbix_hostmacro.py index 0af3ff104a3..88c98281444 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_hostmacro.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_hostmacro.py @@ -72,6 +72,7 @@ EXAMPLES = ''' ''' +import atexit import traceback try: @@ -200,6 +201,7 @@ def main(): zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_maintenance.py b/lib/ansible/modules/monitoring/zabbix/zabbix_maintenance.py index 15c58a0de9c..949bfd18fe0 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_maintenance.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_maintenance.py @@ -122,6 +122,7 @@ EXAMPLES = ''' ''' +import atexit import datetime import time import traceback @@ -317,6 +318,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) # zabbix_api can call sys.exit() so we need to catch SystemExit here except (Exception, SystemExit) as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_map.py b/lib/ansible/modules/monitoring/zabbix/zabbix_map.py index 6acfda1450e..8111f657ce4 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_map.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_map.py @@ -173,6 +173,7 @@ ANSIBLE_METADATA = { } +import atexit import base64 import traceback @@ -791,6 +792,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_mediatype.py b/lib/ansible/modules/monitoring/zabbix/zabbix_mediatype.py index 9a485cbd3f3..a54802e5f36 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_mediatype.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_mediatype.py @@ -219,6 +219,7 @@ EXAMPLES = ''' ''' +import atexit import traceback @@ -585,6 +586,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_proxy.py b/lib/ansible/modules/monitoring/zabbix/zabbix_proxy.py index c41a508cbbe..4690ab8a352 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_proxy.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_proxy.py @@ -126,6 +126,7 @@ RETURN = ''' # ''' import traceback +import atexit from ansible.module_utils.basic import AnsibleModule, missing_required_lib try: @@ -157,7 +158,6 @@ class Proxy(object): def add_proxy(self, data): try: if self._module.check_mode: - self._zapi.logout() self._module.exit_json(changed=True) parameters = {} @@ -166,29 +166,24 @@ class Proxy(object): parameters[item] = data[item] proxy_ids_list = self._zapi.proxy.create(parameters) - self._zapi.logout() self._module.exit_json(changed=True, result="Successfully added proxy %s (%s)" % (data['host'], data['status'])) if len(proxy_ids_list) >= 1: return proxy_ids_list['proxyids'][0] except Exception as e: - self._zapi.logout() self._module.fail_json(msg="Failed to create proxy %s: %s" % (data['host'], e)) def delete_proxy(self, proxy_id, proxy_name): try: if self._module.check_mode: - self._zapi.logout() self._module.exit_json(changed=True) self._zapi.proxy.delete([proxy_id]) - self._zapi.logout() self._module.exit_json(changed=True, result="Successfully deleted" + " proxy %s" % proxy_name) except Exception as e: - self._zapi.logout() self._module.fail_json(msg="Failed to delete proxy %s: %s" % (proxy_name, str(e))) @@ -210,7 +205,6 @@ class Proxy(object): def update_proxy(self, proxy_id, data): try: if self._module.check_mode: - self._zapi.logout() self._module.exit_json(changed=True) parameters = {'proxyid': proxy_id} @@ -229,17 +223,14 @@ class Proxy(object): if len(parameters) > 1: self._zapi.proxy.update(parameters) - self._zapi.logout() self._module.exit_json( changed=True, result="Successfully updated proxy %s (%s)" % (data['host'], proxy_id) ) else: - self._zapi.logout() self._module.exit_json(changed=False) except Exception as e: - self._zapi.logout() self._module.fail_json(msg="Failed to update proxy %s: %s" % (data['host'], e)) @@ -319,6 +310,7 @@ def main(): passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py b/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py index f3d3ae9071f..2e3b393f945 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_screen.py @@ -154,6 +154,7 @@ EXAMPLES = ''' ''' +import atexit import traceback try: @@ -393,6 +394,7 @@ def main(): zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except Exception as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e) diff --git a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py index 790a38a6866..d2762b13096 100644 --- a/lib/ansible/modules/monitoring/zabbix/zabbix_template.py +++ b/lib/ansible/modules/monitoring/zabbix/zabbix_template.py @@ -196,6 +196,7 @@ template_json: from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils._text import to_native +import atexit import json import traceback @@ -492,6 +493,7 @@ def main(): zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) zbx.login(login_user, login_password) + atexit.register(zbx.logout) except ZabbixAPIException as e: module.fail_json(msg="Failed to connect to Zabbix server: %s" % e)