2018-05-17 14:06:24 +00:00
|
|
|
# (C) 2017 Red Hat Inc.
|
|
|
|
# Copyright (C) 2017 Lenovo.
|
|
|
|
#
|
|
|
|
# GNU General Public License v3.0+
|
|
|
|
#
|
|
|
|
# This program 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.
|
|
|
|
#
|
|
|
|
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
#
|
2018-05-30 09:53:26 +00:00
|
|
|
# Contains CLIConf Plugin methods for CNOS Modules
|
2018-05-17 14:06:24 +00:00
|
|
|
# Lenovo Networking
|
|
|
|
#
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2019-02-08 16:04:05 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
cliconf: cnos
|
|
|
|
short_description: Use cnos cliconf to run command on Lenovo CNOS platform
|
|
|
|
description:
|
|
|
|
- This cnos plugin provides low level abstraction apis for
|
|
|
|
sending and receiving CLI commands from Lenovo CNOS network devices.
|
|
|
|
version_added: 2.6
|
|
|
|
"""
|
|
|
|
|
2018-05-17 14:06:24 +00:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
|
|
|
|
from itertools import chain
|
2019-02-01 14:17:52 +00:00
|
|
|
from ansible.module_utils.common._collections_compat import Mapping
|
2018-05-17 14:06:24 +00:00
|
|
|
from ansible.module_utils._text import to_bytes, to_text
|
|
|
|
from ansible.module_utils.network.common.utils import to_list
|
|
|
|
from ansible.plugins.cliconf import CliconfBase, enable_mode
|
|
|
|
|
|
|
|
|
|
|
|
class Cliconf(CliconfBase):
|
|
|
|
|
|
|
|
def get_device_info(self):
|
|
|
|
device_info = {}
|
|
|
|
|
|
|
|
device_info['network_os'] = 'cnos'
|
2019-01-22 14:07:04 +00:00
|
|
|
reply = self.get('show sys-info')
|
2018-05-17 14:06:24 +00:00
|
|
|
data = to_text(reply, errors='surrogate_or_strict').strip()
|
2019-01-22 14:07:04 +00:00
|
|
|
host = self.get('show hostname')
|
2018-07-18 16:17:08 +00:00
|
|
|
hostname = to_text(host, errors='surrogate_or_strict').strip()
|
|
|
|
if data:
|
|
|
|
device_info['network_os_version'] = self.parse_version(data)
|
|
|
|
device_info['network_os_model'] = self.parse_model(data)
|
|
|
|
device_info['network_os_hostname'] = hostname
|
2018-05-17 14:06:24 +00:00
|
|
|
|
2018-07-18 16:17:08 +00:00
|
|
|
return device_info
|
2018-05-17 14:06:24 +00:00
|
|
|
|
2018-07-18 16:17:08 +00:00
|
|
|
def parse_version(self, data):
|
|
|
|
for line in data.split('\n'):
|
|
|
|
line = line.strip()
|
|
|
|
match = re.match(r'System Software Revision (.*?)',
|
|
|
|
line, re.M | re.I)
|
|
|
|
if match:
|
|
|
|
vers = line.split(':')
|
|
|
|
ver = vers[1].strip()
|
|
|
|
return ver
|
|
|
|
return "NA"
|
2018-05-17 14:06:24 +00:00
|
|
|
|
2018-07-18 16:17:08 +00:00
|
|
|
def parse_model(self, data):
|
|
|
|
for line in data.split('\n'):
|
|
|
|
line = line.strip()
|
|
|
|
match = re.match(r'System Model (.*?)', line, re.M | re.I)
|
|
|
|
if match:
|
|
|
|
mdls = line.split(':')
|
|
|
|
mdl = mdls[1].strip()
|
|
|
|
return mdl
|
|
|
|
return "NA"
|
2018-05-17 14:06:24 +00:00
|
|
|
|
|
|
|
@enable_mode
|
2019-01-28 13:24:44 +00:00
|
|
|
def get_config(self, source='running', format='text', flags=None):
|
2018-05-17 14:06:24 +00:00
|
|
|
if source not in ('running', 'startup'):
|
|
|
|
msg = "fetching configuration from %s is not supported"
|
|
|
|
return self.invalid_params(msg % source)
|
|
|
|
if source == 'running':
|
2019-01-22 14:07:04 +00:00
|
|
|
cmd = 'show running-config'
|
2018-05-17 14:06:24 +00:00
|
|
|
else:
|
2019-01-22 14:07:04 +00:00
|
|
|
cmd = 'show startup-config'
|
2018-05-17 14:06:24 +00:00
|
|
|
return self.send_command(cmd)
|
|
|
|
|
|
|
|
@enable_mode
|
2019-02-01 14:17:52 +00:00
|
|
|
def edit_config(self, candidate=None, commit=True,
|
|
|
|
replace=None, comment=None):
|
|
|
|
resp = {}
|
|
|
|
results = []
|
|
|
|
requests = []
|
|
|
|
if commit:
|
|
|
|
self.send_command('configure terminal')
|
|
|
|
for line in to_list(candidate):
|
|
|
|
if not isinstance(line, Mapping):
|
|
|
|
line = {'command': line}
|
|
|
|
|
|
|
|
cmd = line['command']
|
|
|
|
if cmd != 'end' and cmd[0] != '!':
|
|
|
|
results.append(self.send_command(**line))
|
|
|
|
requests.append(cmd)
|
|
|
|
|
|
|
|
self.send_command('end')
|
|
|
|
else:
|
|
|
|
raise ValueError('check mode is not supported')
|
|
|
|
|
|
|
|
resp['request'] = requests
|
|
|
|
resp['response'] = results
|
|
|
|
return resp
|
2018-05-17 14:06:24 +00:00
|
|
|
|
2019-05-07 21:25:57 +00:00
|
|
|
def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
|
|
|
|
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
|
2018-05-17 14:06:24 +00:00
|
|
|
|
|
|
|
def get_capabilities(self):
|
2019-01-28 13:24:44 +00:00
|
|
|
result = super(Cliconf, self).get_capabilities()
|
2018-05-17 14:06:24 +00:00
|
|
|
return json.dumps(result)
|