mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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.
This commit is contained in:
parent
a3cc24efb2
commit
7c6916f8d1
1 changed files with 46 additions and 73 deletions
|
@ -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:
|
||||||
|
return _DEVICE_CONFIGS[cmd]
|
||||||
|
except KeyError:
|
||||||
|
rc, out, err = module.exec_command(cmd)
|
||||||
|
if rc != 0:
|
||||||
|
module.fail_json(msg='unable to retrieve current config', stderr=err)
|
||||||
|
cfg = str(out).strip()
|
||||||
|
_DEVICE_CONFIGS[cmd] = cfg
|
||||||
|
return cfg
|
||||||
|
|
||||||
class Cli(CliBase):
|
def run_commands(module, commands, check_rc=True):
|
||||||
|
assert isinstance(commands, list), 'commands must be a list'
|
||||||
|
responses = list()
|
||||||
|
|
||||||
CLI_PROMPTS_RE = [
|
for cmd in commands:
|
||||||
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
|
rc, out, err = module.exec_command(cmd)
|
||||||
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
|
if check_rc and rc != 0:
|
||||||
]
|
module.fail_json(msg=err, rc=rc)
|
||||||
|
responses.append(out)
|
||||||
|
return responses
|
||||||
|
|
||||||
CLI_ERRORS_RE = [
|
def load_config(module, commands, commit=False, replace=False):
|
||||||
re.compile(r"% ?Error"),
|
assert isinstance(commands, list), 'commands must be a list'
|
||||||
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+"),
|
|
||||||
]
|
|
||||||
|
|
||||||
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
|
rc, out, err = module.exec_command('configure terminal')
|
||||||
|
if rc != 0:
|
||||||
|
module.fail_json(msg='unable to enter configuration mode', err=err)
|
||||||
|
|
||||||
def connect(self, params, **kwargs):
|
failed = False
|
||||||
super(Cli, self).connect(params, kickstart=False, **kwargs)
|
for command in commands:
|
||||||
self.shell.send(['terminal length 0', 'terminal exec prompt no-timestamp'])
|
if command == 'end':
|
||||||
|
pass
|
||||||
|
|
||||||
### Config methods ###
|
rc, out, err = module.exec_command(command)
|
||||||
|
if rc != 0:
|
||||||
|
failed = True
|
||||||
|
break
|
||||||
|
|
||||||
def configure(self, commands):
|
if failed:
|
||||||
cmds = ['configure terminal']
|
module.exec_command('abort')
|
||||||
if commands[-1] == 'end':
|
module.fail_json(msg=err, commands=commands, rc=rc)
|
||||||
commands.pop()
|
|
||||||
cmds.extend(to_list(commands))
|
|
||||||
cmds.extend(['commit', 'end'])
|
|
||||||
responses = self.execute(cmds)
|
|
||||||
return responses[1:]
|
|
||||||
|
|
||||||
def get_config(self, flags=None):
|
rc, diff, err = module.exec_command('show commit changes diff')
|
||||||
cmd = 'show running-config'
|
if commit:
|
||||||
if flags:
|
cmd = 'commit'
|
||||||
if isinstance(flags, list):
|
else:
|
||||||
cmd += ' %s' % ' '.join(flags)
|
cmd = 'abort'
|
||||||
else:
|
module.exec_command(cmd)
|
||||||
cmd += ' %s' % flags
|
|
||||||
return self.execute([cmd])[0]
|
|
||||||
|
|
||||||
def load_config(self, config, commit=False, replace=False, comment=None):
|
return {'diff': diff}
|
||||||
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$')
|
|
||||||
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…
Add table
Reference in a new issue