2016-01-10 15:33:00 +00:00
|
|
|
#
|
|
|
|
# (c) 2015 Peter Sprygada, <psprygada@ansible.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
2016-04-25 20:04:19 +00:00
|
|
|
|
2016-04-06 02:44:07 +00:00
|
|
|
from distutils.version import LooseVersion
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
from ansible.module_utils.pycompat24 import get_exception
|
|
|
|
from ansible.module_utils.network import NetworkError, register_transport, to_list
|
|
|
|
from ansible.module_utils.shell import CliBase
|
2016-08-23 20:13:44 +00:00
|
|
|
from ansible.module_utils.six import string_types
|
2016-04-05 18:06:17 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
# temporary fix until modules are update. to be removed before 2.2 final
|
|
|
|
from ansible.module_utils.network import get_module
|
|
|
|
|
2016-04-06 02:44:07 +00:00
|
|
|
try:
|
|
|
|
from jnpr.junos import Device
|
|
|
|
from jnpr.junos.utils.config import Config
|
|
|
|
from jnpr.junos.version import VERSION
|
2016-08-31 18:34:15 +00:00
|
|
|
from jnpr.junos.exception import RpcError, ConnectError, ConfigLoadError, CommitError
|
2016-04-06 02:44:07 +00:00
|
|
|
from jnpr.junos.exception import LockError, UnlockError
|
2016-08-31 18:34:15 +00:00
|
|
|
if LooseVersion(VERSION) < LooseVersion('1.2.2'):
|
2016-04-06 02:44:07 +00:00
|
|
|
HAS_PYEZ = False
|
|
|
|
else:
|
|
|
|
HAS_PYEZ = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYEZ = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
import jxmlease
|
|
|
|
HAS_JXMLEASE = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_JXMLEASE = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
from lxml import etree
|
|
|
|
except ImportError:
|
|
|
|
import xml.etree.ElementTree as etree
|
|
|
|
|
|
|
|
|
|
|
|
def xml_to_json(val):
|
2016-08-23 20:13:44 +00:00
|
|
|
if isinstance(val, string_types):
|
2016-04-06 02:44:07 +00:00
|
|
|
return jxmlease.parse(val)
|
|
|
|
else:
|
|
|
|
return jxmlease.parse_etree(val)
|
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
|
2016-04-06 02:44:07 +00:00
|
|
|
def xml_to_string(val):
|
|
|
|
return etree.tostring(val)
|
|
|
|
|
|
|
|
|
|
|
|
class Netconf(object):
|
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def __init__(self):
|
2016-04-06 02:44:07 +00:00
|
|
|
self.device = None
|
|
|
|
self.config = None
|
|
|
|
self._locked = False
|
2016-08-31 18:34:15 +00:00
|
|
|
self._connected = False
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def _error(self, msg):
|
2016-04-06 02:44:07 +00:00
|
|
|
if self.device:
|
|
|
|
if self._locked:
|
|
|
|
self.config.unlock()
|
|
|
|
self.disconnect()
|
2016-08-31 18:34:15 +00:00
|
|
|
raise NetworkError(msg)
|
|
|
|
|
|
|
|
def _log(self, msg):
|
|
|
|
# Logging is a complex topic and a discussion will be started after 2.2
|
|
|
|
# is released
|
|
|
|
pass
|
|
|
|
|
|
|
|
def connect(self, params, **kwargs):
|
|
|
|
host = params['host']
|
|
|
|
port = params.get('port') or 830
|
|
|
|
|
|
|
|
user = params['username']
|
|
|
|
passwd = params['password']
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
try:
|
2016-08-31 18:34:15 +00:00
|
|
|
self.device = Device(host, user=user, passwd=passwd, port=port, gather_facts=False).open()
|
|
|
|
except ConnectError:
|
|
|
|
exc = get_exception()
|
|
|
|
self._error('unable to connect to %s: %s' % (host, str(exc)))
|
|
|
|
|
|
|
|
self.config = Config(self.device)
|
|
|
|
self._connected = True
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def disconnect(self):
|
|
|
|
if self.device:
|
|
|
|
self.device.close()
|
|
|
|
self._connected = False
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
### Command methods ###
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def run_commands(self, commands, **kwargs):
|
|
|
|
output = kwargs.get('format') or 'xml'
|
|
|
|
return self.execute(to_list(commands), format=output)
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def execute(self, commands, format='xml', **kwargs):
|
|
|
|
'''Send commands to the device.'''
|
|
|
|
try:
|
|
|
|
return self.device.cli(commands, format=format)
|
|
|
|
except (ValueError, RpcError):
|
2016-05-16 12:11:15 +00:00
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error('Unable to get cli output: %s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
### Config methods ###
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
def unlock_config(self):
|
|
|
|
try:
|
|
|
|
self.config.unlock()
|
|
|
|
self._locked = False
|
2016-05-16 12:11:15 +00:00
|
|
|
except UnlockError:
|
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._log('unable to unlock config: %s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
def lock_config(self):
|
|
|
|
try:
|
|
|
|
self.config.lock()
|
|
|
|
self._locked = True
|
2016-05-16 12:11:15 +00:00
|
|
|
except LockError:
|
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._log('unable to lock config: %s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
def check_config(self):
|
|
|
|
if not self.config.commit_check():
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error(msg='Commit check failed')
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
def commit_config(self, comment=None, confirm=None):
|
|
|
|
try:
|
|
|
|
kwargs = dict(comment=comment)
|
|
|
|
if confirm and confirm > 0:
|
|
|
|
kwargs['confirm'] = confirm
|
|
|
|
return self.config.commit(**kwargs)
|
2016-05-16 12:11:15 +00:00
|
|
|
except CommitError:
|
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error('Unable to commit configuration: %s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def load_config(self, candidate, action='replace', comment=None, confirm=None, format='text', commit=True):
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
merge = action == 'merge'
|
|
|
|
overwrite = action == 'overwrite'
|
|
|
|
|
|
|
|
self.lock_config()
|
|
|
|
|
|
|
|
try:
|
2016-08-31 18:34:15 +00:00
|
|
|
self.config.load(candidate, format=format, merge=merge, overwrite=overwrite)
|
2016-05-16 12:11:15 +00:00
|
|
|
except ConfigLoadError:
|
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error('Unable to load config: %s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
diff = self.config.diff()
|
|
|
|
self.check_config()
|
|
|
|
if commit and diff:
|
|
|
|
self.commit_config(comment=comment, confirm=confirm)
|
|
|
|
|
|
|
|
self.unlock_config()
|
|
|
|
|
|
|
|
return diff
|
|
|
|
|
|
|
|
def rollback_config(self, identifier, commit=True, comment=None):
|
|
|
|
|
|
|
|
self.lock_config()
|
|
|
|
|
|
|
|
try:
|
2016-08-31 18:34:15 +00:00
|
|
|
self.config.rollback(identifier)
|
|
|
|
except ValueError:
|
2016-05-16 12:11:15 +00:00
|
|
|
exc = get_exception()
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error('Unable to rollback config: $s' % str(exc))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
diff = self.config.diff()
|
|
|
|
if commit:
|
|
|
|
self.commit_config(comment=comment)
|
|
|
|
|
|
|
|
self.unlock_config()
|
|
|
|
return diff
|
|
|
|
|
|
|
|
def get_facts(self, refresh=True):
|
|
|
|
if refresh:
|
|
|
|
self.device.facts_refresh()
|
|
|
|
return self.device.facts
|
|
|
|
|
2016-04-24 11:43:21 +00:00
|
|
|
def get_config(self, config_format="text"):
|
2016-04-24 12:20:14 +00:00
|
|
|
if config_format not in ['text', 'set', 'xml']:
|
|
|
|
msg = 'invalid config format... must be one of xml, text, set'
|
2016-08-31 18:34:15 +00:00
|
|
|
self._error(msg=msg)
|
2016-04-24 11:43:21 +00:00
|
|
|
|
2016-04-24 12:20:14 +00:00
|
|
|
ele = self.rpc('get_configuration', format=config_format)
|
|
|
|
if config_format in ['text', 'set']:
|
2016-08-31 18:34:15 +00:00
|
|
|
return str(ele.text).strip()
|
2016-04-24 11:43:21 +00:00
|
|
|
elif config_format == "xml":
|
|
|
|
return ele
|
2016-04-06 02:44:07 +00:00
|
|
|
|
|
|
|
def rpc(self, name, format='xml', **kwargs):
|
|
|
|
meth = getattr(self.device.rpc, name)
|
|
|
|
reply = meth({'format': format}, **kwargs)
|
|
|
|
return reply
|
2016-08-31 18:34:15 +00:00
|
|
|
Netconf = register_transport('netconf')(Netconf)
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
class Cli(CliBase):
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
CLI_PROMPTS_RE = [
|
|
|
|
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
|
|
|
|
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
|
|
|
|
]
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
CLI_ERRORS_RE = [
|
|
|
|
re.compile(r"% ?Error"),
|
|
|
|
re.compile(r"% ?Bad secret"),
|
|
|
|
re.compile(r"invalid input", re.I),
|
|
|
|
re.compile(r"(?:incomplete|ambiguous) command", re.I),
|
|
|
|
re.compile(r"connection timed out", re.I),
|
|
|
|
re.compile(r"[^\r\n]+ not found", re.I),
|
|
|
|
re.compile(r"'[^']' +returned error code: ?\d+"),
|
|
|
|
]
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def connect(self, params, **kwargs):
|
|
|
|
super(Cli, self).connect(params, **kwargs)
|
2016-01-19 17:57:14 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
if self.shell._matched_prompt.strip().endswith('%'):
|
|
|
|
self.execute('cli')
|
|
|
|
self.execute('set cli screen-length 0')
|
2016-04-25 20:04:19 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def authorize(self, params, **kwargs):
|
|
|
|
raise NotImplementedError
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
### Config methods ###
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def configure(self, commands, **kwargs):
|
|
|
|
cmds = ['configure']
|
|
|
|
cmds.extend(to_list(commands))
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
if kwargs.get('comment'):
|
|
|
|
cmds.append('commit and-quit comment "%s"' % kwargs.get('comment'))
|
|
|
|
else:
|
|
|
|
cmds.append('commit and-quit')
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
responses = self.execute(cmds)
|
|
|
|
return responses[1:-1]
|
2016-04-06 02:44:07 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def get_config(self, include_defaults=False, **kwargs):
|
|
|
|
raise NotImplementedError
|
2016-01-10 15:33:00 +00:00
|
|
|
|
2016-08-31 18:34:15 +00:00
|
|
|
def save_config(self):
|
|
|
|
self.execute(['copy running-config startup-config'])
|
|
|
|
Cli = register_transport('cli', default=True)(Cli)
|