mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 13:20:23 -07:00
Initial commit
This commit is contained in:
commit
aebc1b03fd
4861 changed files with 812621 additions and 0 deletions
0
plugins/module_utils/network/netvisor/__init__.py
Normal file
0
plugins/module_utils/network/netvisor/__init__.py
Normal file
59
plugins/module_utils/network/netvisor/netvisor.py
Normal file
59
plugins/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_collections.ansible.netcommon.plugins.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
|
66
plugins/module_utils/network/netvisor/pn_nvos.py
Normal file
66
plugins/module_utils/network/netvisor/pn_nvos.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
# 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
|
||||
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.network.netvisor.netvisor import run_commands
|
||||
|
||||
|
||||
def pn_cli(module, switch=None, username=None, password=None, switch_local=None):
|
||||
"""
|
||||
Method to generate the cli portion to launch the Netvisor cli.
|
||||
:param module: The Ansible module to fetch username and password.
|
||||
:return: The cli string for further processing.
|
||||
"""
|
||||
|
||||
cli = ''
|
||||
|
||||
if username and password:
|
||||
cli += '--user "%s":"%s" ' % (username, password)
|
||||
if switch:
|
||||
cli += ' switch ' + switch
|
||||
if switch_local:
|
||||
cli += ' switch-local '
|
||||
|
||||
return cli
|
||||
|
||||
|
||||
def booleanArgs(arg, trueString, falseString):
|
||||
if arg is True:
|
||||
return " %s " % trueString
|
||||
elif arg is False:
|
||||
return " %s " % falseString
|
||||
else:
|
||||
return ""
|
||||
|
||||
|
||||
def run_cli(module, cli, state_map):
|
||||
"""
|
||||
This method executes the cli command on the target node(s) and returns the
|
||||
output. The module then exits based on the output.
|
||||
:param cli: the complete cli string to be executed on the target node(s).
|
||||
:param state_map: Provides state of the command.
|
||||
:param module: The Ansible module to fetch command
|
||||
"""
|
||||
state = module.params['state']
|
||||
command = state_map[state]
|
||||
|
||||
result, out, err = run_commands(module, cli)
|
||||
|
||||
results = dict(
|
||||
command=cli,
|
||||
msg="%s operation completed" % cli,
|
||||
changed=True
|
||||
)
|
||||
# Response in JSON format
|
||||
if result != 0:
|
||||
module.exit_json(
|
||||
command=cli,
|
||||
msg="%s operation failed" % cli,
|
||||
changed=False
|
||||
)
|
||||
|
||||
module.exit_json(**results)
|
Loading…
Add table
Add a link
Reference in a new issue