mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Pluribus Networks network cli terminal and cliconf plugins (#53735)
* Pluribus Networks network cli terminal and cliconf plugins * Changes in Unit tests and modules according to network_cli connection * Changes in Unit tests and modules according to network_cli connection
This commit is contained in:
committed by
Ganesh Nalawade
parent
4594aee25a
commit
e2d92e82c4
59
lib/ansible/module_utils/network/netvisor/netvisor.py
Normal file
59
lib/ansible/module_utils/network/netvisor/netvisor.py
Normal file
@@ -0,0 +1,59 @@
|
||||
# Copyright: (c) 2018, Pluribus Networks
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
#
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
import json
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.network.common.utils import to_list, ComplexList
|
||||
from ansible.module_utils.connection import Connection, ConnectionError
|
||||
from ansible.module_utils.connection import exec_command
|
||||
|
||||
|
||||
def get_connection(module):
|
||||
if hasattr(module, '_nvos_connection'):
|
||||
return module._nvos_connection
|
||||
|
||||
capabilities = get_capabilities(module)
|
||||
network_api = capabilities.get('network_api')
|
||||
if network_api == 'cliconf':
|
||||
module._nvos_connection = Connection(module._socket_path)
|
||||
else:
|
||||
module.fail_json(msg='Invalid connection type %s' % network_api)
|
||||
|
||||
return module._nvos_connection
|
||||
|
||||
|
||||
def get_capabilities(module):
|
||||
if hasattr(module, '_nvos_capabilities'):
|
||||
return module._nvos_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._nvos_capabilities = json.loads(capabilities)
|
||||
return module._nvos_capabilities
|
||||
|
||||
|
||||
def to_commands(module, commands):
|
||||
spec = {
|
||||
'command': dict(key=True),
|
||||
'prompt': dict(),
|
||||
'answer': dict()
|
||||
}
|
||||
transform = ComplexList(spec, module)
|
||||
return transform(commands)
|
||||
|
||||
|
||||
def run_commands(module, commands, check_rc=True):
|
||||
commands = to_commands(module, to_list(commands))
|
||||
for cmd in commands:
|
||||
cmd = module.jsonify(cmd)
|
||||
rc, out, err = exec_command(module, cmd)
|
||||
if check_rc and rc != 0:
|
||||
module.fail_json(msg=to_text(err, errors='surrogate_or_strict'), rc=rc)
|
||||
responses = (to_text(out, errors='surrogate_or_strict'))
|
||||
|
||||
return rc, out, err
|
||||
@@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
import shlex
|
||||
from ansible.module_utils.network.netvisor.netvisor import run_commands
|
||||
|
||||
|
||||
def pn_cli(module, switch=None, username=None, password=None, switch_local=None):
|
||||
@@ -16,7 +16,7 @@ def pn_cli(module, switch=None, username=None, password=None, switch_local=None)
|
||||
:return: The cli string for further processing.
|
||||
"""
|
||||
|
||||
cli = '/usr/bin/cli --quiet -e --no-login-prompt '
|
||||
cli = ''
|
||||
|
||||
if username and password:
|
||||
cli += '--user "%s":"%s" ' % (username, password)
|
||||
@@ -48,25 +48,19 @@ def run_cli(module, cli, state_map):
|
||||
state = module.params['state']
|
||||
command = state_map[state]
|
||||
|
||||
cmd = shlex.split(cli)
|
||||
result, out, err = module.run_command(cmd)
|
||||
|
||||
remove_cmd = '/usr/bin/cli --quiet -e --no-login-prompt'
|
||||
result, out, err = run_commands(module, cli)
|
||||
|
||||
results = dict(
|
||||
command=' '.join(cmd).replace(remove_cmd, ''),
|
||||
msg="%s operation completed" % command,
|
||||
command=cli,
|
||||
msg="%s operation completed" % cli,
|
||||
changed=True
|
||||
)
|
||||
# Response in JSON format
|
||||
if result != 0:
|
||||
module.exit_json(
|
||||
command=' '.join(cmd).replace(remove_cmd, ''),
|
||||
stderr=err.strip(),
|
||||
msg="%s operation failed" % command,
|
||||
command=cli,
|
||||
msg="%s operation failed" % cli,
|
||||
changed=False
|
||||
)
|
||||
|
||||
if out:
|
||||
results['stdout'] = out.strip()
|
||||
module.exit_json(**results)
|
||||
|
||||
Reference in New Issue
Block a user