Network Module: EOS (#16158)

* add new module network

* move EOS to NetworkModule

* shell.py Python 3.x compatibility

* implements the Command class through the connection for eos

This implements a new Command class that specifies the cli command
and output format.  This removes the need to batch commands through
the connection

* initial add of netcmd module
This commit is contained in:
Nathaniel Case 2016-06-20 12:11:48 -04:00 committed by GitHub
commit 5dccff29bf
5 changed files with 933 additions and 335 deletions

View file

@ -17,78 +17,224 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
import collections
import re
from ansible.module_utils.basic import AnsibleModule, env_fallback, get_exception
from ansible.module_utils.shell import Shell, ShellError, Command, HAS_PARAMIKO
from ansible.module_utils.netcfg import parse
from ansible.module_utils.urls import fetch_url
from ansible.module_utils.basic import json
from ansible.module_utils.network import NetCli, NetworkError, get_module, Command
from ansible.module_utils.network import add_argument, register_transport, to_list
from ansible.module_utils.netcfg import NetworkConfig
from ansible.module_utils.urls import fetch_url, url_argument_spec
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
NET_COMMON_ARGS = dict(
host=dict(required=True),
port=dict(type='int'),
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'),
authorize=dict(default=False, fallback=(env_fallback, ['ANSIBLE_NET_AUTHORIZE']), type='bool'),
auth_pass=dict(no_log=True, fallback=(env_fallback, ['ANSIBLE_NET_AUTH_PASS'])),
transport=dict(default='cli', choices=['cli', 'eapi']),
use_ssl=dict(default=True, type='bool'),
provider=dict(type='dict')
)
EAPI_FORMATS = ['json', 'text']
CLI_PROMPTS_RE = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
add_argument('use_ssl', dict(default=True, type='bool'))
add_argument('validate_certs', dict(default=True, type='bool'))
CLI_ERRORS_RE = [
re.compile(r"% ?Error"),
re.compile(r"^% \w+", re.M),
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+"),
re.compile(r"[^\r\n]\/bin\/(?:ba)?sh")
]
ModuleStub = collections.namedtuple('ModuleStub', 'params fail_json')
def argument_spec():
return dict(
# config options
running_config=dict(aliases=['config']),
config_session=dict(default='ansible_session'),
save_config=dict(default=False, aliases=['save']),
force=dict(type='bool', default=False)
)
eos_argument_spec = argument_spec()
def to_list(val):
if isinstance(val, (list, tuple)):
return list(val)
elif val is not None:
return [val]
def get_config(module):
config = module.params['running_config']
if not config:
config = module.config.get_config(include_defaults=False)
return NetworkConfig(indent=3, contents=config)
def load_config(module, candidate):
if not module.params['force']:
config = get_config(module)
commands = candidate.difference(config)
else:
return list()
commands = str(candidate)
commands = [str(c).strip() for c in commands]
class Eapi(object):
session = module.params['config_session']
save_config = module.params['save_config']
def __init__(self, module):
self.module = module
result = dict(changed=False)
# sets the module_utils/urls.py req parameters
self.module.params['url_username'] = module.params['username']
self.module.params['url_password'] = module.params['password']
if commands:
if module._diff:
diff = module.config.load_config(commands, session_name=session)
if diff:
result['diff'] = dict(prepared=diff)
if not module.check_mode:
module.config.commit_config(session)
if save_config:
module.config.save_config()
else:
module.config.abort_config(session_name=session)
if not module.check_mode:
module.config(commands)
if save_config:
module.config.save_config()
result['changed'] = True
result['updates'] = commands
return result
def expand_intf_range(interfaces):
match = re.match(r'([a-zA-Z]+)(.+)', interfaces)
if not match:
raise ValueError('could not parse interface range')
name = match.group(1)
values = match.group(2).split(',')
indicies = list()
for val in values:
tokens = val.split('-')
# single index value to handle
if len(tokens) == 1:
indicies.append(tokens[0])
elif len(tokens) == 2:
pairs = list()
mod = 0
for token in tokens:
parts = token.split('/')
if len(parts) == 1:
port = parts[0]
if port == '$':
port = last_port
pairs.append((mod, int(port)))
elif len(parts) == 2:
mod = int(parts[0])
port = parts[1]
if port == '$':
port = last_port
pairs.append((mod, int(port)))
else:
raise ValueError('unable to parse interface')
if pairs[0][0] == pairs[1][0]:
# same module
mod = pairs[0][0]
start = pairs[0][1]
end = pairs[1][1] + 1
for i in range(start, end):
if mod == 0:
indicies.append(i)
else:
indicies.append('%s/%s' % (mod, i))
else:
# span modules
start_mod, start_port = pairs[0]
end_mod, end_port = pairs[1]
end_port += 1
for i in range(start_port, last_port+1):
indicies.append('%s/%s' % (start_mod, i))
for i in range(first_port, end_port):
indicies.append('%s/%s' % (end_mod, i))
return ['%s%s' % (name, index) for index in indicies]
class EosConfigMixin(object):
def configure(self, commands, **kwargs):
commands = prepare_config(commands)
responses = self.execute(commands)
responses.pop(0)
return responses
def get_config(self, **kwargs):
cmd = 'show running-config'
if kwargs.get('include_defaults') is True:
cmd += ' all'
return self.execute([cmd])[0]
def load_config(self, commands, session_name='ansible_temp_session', **kwargs):
commands = to_list(commands)
commands.insert(0, 'configure session %s' % session_name)
commands.append('show session-config diffs')
commands.append('end')
responses = self.execute(commands)
return responses[-2]
def replace_config(self, contents, params, **kwargs):
remote_user = params['username']
remote_path = '/home/%s/ansible-config' % remote_user
commands = [
'bash echo "%s" > %s' % (contents, remote_path),
'diff running-config file:/%s' % remote_path,
'config replace file:/%s' % remote_path,
]
responses = self.run_commands(commands)
return responses[-2]
def commit_config(self, session_name):
session = 'configure session %s' % session_name
commands = [session, 'commit', 'no %s' % session]
self.execute(commands)
def abort_config(self, session_name):
command = 'no configure session %s' % session_name
self.execute([command])
def save_config(self):
self.execute(['copy running-config startup-config'])
class Eapi(EosConfigMixin):
def __init__(self):
self.url = None
self.url_args = ModuleStub(url_argument_spec(), self._error)
self.enable = None
self.default_output = 'json'
self._connected = False
def _get_body(self, commands, encoding, reqid=None):
def _error(self, msg):
raise NetworkError(msg, url=self.url)
def _get_body(self, commands, format, reqid=None):
"""Create a valid eAPI JSON-RPC request message
"""
params = dict(version=1, cmds=commands, format=encoding)
if format not in EAPI_FORMATS:
msg = 'invalid format, received %s, expected one of %s' % \
(format, ','.join(EAPI_FORMATS))
self._error(msg=msg)
params = dict(version=1, cmds=commands, format=format)
return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params)
def connect(self):
host = self.module.params['host']
port = self.module.params['port']
def connect(self, params, **kwargs):
host = params['host']
port = params['port']
if self.module.params['use_ssl']:
# sets the module_utils/urls.py req parameters
self.url_args.params['url_username'] = params['username']
self.url_args.params['url_password'] = params['password']
self.url_args.params['validate_certs'] = params['validate_certs']
if params['use_ssl']:
proto = 'https'
if not port:
port = 443
@ -98,176 +244,146 @@ class Eapi(object):
port = 80
self.url = '%s://%s:%s/command-api' % (proto, host, port)
self._connected = True
def authorize(self):
if self.module.params['auth_pass']:
passwd = self.module.params['auth_pass']
def disconnect(self, **kwargs):
self.url = None
self._connected = False
def authorize(self, params, **kwargs):
if params.get('auth_pass'):
passwd = params['auth_pass']
self.enable = dict(cmd='enable', input=passwd)
else:
self.enable = 'enable'
def send(self, commands, encoding='json'):
### implementation of network.Cli ###
def run_commands(self, commands):
output = None
cmds = list()
responses = list()
for cmd in commands:
if output and output != cmd.output:
responses.extend(self.execute(cmds, format=output))
cmds = list()
output = cmd.output
cmds.append(str(cmd))
if cmds:
responses.extend(self.execute(cmds, format=output))
for index, cmd in enumerate(commands):
if cmd.output == 'text':
responses[index] = responses[index].get('output')
return responses
def execute(self, commands, format='json', **kwargs):
"""Send commands to the device.
"""
clist = to_list(commands)
if self.url is None:
raise NetworkError('Not connected to endpoint.')
if self.enable is not None:
clist.insert(0, self.enable)
commands.insert(0, self.enable)
data = self._get_body(clist, encoding)
data = self.module.jsonify(data)
data = self._get_body(commands, format)
data = json.dumps(data)
headers = {'Content-Type': 'application/json-rpc'}
response, headers = fetch_url(self.module, self.url, data=data,
headers=headers, method='POST')
response, headers = fetch_url(
self.url_args, self.url, data=data, headers=headers,
method='POST'
)
if headers['status'] != 200:
self.module.fail_json(**headers)
raise NetworkError(**headers)
try:
response = json.loads(response.read())
except ValueError:
raise NetworkError('unable to load response from device')
response = self.module.from_json(response.read())
if 'error' in response:
err = response['error']
self.module.fail_json(msg='json-rpc error', commands=commands, **err)
raise NetworkError(
msg=err['message'], code=err['code'], data=err['data'],
commands=commands
)
if self.enable:
response['result'].pop(0)
return response['result']
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']
key_filename = self.module.params['ssh_keyfile']
try:
self.shell = Shell(prompts_re=CLI_PROMPTS_RE, errors_re=CLI_ERRORS_RE)
self.shell.open(host, port=port, username=username, password=password, key_filename=key_filename)
except ShellError:
e = get_exception()
msg = 'failed to connect to %s:%s - %s' % (host, port, str(e))
self.module.fail_json(msg=msg)
def authorize(self):
passwd = self.module.params['auth_pass']
self.send(Command('enable', prompt=NET_PASSWD_RE, response=passwd))
def send(self, commands):
try:
return self.shell.send(commands)
except ShellError:
e = get_exception()
self.module.fail_json(msg=e.message, commands=commands)
def get_config(self, **kwargs):
return self.run_commands(['show running-config'], format='text')[0]
Eapi = register_transport('eapi')(Eapi)
class NetworkModule(AnsibleModule):
class Cli(NetCli, EosConfigMixin):
CLI_PROMPTS_RE = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
]
def __init__(self, *args, **kwargs):
super(NetworkModule, self).__init__(*args, **kwargs)
self.connection = None
self._config = None
self._connected = False
CLI_ERRORS_RE = [
re.compile(r"% ?Error"),
re.compile(r"^% \w+", re.M),
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+"),
re.compile(r"[^\r\n]\/bin\/(?:ba)?sh")
]
@property
def connected(self):
return self._connected
@property
def config(self):
if not self._config:
self._config = self.get_config()
return self._config
def _load_params(self):
super(NetworkModule, self)._load_params()
provider = self.params.get('provider') or dict()
for key, value in provider.items():
if key in NET_COMMON_ARGS:
if self.params.get(key) is None and value is not None:
self.params[key] = value
def connect(self):
cls = globals().get(str(self.params['transport']).capitalize())
try:
self.connection = cls(self)
except TypeError:
e = get_exception()
self.fail_json(msg=e.message)
self.connection.connect()
self.connection.send('terminal length 0')
if self.params['authorize']:
self.connection.authorize()
def __init__(self):
super(Cli, self).__init__()
def connect(self, params, **kwargs):
super(Cli, self).connect(params, kickstart=True, **kwargs)
self.shell.send('terminal length 0')
self._connected = True
def configure(self, commands, replace=False):
if replace:
responses = self.config_replace(commands)
else:
responses = self.config_terminal(commands)
def authorize(self, params, **kwargs):
passwd = params['auth_pass']
self.execute(Command('enable', prompt=NET_PASSWD_RE, response=passwd))
### implementation of network.Cli ###
def run_commands(self, commands):
cmds = list(prepare_commands(commands))
responses = self.execute(cmds)
for index, cmd in enumerate(commands):
if cmd.output == 'json':
try:
responses[index] = json.loads(responses[index])
except ValueError:
raise NetworkError(
msg='unable to load response from device',
response=responses[index]
)
return responses
Cli = register_transport('cli', default=True)(Cli)
def config_terminal(self, commands):
commands = to_list(commands)
commands.insert(0, 'configure terminal')
responses = self.execute(commands)
responses.pop(0)
return responses
def prepare_config(commands):
commands = to_list(commands)
commands.insert(0, 'configure terminal')
commands.append('end')
return commands
def config_replace(self, commands):
if self.params['transport'] == 'cli':
self.fail_json(msg='config replace only supported over eapi')
cmd = 'configure replace terminal:'
commands = '\n'.join(to_list(commands))
command = dict(cmd=cmd, input=commands)
return self.execute(command)
def execute(self, commands, **kwargs):
if not self.connected:
self.connect()
return self.connection.send(commands, **kwargs)
def disconnect(self):
self.connection.close()
self._connected = False
def parse_config(self, cfg):
return parse(cfg, indent=3)
def get_config(self):
cmd = 'show running-config'
if self.params.get('include_defaults'):
cmd += ' all'
if self.params['transport'] == 'cli':
return self.execute(cmd)[0]
def prepare_commands(commands):
jsonify = lambda x: '%s | json' % x
for cmd in to_list(commands):
if cmd.output == 'json':
cmd = jsonify(cmd)
else:
resp = self.execute(cmd, encoding='text')
return resp[0]['output']
def get_module(**kwargs):
"""Return instance of NetworkModule
"""
argument_spec = NET_COMMON_ARGS.copy()
if kwargs.get('argument_spec'):
argument_spec.update(kwargs['argument_spec'])
kwargs['argument_spec'] = argument_spec
module = NetworkModule(**kwargs)
if module.params['transport'] == 'cli' and not HAS_PARAMIKO:
module.fail_json(msg='paramiko is required but does not appear to be installed')
return module
cmd = str(cmd)
yield cmd