mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 04:40:22 -07:00
Adding slx_command module and supporting module_utils. (#38235)
* Adding slx_command module and supporting module_utils. This commit adds the slx_command module and tests as well as the required slxos module_utils. * Update copyright in header * Adding missing module init * Cleaning up shebangs/licensing. * Incorporating feedback Removing reference to `waitfor` alias in `slxos_command` module. Adding `Extreme Networks` to `short_description` of `slxos_command` module. * Adding cliconf tests * Fixing 3.X tests * Adding docstrings to test methods for slxos cliconf tests * Adding slxos terminal tests * Adding slxos module_utils tests * Adding Extreme Networks team members to BOTMETA.yml
This commit is contained in:
parent
8f6ee2a5ca
commit
5b1a8ee1e8
20 changed files with 2375 additions and 0 deletions
107
lib/ansible/module_utils/network/slxos/slxos.py
Normal file
107
lib/ansible/module_utils/network/slxos/slxos.py
Normal file
|
@ -0,0 +1,107 @@
|
|||
#
|
||||
# (c) 2018 Extreme Networks Inc.
|
||||
#
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# 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/>.
|
||||
#
|
||||
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
|
||||
|
||||
|
||||
def get_connection(module):
|
||||
"""Get switch connection
|
||||
|
||||
Creates reusable SSH connection to the switch described in a given module.
|
||||
|
||||
Args:
|
||||
module: A valid AnsibleModule instance.
|
||||
|
||||
Returns:
|
||||
An instance of `ansible.module_utils.connection.Connection` with a
|
||||
connection to the switch described in the provided module.
|
||||
|
||||
Raises:
|
||||
AnsibleConnectionFailure: An error occurred connecting to the device
|
||||
"""
|
||||
if hasattr(module, 'slxos_connection'):
|
||||
return module.slxos_connection
|
||||
|
||||
capabilities = get_capabilities(module)
|
||||
network_api = capabilities.get('network_api')
|
||||
if network_api == 'cliconf':
|
||||
module.slxos_connection = Connection(module._socket_path)
|
||||
else:
|
||||
module.fail_json(msg='Invalid connection type %s' % network_api)
|
||||
|
||||
return module.slxos_connection
|
||||
|
||||
|
||||
def get_capabilities(module):
|
||||
"""Get switch capabilities
|
||||
|
||||
Collects and returns a python object with the switch capabilities.
|
||||
|
||||
Args:
|
||||
module: A valid AnsibleModule instance.
|
||||
|
||||
Returns:
|
||||
A dictionary containing the switch capabilities.
|
||||
"""
|
||||
if hasattr(module, 'slxos_capabilities'):
|
||||
return module.slxos_capabilities
|
||||
|
||||
capabilities = Connection(module._socket_path).get_capabilities()
|
||||
module.slxos_capabilities = json.loads(capabilities)
|
||||
return module.slxos_capabilities
|
||||
|
||||
|
||||
def run_commands(module, commands):
|
||||
"""Run command list against connection.
|
||||
|
||||
Get new or previously used connection and send commands to it one at a time,
|
||||
collecting response.
|
||||
|
||||
Args:
|
||||
module: A valid AnsibleModule instance.
|
||||
commands: Iterable of command strings.
|
||||
|
||||
Returns:
|
||||
A list of output strings.
|
||||
"""
|
||||
responses = list()
|
||||
connection = get_connection(module)
|
||||
|
||||
for cmd in to_list(commands):
|
||||
if isinstance(cmd, dict):
|
||||
command = cmd['command']
|
||||
prompt = cmd['prompt']
|
||||
answer = cmd['answer']
|
||||
else:
|
||||
command = cmd
|
||||
prompt = None
|
||||
answer = None
|
||||
|
||||
out = connection.get(command, prompt, answer)
|
||||
|
||||
try:
|
||||
out = to_text(out, errors='surrogate_or_strict')
|
||||
except UnicodeError:
|
||||
module.fail_json(msg=u'Failed to decode output from %s: %s' % (cmd, to_text(out)))
|
||||
|
||||
responses.append(out)
|
||||
|
||||
return responses
|
Loading…
Add table
Add a link
Reference in a new issue