still work in progress

This commit is contained in:
Valentin Gurmeza 2018-01-31 17:36:53 -08:00
parent da86eb8a6b
commit 8aa0c16c84
3 changed files with 75 additions and 8 deletions

View file

@ -59,6 +59,7 @@ EXAMPLES = '''
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from mt_utils import get_connection
def main(): def main():
@ -107,14 +108,15 @@ def main():
desired_params = params['settings'], desired_params = params['settings'],
idempotent_param = idempotent_parameter, idempotent_param = idempotent_parameter,
api_path = '/interface/' + str(params['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 # exit if login failed
if not mt_obj.login_success: #if not mt_obj.login_success:
module.fail_json( # module.fail_json(
msg = mt_obj.failed_msg # msg = mt_obj.failed_msg
) # )
# add, remove or edit things # add, remove or edit things
mt_obj.sync_state() mt_obj.sync_state()

63
library/mt_utils.py Normal file
View file

@ -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
)

View file

@ -1,7 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
from ansible.module_utils import MikrotikConnection from ansible.module_utils import MikrotikConnection
import re import re
import sys from mt_utils import get_connection
# import sys
def list_to_string(list): def list_to_string(list):
@ -57,7 +59,7 @@ class MikrotikIdempotent():
def __init__( def __init__(
self, hostname, username, password, desired_params, api_path, 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.state = state
self.desired_params = desired_params self.desired_params = desired_params
@ -72,7 +74,7 @@ class MikrotikIdempotent():
self.failed = False self.failed = False
self.failed_msg = [] self.failed_msg = []
self.mk = MikrotikConnection.login() self.mk = conn
def get_current_params(self): def get_current_params(self):
clean_params(self.desired_params) clean_params(self.desired_params)