2016-01-08 20:00:23 +01: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-05 20:06:17 +02:00
|
|
|
import re
|
|
|
|
|
2016-04-25 22:04:19 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule, env_fallback, get_exception
|
|
|
|
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO
|
2016-04-05 20:06:17 +02:00
|
|
|
from ansible.module_utils.netcfg import parse
|
|
|
|
|
2016-01-08 20:00:23 +01:00
|
|
|
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
|
|
|
|
|
|
|
|
NET_COMMON_ARGS = dict(
|
|
|
|
host=dict(required=True),
|
|
|
|
port=dict(default=22, type='int'),
|
2016-04-01 16:53:44 +02:00
|
|
|
username=dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])),
|
|
|
|
password=dict(no_log=True, fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD'])),
|
|
|
|
ssh_keyfile=dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
|
2016-01-19 18:27:19 +01:00
|
|
|
provider=dict()
|
2016-01-08 20:00:23 +01:00
|
|
|
)
|
|
|
|
|
2016-04-04 22:06:01 +02:00
|
|
|
CLI_PROMPTS_RE = [
|
|
|
|
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
|
|
|
|
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
|
|
|
|
]
|
|
|
|
|
|
|
|
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-04-25 22:04:19 +02:00
|
|
|
|
2016-01-08 20:00:23 +01:00
|
|
|
def to_list(val):
|
|
|
|
if isinstance(val, (list, tuple)):
|
|
|
|
return list(val)
|
|
|
|
elif val is not None:
|
|
|
|
return [val]
|
|
|
|
else:
|
|
|
|
return list()
|
|
|
|
|
2016-04-25 22:04:19 +02:00
|
|
|
|
2016-01-08 20:00:23 +01:00
|
|
|
class Cli(object):
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.shell = None
|
|
|
|
|
|
|
|
def connect(self, **kwargs):
|
|
|
|
host = self.module.params['host']
|
|
|
|
port = self.module.params['port'] or 22
|
|
|
|
|
|
|
|
username = self.module.params['username']
|
|
|
|
password = self.module.params['password']
|
2016-04-01 16:53:44 +02:00
|
|
|
key_filename = self.module.params['ssh_keyfile']
|
2016-01-08 20:00:23 +01:00
|
|
|
|
2016-02-17 15:14:41 +01:00
|
|
|
try:
|
2016-04-01 16:53:44 +02:00
|
|
|
self.shell = Shell(kickstart=False, prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE)
|
|
|
|
self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename)
|
2016-04-25 22:04:19 +02:00
|
|
|
except ShellError:
|
|
|
|
e = get_exception()
|
|
|
|
msg = 'failed to connect to %s:%s - %s' % (host, port, str(e))
|
2016-02-17 15:14:41 +01:00
|
|
|
self.module.fail_json(msg=msg)
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
def send(self, commands):
|
2016-04-25 22:04:19 +02:00
|
|
|
try:
|
|
|
|
return self.shell.send(commands)
|
|
|
|
except ShellError:
|
|
|
|
e = get_exception()
|
|
|
|
self.module.fail_json(msg=e.message, commands=commands)
|
|
|
|
|
2016-01-08 20:00:23 +01:00
|
|
|
|
2016-01-19 18:27:19 +01:00
|
|
|
class NetworkModule(AnsibleModule):
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2016-01-19 18:27:19 +01:00
|
|
|
super(NetworkModule, self).__init__(*args, **kwargs)
|
2016-01-08 20:00:23 +01:00
|
|
|
self.connection = None
|
|
|
|
self._config = None
|
2016-04-05 03:49:45 +02:00
|
|
|
self._connected = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connected(self):
|
|
|
|
return self._connected
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def config(self):
|
|
|
|
if not self._config:
|
|
|
|
self._config = self.get_config()
|
|
|
|
return self._config
|
|
|
|
|
2016-01-19 18:27:19 +01:00
|
|
|
def _load_params(self):
|
2016-04-06 20:54:47 +02:00
|
|
|
super(NetworkModule, self)._load_params()
|
|
|
|
provider = self.params.get('provider') or dict()
|
2016-01-19 18:27:19 +01:00
|
|
|
for key, value in provider.items():
|
2016-04-25 22:04:19 +02:00
|
|
|
if key in NET_COMMON_ARGS:
|
2016-04-06 20:54:47 +02:00
|
|
|
if self.params.get(key) is None and value is not None:
|
|
|
|
self.params[key] = value
|
2016-01-19 18:27:19 +01:00
|
|
|
|
2016-01-08 20:00:23 +01:00
|
|
|
def connect(self):
|
2016-04-25 22:04:19 +02:00
|
|
|
self.connection = Cli(self)
|
|
|
|
|
|
|
|
self.connection.connect()
|
|
|
|
self.connection.send('terminal length 0')
|
|
|
|
self._connected = True
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
def configure(self, commands):
|
|
|
|
commands = to_list(commands)
|
|
|
|
commands.insert(0, 'configure terminal')
|
|
|
|
commands.append('commit')
|
|
|
|
responses = self.execute(commands)
|
|
|
|
responses.pop(0)
|
|
|
|
responses.pop()
|
|
|
|
return responses
|
|
|
|
|
|
|
|
def execute(self, commands, **kwargs):
|
2016-04-25 22:04:19 +02:00
|
|
|
if not self.connected:
|
|
|
|
self.connect()
|
|
|
|
return self.connection.send(commands, **kwargs)
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
self.connection.close()
|
2016-04-05 03:49:45 +02:00
|
|
|
self._connected = False
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
def parse_config(self, cfg):
|
|
|
|
return parse(cfg, indent=1)
|
|
|
|
|
|
|
|
def get_config(self):
|
|
|
|
return self.execute('show running-config')[0]
|
|
|
|
|
|
|
|
def get_module(**kwargs):
|
2016-01-19 18:27:19 +01:00
|
|
|
"""Return instance of NetworkModule
|
2016-01-08 20:00:23 +01:00
|
|
|
"""
|
|
|
|
argument_spec = NET_COMMON_ARGS.copy()
|
|
|
|
if kwargs.get('argument_spec'):
|
|
|
|
argument_spec.update(kwargs['argument_spec'])
|
|
|
|
kwargs['argument_spec'] = argument_spec
|
|
|
|
|
2016-01-19 18:27:19 +01:00
|
|
|
module = NetworkModule(**kwargs)
|
2016-01-08 20:00:23 +01:00
|
|
|
|
|
|
|
if not HAS_PARAMIKO:
|
|
|
|
module.fail_json(msg='paramiko is required but does not appear to be installed')
|
|
|
|
|
|
|
|
return module
|