mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-25 11:51:26 -07:00
nxos enable mode (#39355)
* nxos enable mode Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * fix prompt Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * Add authorize,auth_pass Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * remove byte string from exec_cli_command Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * Add on_become test Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * removed_in_version Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
This commit is contained in:
parent
83df7249fd
commit
f08332acb4
10 changed files with 162 additions and 1 deletions
|
@ -20,9 +20,11 @@ from __future__ import (absolute_import, division, print_function)
|
|||
__metaclass__ = type
|
||||
|
||||
import re
|
||||
import json
|
||||
|
||||
from ansible.plugins.terminal import TerminalBase
|
||||
from ansible.errors import AnsibleConnectionFailure
|
||||
from ansible.module_utils._text import to_bytes, to_text
|
||||
|
||||
|
||||
class TerminalModule(TerminalBase):
|
||||
|
@ -48,9 +50,50 @@ class TerminalModule(TerminalBase):
|
|||
re.compile(br"invalid (.+?)at '\^' marker", re.I)
|
||||
]
|
||||
|
||||
def on_become(self, passwd=None):
|
||||
if self._get_prompt().endswith(b'enable#'):
|
||||
return
|
||||
|
||||
out = self._exec_cli_command('show privilege')
|
||||
out = to_text(out, errors='surrogate_then_replace').strip()
|
||||
if 'Disabled' in out:
|
||||
raise AnsibleConnectionFailure('Feature privilege is not enabled')
|
||||
|
||||
# if already at privilege level 15 return
|
||||
if '15' in out:
|
||||
return
|
||||
|
||||
cmd = {u'command': u'enable'}
|
||||
if passwd:
|
||||
cmd[u'prompt'] = to_text(r"(?i)[\r\n]?Password: $", errors='surrogate_or_strict')
|
||||
cmd[u'answer'] = passwd
|
||||
cmd[u'prompt_retry_check'] = True
|
||||
|
||||
try:
|
||||
self._exec_cli_command(to_bytes(json.dumps(cmd), errors='surrogate_or_strict'))
|
||||
prompt = self._get_prompt()
|
||||
if prompt is None or not prompt.strip().endswith(b'enable#'):
|
||||
raise AnsibleConnectionFailure('failed to elevate privilege to enable mode still at prompt [%s]' % prompt)
|
||||
except AnsibleConnectionFailure as e:
|
||||
prompt = self._get_prompt()
|
||||
raise AnsibleConnectionFailure('unable to elevate privilege to enable mode, at prompt [%s] with error: %s' % (prompt, e.message))
|
||||
|
||||
def on_unbecome(self):
|
||||
prompt = self._get_prompt()
|
||||
if prompt is None:
|
||||
# if prompt is None most likely the terminal is hung up at a prompt
|
||||
return
|
||||
|
||||
if b'(config' in prompt:
|
||||
self._exec_cli_command('end')
|
||||
self._exec_cli_command('exit')
|
||||
|
||||
elif prompt.endswith(b'enable#'):
|
||||
self._exec_cli_command('exit')
|
||||
|
||||
def on_open_shell(self):
|
||||
try:
|
||||
for cmd in (b'terminal length 0', b'terminal width 511'):
|
||||
for cmd in ('terminal length 0', 'terminal width 511'):
|
||||
self._exec_cli_command(cmd)
|
||||
except AnsibleConnectionFailure:
|
||||
raise AnsibleConnectionFailure('unable to set terminal parameters')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue