Use atexit to logout after zabbix module run (#58525)

This commit is contained in:
Mikhail Naletov 2019-06-30 20:35:05 +03:00 committed by ansibot
parent f0801879c7
commit d582efcc51
12 changed files with 23 additions and 10 deletions

View file

@ -440,6 +440,7 @@ msg:
''' '''
import atexit
import traceback import traceback
try: try:
@ -2002,6 +2003,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user,
passwd=http_login_password, validate_certs=validate_certs) passwd=http_login_password, validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -76,6 +76,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
try: try:
@ -169,6 +170,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -57,6 +57,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib 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, zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -276,6 +276,7 @@ EXAMPLES = '''
''' '''
import atexit
import copy import copy
import traceback import traceback

View file

@ -96,6 +96,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib 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, zbx = ZabbixAPIExtends(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -72,6 +72,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
try: try:
@ -200,6 +201,7 @@ def main():
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) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -122,6 +122,7 @@ EXAMPLES = '''
''' '''
import atexit
import datetime import datetime
import time import time
import traceback import traceback
@ -317,6 +318,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
# zabbix_api can call sys.exit() so we need to catch SystemExit here # zabbix_api can call sys.exit() so we need to catch SystemExit here
except (Exception, SystemExit) as e: except (Exception, SystemExit) 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

@ -173,6 +173,7 @@ ANSIBLE_METADATA = {
} }
import atexit
import base64 import base64
import traceback import traceback
@ -791,6 +792,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -219,6 +219,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
@ -585,6 +586,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password, zbx = ZabbixAPI(server_url, timeout=timeout, user=http_login_user, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -126,6 +126,7 @@ RETURN = ''' # '''
import traceback import traceback
import atexit
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule, missing_required_lib
try: try:
@ -157,7 +158,6 @@ class Proxy(object):
def add_proxy(self, data): def add_proxy(self, data):
try: try:
if self._module.check_mode: if self._module.check_mode:
self._zapi.logout()
self._module.exit_json(changed=True) self._module.exit_json(changed=True)
parameters = {} parameters = {}
@ -166,29 +166,24 @@ class Proxy(object):
parameters[item] = data[item] parameters[item] = data[item]
proxy_ids_list = self._zapi.proxy.create(parameters) proxy_ids_list = self._zapi.proxy.create(parameters)
self._zapi.logout()
self._module.exit_json(changed=True, self._module.exit_json(changed=True,
result="Successfully added proxy %s (%s)" % result="Successfully added proxy %s (%s)" %
(data['host'], data['status'])) (data['host'], data['status']))
if len(proxy_ids_list) >= 1: if len(proxy_ids_list) >= 1:
return proxy_ids_list['proxyids'][0] return proxy_ids_list['proxyids'][0]
except Exception as e: except Exception as e:
self._zapi.logout()
self._module.fail_json(msg="Failed to create proxy %s: %s" % self._module.fail_json(msg="Failed to create proxy %s: %s" %
(data['host'], e)) (data['host'], e))
def delete_proxy(self, proxy_id, proxy_name): def delete_proxy(self, proxy_id, proxy_name):
try: try:
if self._module.check_mode: if self._module.check_mode:
self._zapi.logout()
self._module.exit_json(changed=True) self._module.exit_json(changed=True)
self._zapi.proxy.delete([proxy_id]) self._zapi.proxy.delete([proxy_id])
self._zapi.logout()
self._module.exit_json(changed=True, self._module.exit_json(changed=True,
result="Successfully deleted" result="Successfully deleted"
+ " proxy %s" % proxy_name) + " proxy %s" % proxy_name)
except Exception as e: except Exception as e:
self._zapi.logout()
self._module.fail_json(msg="Failed to delete proxy %s: %s" % self._module.fail_json(msg="Failed to delete proxy %s: %s" %
(proxy_name, str(e))) (proxy_name, str(e)))
@ -210,7 +205,6 @@ class Proxy(object):
def update_proxy(self, proxy_id, data): def update_proxy(self, proxy_id, data):
try: try:
if self._module.check_mode: if self._module.check_mode:
self._zapi.logout()
self._module.exit_json(changed=True) self._module.exit_json(changed=True)
parameters = {'proxyid': proxy_id} parameters = {'proxyid': proxy_id}
@ -229,17 +223,14 @@ class Proxy(object):
if len(parameters) > 1: if len(parameters) > 1:
self._zapi.proxy.update(parameters) self._zapi.proxy.update(parameters)
self._zapi.logout()
self._module.exit_json( self._module.exit_json(
changed=True, changed=True,
result="Successfully updated proxy %s (%s)" % result="Successfully updated proxy %s (%s)" %
(data['host'], proxy_id) (data['host'], proxy_id)
) )
else: else:
self._zapi.logout()
self._module.exit_json(changed=False) self._module.exit_json(changed=False)
except Exception as e: except Exception as e:
self._zapi.logout()
self._module.fail_json(msg="Failed to update proxy %s: %s" % self._module.fail_json(msg="Failed to update proxy %s: %s" %
(data['host'], e)) (data['host'], e))
@ -319,6 +310,7 @@ def main():
passwd=http_login_password, passwd=http_login_password,
validate_certs=validate_certs) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -154,6 +154,7 @@ EXAMPLES = '''
''' '''
import atexit
import traceback import traceback
try: try:
@ -393,6 +394,7 @@ def main():
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) validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except Exception as e: except Exception 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

@ -196,6 +196,7 @@ template_json:
from distutils.version import LooseVersion from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule, missing_required_lib from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
import atexit
import json import json
import traceback import traceback
@ -492,6 +493,7 @@ def main():
zbx = ZabbixAPI(server_url, timeout=timeout, zbx = ZabbixAPI(server_url, timeout=timeout,
user=http_login_user, passwd=http_login_password, validate_certs=validate_certs) user=http_login_user, passwd=http_login_password, validate_certs=validate_certs)
zbx.login(login_user, login_password) zbx.login(login_user, login_password)
atexit.register(zbx.logout)
except ZabbixAPIException as e: except ZabbixAPIException 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)