From fa0d39a1b4281d6dd7b087ea491f92f6a518b2ca Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Wed, 24 Aug 2016 11:03:01 -0400 Subject: [PATCH] updates iosxr shared module This completes the refactor of the iosxr 2.2 shared module. It also includes the iosxr_config action plugin to be implemented by the iosxr_config module for 2.2 --- lib/ansible/module_utils/iosxr.py | 81 ++++++++++++++-------- lib/ansible/plugins/action/iosxr_config.py | 28 ++++++++ 2 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 lib/ansible/plugins/action/iosxr_config.py diff --git a/lib/ansible/module_utils/iosxr.py b/lib/ansible/module_utils/iosxr.py index 357ef6b42df..c3f50370df7 100644 --- a/lib/ansible/module_utils/iosxr.py +++ b/lib/ansible/module_utils/iosxr.py @@ -28,10 +28,14 @@ import re -from ansible.module_utils.network import Command, NetCli, NetworkError, get_module +from ansible.module_utils.network import NetworkModule, NetworkError +from ansible.module_utils.network import Command +from ansible.module_utils.shell import CliBase from ansible.module_utils.network import register_transport, to_list -class Cli(NetCli): + +class Cli(CliBase): + CLI_PROMPTS_RE = [ re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$") @@ -53,35 +57,56 @@ class Cli(NetCli): super(Cli, self).connect(params, kickstart=False, **kwargs) self.shell.send('terminal length 0') - ### implementation of network.Cli ### - - def configure(self, commands, **kwargs): - cmds = ['configure'] - cmds.extend(to_list(commands)) - cmds.append('end') - - responses = self.execute(cmds) - return responses[1:-1] - - def get_config(self, params, **kwargs): - return self.run_commands('show running-config')[0] - - def load_config(self, commands, commit=False, **kwargs): - raise NotImplementedError - - def replace_config(self, commands, **kwargs): - raise NotImplementedError - - def commit_config(self, **kwargs): - command = 'commit' - self.run_commands([command]) - - def abort_config(self, **kwargs): - command = 'abort' - self.run_commands([command]) + ### implementation of netcli.Cli ### def run_commands(self, commands): cmds = to_list(commands) responses = self.execute(cmds) return responses + + ### immplementation of netcfg.Config ### + + def configure(self, commands, **kwargs): + cmds = ['configure terminal'] + cmds.extend(to_list(commands)) + responses = self.execute(cmds) + return responses[1:] + + def get_config(self, flags=None, **kwargs): + cmd = 'show running-config' + if flags: + if isinstance(flags, list): + cmd += ' %s' % ' '.join(flags) + else: + cmd += ' %s' % flags + return self.execute([cmd])[0] + + def load_config(self, config, replace=False, commit=False, **kwargs): + commands = ['configure terminal'] + commands.extend(config) + + if commands[-1] == 'end': + commands.pop() + + try: + self.execute(commands) + diff = self.execute(['show commit changes diff']) + if commit: + if replace: + prompt = re.compile(r'\[no\]:\s$') + cmd = Command('commit replace', prompt=prompt, + response='yes') + self.execute([cmd, 'end']) + else: + self.execute(['commit', 'end']) + except NetworkError: + self.execute(['abort']) + diff = None + raise + return diff[0] + + def save_config(self): + raise NotImplementedError + + Cli = register_transport('cli', default=True)(Cli) diff --git a/lib/ansible/plugins/action/iosxr_config.py b/lib/ansible/plugins/action/iosxr_config.py new file mode 100644 index 00000000000..ffcb0f057f8 --- /dev/null +++ b/lib/ansible/plugins/action/iosxr_config.py @@ -0,0 +1,28 @@ +# +# Copyright 2015 Peter Sprygada +# +# 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 . +# +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible.plugins.action import ActionBase +from ansible.plugins.action.net_config import ActionModule as NetActionModule + +class ActionModule(NetActionModule, ActionBase): + pass + +