diff --git a/library/mt_interfaces.py b/library/mt_interfaces.py index 731fe1c..4f200b3 100644 --- a/library/mt_interfaces.py +++ b/library/mt_interfaces.py @@ -59,6 +59,7 @@ EXAMPLES = ''' from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent from ansible.module_utils.basic import AnsibleModule +from mt_utils import get_connection def main(): @@ -107,14 +108,15 @@ def main(): desired_params = params['settings'], idempotent_param = idempotent_parameter, api_path = '/interface/' + str(params['parameter']), - check_mode = module.check_mode + conn = get_connection(module), + check_mode = module.check_mode, ) # exit if login failed - if not mt_obj.login_success: - module.fail_json( - msg = mt_obj.failed_msg - ) + #if not mt_obj.login_success: + # module.fail_json( + # msg = mt_obj.failed_msg + # ) # add, remove or edit things mt_obj.sync_state() diff --git a/library/mt_utils.py b/library/mt_utils.py new file mode 100644 index 0000000..bd094f3 --- /dev/null +++ b/library/mt_utils.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +from ansible.module_utils.basic import env_fallback +from ansible.module_utils.six import iteritems +from __future__ import absolute_import, division, print_function +from ansible.module_utils import mt_api + +__metaclass__ = type + + +routeros_provider_spec = { + 'host': dict(), + 'port': dict(type='int'), + + 'username': dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])), + 'password': dict(fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD']), no_log=True), + 'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'), + + 'use_ssl': dict(type='bool'), + 'validate_certs': dict(type='bool'), + + 'timeout': dict(type='int'), + + 'transport': dict(default='api', choices=['cli', 'api']) +} + + +def load_params(module): + provider = module.params.get('provider') or dict() + for key, value in iteritems(provider): + if key in routeros_provider_spec: + if module.params.get(key) is None and value is not None: + module.params[key] = value + +_DEVICE_CONNECTION = None + + +def get_connection(module): + # pylint: disable=global-statement + global _DEVICE_CONNECTION + if not _DEVICE_CONNECTION: + load_params(module) + conn = Api(module) + _DEVICE_CONNECTION = conn + return _DEVICE_CONNECTION + + +class Api: + def __init__(self, module): + self._module = module + + username = self._module.params['username'] + password = self._module.params['password'] + + host = self._module.params['host'] + port = self._module.params['port'] + + timeout = self._module.params['timeout'] + + self._api = mt_api.Mikrotik( + host, + username, + password + ) diff --git a/pythonlibs/mt_common.py b/pythonlibs/mt_common.py index 814c431..a8f3141 100644 --- a/pythonlibs/mt_common.py +++ b/pythonlibs/mt_common.py @@ -1,7 +1,9 @@ #!/usr/bin/env python from ansible.module_utils import MikrotikConnection import re -import sys +from mt_utils import get_connection + +# import sys def list_to_string(list): @@ -57,7 +59,7 @@ class MikrotikIdempotent(): def __init__( self, hostname, username, password, desired_params, api_path, - state, idempotent_param, check_mode=False): + state, idempotent_param, conn, check_mode=False): self.state = state self.desired_params = desired_params @@ -72,7 +74,7 @@ class MikrotikIdempotent(): self.failed = False self.failed_msg = [] - self.mk = MikrotikConnection.login() + self.mk = conn def get_current_params(self): clean_params(self.desired_params)