# # Copyright (c) 2019 Ericsson AB. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # from __future__ import (absolute_import, division, print_function) __metaclass__ = type import json from ansible.module_utils._text import to_text from ansible.module_utils.basic import env_fallback from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list, ComplexList from ansible.module_utils.connection import Connection, ConnectionError _DEVICE_CONFIGS = {} def get_connection(module): if hasattr(module, '_eric_eccli_connection'): return module._eric_eccli_connection capabilities = get_capabilities(module) network_api = capabilities.get('network_api') if network_api == 'cliconf': module._eric_eccli_connection = Connection(module._socket_path) else: module.fail_json(msg='Invalid connection type %s' % network_api) return module._eric_eccli_connection def get_capabilities(module): if hasattr(module, '_eric_eccli_capabilities'): return module._eric_eccli_capabilities try: capabilities = Connection(module._socket_path).get_capabilities() except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors='surrogate_then_replace')) module._eric_eccli_capabilities = json.loads(capabilities) return module._eric_eccli_capabilities def run_commands(module, commands, check_rc=True): connection = get_connection(module) try: return connection.run_commands(commands=commands, check_rc=check_rc) except ConnectionError as exc: module.fail_json(msg=to_text(exc))