diff options
author | Peter Sprygada <privateip@users.noreply.github.com> | 2016-09-07 08:42:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-07 08:42:34 -0400 |
commit | d67af04c45b8cfc4e7e19cf2d2af2f980db88e7d (patch) | |
tree | ca8b755d188e14061ef5d7ad7ec7cd8daf58b009 | |
parent | 4ed88512e45112f9670560ac3f01707a40a7f5c5 (diff) | |
parent | ca95b2ae4f96e0b296f1ad6152ec3ea0edab7afc (diff) | |
download | ansible-d67af04c45b8cfc4e7e19cf2d2af2f980db88e7d.tar.gz |
Merge pull request #17431 from privateip/eos
minor roll up of fixes to eos shared module
-rw-r--r-- | lib/ansible/module_utils/eos.py | 75 |
1 files changed, 43 insertions, 32 deletions
diff --git a/lib/ansible/module_utils/eos.py b/lib/ansible/module_utils/eos.py index 0511cccbd9..dea26bad95 100644 --- a/lib/ansible/module_utils/eos.py +++ b/lib/ansible/module_utils/eos.py @@ -27,6 +27,7 @@ # import re +import time from ansible.module_utils.basic import json from ansible.module_utils.network import ModuleStub, NetworkError, NetworkModule @@ -34,6 +35,7 @@ from ansible.module_utils.network import add_argument, register_transport, to_li from ansible.module_utils.netcli import Command from ansible.module_utils.shell import CliBase from ansible.module_utils.urls import fetch_url, url_argument_spec +from ansible.module_utils.netcli import Command EAPI_FORMATS = ['json', 'text'] @@ -58,7 +60,7 @@ class EosConfigMixin(object): cmd += ' all' return self.execute([cmd])[0] - def load_config(self, config, session, commit=False, replace=False, **kwargs): + def load_config(self, config, session=None, commit=False, replace=False, **kwargs): """ Loads the configuration into the remote device This method handles the actual loading of the config @@ -70,6 +72,7 @@ class EosConfigMixin(object): :returns list: ordered set of responses from device """ + session = session or 'ansible_%s' % int(time.time()) commands = ['configure session %s' % session] if replace: commands.append('rollback clean-config') @@ -97,7 +100,12 @@ class EosConfigMixin(object): commands = ['configure session %s' % session, 'show session-config diffs', 'end'] - response = self.execute(commands) + + if isinstance(self, Eapi): + response = self.execute(commands, output='text') + else: + response = self.execute(commands) + return response[-2] def commit_config(self, session): @@ -120,16 +128,15 @@ class Eapi(EosConfigMixin): def _error(self, msg): raise NetworkError(msg, url=self.url) - def _get_body(self, commands, format, reqid=None): + def _get_body(self, commands, output, reqid=None): """Create a valid eAPI JSON-RPC request message """ - - if format not in EAPI_FORMATS: + if output not in EAPI_FORMATS: msg = 'invalid format, received %s, expected one of %s' % \ - (format, ','.join(EAPI_FORMATS)) + (output, ', '.join(EAPI_FORMATS)) self._error(msg=msg) - params = dict(version=1, cmds=commands, format=format) + params = dict(version=1, cmds=commands, format=output) return dict(jsonrpc='2.0', id=reqid, method='runCmds', params=params) def connect(self, params, **kwargs): @@ -175,29 +182,7 @@ class Eapi(EosConfigMixin): if self.enable is not None: commands.insert(0, self.enable) - def run_commands(self, commands, **kwargs): - output = None - cmds = list() - responses = list() - - for cmd in commands: - if output and output != cmd.output: - responses.extend(self.execute(cmds, output=output)) - cmds = list() - - output = cmd.output - cmds.append(str(cmd)) - - if cmds: - responses.extend(self.execute(cmds, output=output)) - - for index, cmd in enumerate(commands): - if cmd.output == 'text': - responses[index] = responses[index].get('output') - - return responses - - data = self._get_body(commands, format) + data = self._get_body(commands, output) data = json.dumps(data) headers = {'Content-Type': 'application/json-rpc'} @@ -227,10 +212,36 @@ class Eapi(EosConfigMixin): return response['result'] + + def run_commands(self, commands, **kwargs): + output = None + cmds = list() + responses = list() + + for cmd in commands: + if output and output != cmd.output: + responses.extend(self.execute(cmds, output=output)) + cmds = list() + + output = cmd.output + cmds.append(str(cmd)) + + if cmds: + responses.extend(self.execute(cmds, output=output)) + + for index, cmd in enumerate(commands): + if cmd.output == 'text': + responses[index] = responses[index].get('output') + + return responses + ### Config methods ### - def get_config(self, **kwargs): - return self.execute(['show running-config'], output='text')[0]['output'] + def get_config(self, include_defaults=False): + cmd = 'show running-config' + if include_defaults: + cmd += ' all' + return self.execute([cmd], output='text')[0]['output'] Eapi = register_transport('eapi')(Eapi) |