refactors iosxr shared module to use network_cli (#20132)

This refactors the iosxr shared module to make use of the network_cli
connection plugin and removes the dependency on the shared shell
module.  This change will break current modules until the modules
are updated.
pull/20134/head
Peter Sprygada 8 years ago committed by GitHub
parent a3cc24efb2
commit 7c6916f8d1

@ -26,87 +26,60 @@
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
import re _DEVICE_CONFIGS = {}
from ansible.module_utils.netcli import Command def get_config(module, flags=[]):
from ansible.module_utils.network import NetworkError, NetworkModule cmd = 'show running-config '
from ansible.module_utils.network import register_transport, to_list cmd += ' '.join(flags)
from ansible.module_utils.shell import CliBase cmd = cmd.strip()
try:
class Cli(CliBase): return _DEVICE_CONFIGS[cmd]
except KeyError:
CLI_PROMPTS_RE = [ rc, out, err = module.exec_command(cmd)
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), if rc != 0:
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$") module.fail_json(msg='unable to retrieve current config', stderr=err)
] cfg = str(out).strip()
_DEVICE_CONFIGS[cmd] = cfg
CLI_ERRORS_RE = [ return cfg
re.compile(r"% ?Error"),
re.compile(r"% ?Bad secret"), def run_commands(module, commands, check_rc=True):
re.compile(r"invalid input", re.I), assert isinstance(commands, list), 'commands must be a list'
re.compile(r"(?:incomplete|ambiguous) command", re.I), responses = list()
re.compile(r"connection timed out", re.I),
re.compile(r"[^\r\n]+ not found", re.I), for cmd in commands:
re.compile(r"'[^']' +returned error code: ?\d+"), rc, out, err = module.exec_command(cmd)
] if check_rc and rc != 0:
module.fail_json(msg=err, rc=rc)
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I) responses.append(out)
return responses
def connect(self, params, **kwargs):
super(Cli, self).connect(params, kickstart=False, **kwargs) def load_config(module, commands, commit=False, replace=False):
self.shell.send(['terminal length 0', 'terminal exec prompt no-timestamp']) assert isinstance(commands, list), 'commands must be a list'
### Config methods ### rc, out, err = module.exec_command('configure terminal')
if rc != 0:
def configure(self, commands): module.fail_json(msg='unable to enter configuration mode', err=err)
cmds = ['configure terminal']
if commands[-1] == 'end': failed = False
commands.pop() for command in commands:
cmds.extend(to_list(commands)) if command == 'end':
cmds.extend(['commit', 'end']) pass
responses = self.execute(cmds)
return responses[1:] rc, out, err = module.exec_command(command)
if rc != 0:
def get_config(self, flags=None): failed = True
cmd = 'show running-config' break
if flags:
if isinstance(flags, list): if failed:
cmd += ' %s' % ' '.join(flags) module.exec_command('abort')
else: module.fail_json(msg=err, commands=commands, rc=rc)
cmd += ' %s' % flags
return self.execute([cmd])[0] rc, diff, err = module.exec_command('show commit changes diff')
if commit:
def load_config(self, config, commit=False, replace=False, comment=None): cmd = 'commit'
commands = ['configure terminal'] else:
commands.extend(config) cmd = 'abort'
module.exec_command(cmd)
if commands[-1] == 'end':
commands.pop() return {'diff': diff}
try:
self.execute(commands)
diff = self.execute(['show commit changes diff'])
if commit:
if replace:
prompt = re.compile(r'\[no\]:\s$')
commit = 'commit replace'
if comment:
commit += ' comment %s' % comment
cmd = Command(commit, prompt=prompt, response='yes')
self.execute([cmd, 'end'])
else:
commit = 'commit'
if comment:
commit += ' comment %s' % comment
self.execute([commit, 'end'])
else:
self.execute(['abort'])
except NetworkError:
self.execute(['abort'])
diff = None
raise
return diff[0]
Cli = register_transport('cli', default=True)(Cli)

Loading…
Cancel
Save