2017-11-27 20:55:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# (c) 2017, Ansible by Red Hat, inc
|
|
|
|
#
|
|
|
|
# This file is part of Ansible by Red Hat
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
from ansible.module_utils.basic import env_fallback
|
2017-12-01 17:03:02 +00:00
|
|
|
from ansible.module_utils.connection import Connection, ConnectionError
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.common.utils import to_list, EntityCollection
|
2017-11-27 20:55:08 +00:00
|
|
|
|
|
|
|
_DEVICE_CONFIGS = {}
|
|
|
|
_CONNECTION = None
|
|
|
|
|
2017-12-06 14:22:15 +00:00
|
|
|
_COMMAND_SPEC = {
|
2017-11-27 20:55:08 +00:00
|
|
|
'command': dict(key=True),
|
|
|
|
'prompt': dict(),
|
|
|
|
'answer': dict()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_connection(module):
|
|
|
|
global _CONNECTION
|
|
|
|
if _CONNECTION:
|
|
|
|
return _CONNECTION
|
|
|
|
_CONNECTION = Connection(module._socket_path)
|
|
|
|
return _CONNECTION
|
|
|
|
|
|
|
|
|
|
|
|
def to_commands(module, commands):
|
|
|
|
if not isinstance(commands, list):
|
|
|
|
raise AssertionError('argument must be of type <list>')
|
|
|
|
|
2017-12-06 14:22:15 +00:00
|
|
|
transform = EntityCollection(module, _COMMAND_SPEC)
|
2017-11-27 20:55:08 +00:00
|
|
|
commands = transform(commands)
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
def run_commands(module, commands, check_rc=True):
|
|
|
|
connection = get_connection(module)
|
|
|
|
|
|
|
|
commands = to_commands(module, to_list(commands))
|
|
|
|
|
|
|
|
responses = list()
|
|
|
|
|
|
|
|
for cmd in commands:
|
|
|
|
out = connection.get(**cmd)
|
|
|
|
responses.append(to_text(out, errors='surrogate_then_replace'))
|
|
|
|
|
|
|
|
return responses
|
2017-12-01 17:03:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_config(module, source='running'):
|
|
|
|
conn = get_connection(module)
|
|
|
|
out = conn.get_config(source)
|
|
|
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
|
|
|
return cfg
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(module, config):
|
|
|
|
try:
|
|
|
|
conn = get_connection(module)
|
|
|
|
conn.edit_config(config)
|
|
|
|
except ConnectionError as exc:
|
|
|
|
module.fail_json(msg=to_text(exc))
|