| 1 |
# |
| 2 |
# (c) 2016 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
|
| 20 |
from __future__ import (absolute_import, division, print_function) |
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
# Py 2.7 compat. |
| 24 |
from ansible.module_utils.six import raise_from |
| 25 |
|
| 26 |
import json |
| 27 |
import re |
| 28 |
|
| 29 |
from ansible.errors import AnsibleConnectionFailure |
| 30 |
from ansible.module_utils._text import to_text, to_bytes |
| 31 |
from ansible.plugins.terminal import TerminalBase |
| 32 |
from ansible.utils.display import Display |
| 33 |
|
| 34 |
display = Display() |
| 35 |
|
| 36 |
|
| 37 |
class TerminalModule(TerminalBase): |
| 38 |
|
| 39 |
# https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/network_cli_connection.html |
| 40 |
|
| 41 |
terminal_stdout_re = [ |
| 42 |
re.compile(br"[\r\n]?[\w\+\-\.:\/\[\]]+(?:\([^\)]+\)){0,3}(?:[>#]) ?$") |
| 43 |
] |
| 44 |
|
| 45 |
terminal_stderr_re = [ |
| 46 |
re.compile(br"% ?Error"), |
| 47 |
re.compile(br"^% \w+", re.M), |
| 48 |
re.compile(br"% ?Bad secret"), |
| 49 |
re.compile(br"[\r\n%] Bad passwords"), |
| 50 |
re.compile(br"invalid input", re.I), |
| 51 |
re.compile(br"(?:incomplete|ambiguous) command", re.I), |
| 52 |
re.compile(br"connection timed out", re.I), |
| 53 |
re.compile(br"[^\r\n]+ not found"), |
| 54 |
re.compile(br"'[^']' +returned error code: ?\d+"), |
| 55 |
re.compile(br"Bad mask", re.I), |
| 56 |
re.compile(br"% ?(\S+) ?overlaps with ?(\S+)", re.I), |
| 57 |
re.compile(br"[%\S] ?Error: ?[\s]+", re.I), |
| 58 |
re.compile(br"[%\S] ?Informational: ?[\s]+", re.I), |
| 59 |
re.compile(br"Command authorization failed"), |
| 60 |
] |
| 61 |
|
| 62 |
def on_open_shell(self): |
| 63 |
try: |
| 64 |
self._exec_cli_command(b"terminal datadump") |
| 65 |
except AnsibleConnectionFailure as e: |
| 66 |
raise_from(AnsibleConnectionFailure("unable to set terminal parameters"), e) |
| 67 |
|
| 68 |
try: |
| 69 |
self._exec_cli_command(b"terminal width 0") |
| 70 |
except AnsibleConnectionFailure: |
| 71 |
display.display( |
| 72 |
"WARNING: Unable to set terminal width, command responses may be truncated" |
| 73 |
) |
| 74 |
|
| 75 |
try: |
| 76 |
self._exec_cli_command(b"terminal no prompt") |
| 77 |
except AnsibleConnectionFailure: |
| 78 |
display.display( |
| 79 |
"WARNING: Unable disable prompt, command responses may fail" |
| 80 |
) |
| 81 |
|
| 82 |
def on_become(self, passwd=None): |
| 83 |
cmd = {u"command": u"enable"} |
| 84 |
if passwd: |
| 85 |
# Note: python-3.5 cannot combine u"" and r"" together. Thus make |
| 86 |
# an r string and use to_text to ensure it's text on both py2 and py3. |
| 87 |
cmd[u"prompt"] = to_text( |
| 88 |
r"[\r\n]?(?:.*)?[Pp]assword: ?$", errors="surrogate_or_strict" |
| 89 |
) |
| 90 |
cmd[u"answer"] = passwd |
| 91 |
cmd[u"prompt_retry_check"] = True |
| 92 |
try: |
| 93 |
self._exec_cli_command( |
| 94 |
to_bytes(json.dumps(cmd), errors="surrogate_or_strict") |
| 95 |
) |
| 96 |
prompt = self._get_prompt() |
| 97 |
if prompt is None or not prompt.endswith(b"#"): |
| 98 |
raise AnsibleConnectionFailure( |
| 99 |
"failed to elevate privilege to enable mode still at prompt [%s]" |
| 100 |
% prompt |
| 101 |
) |
| 102 |
except AnsibleConnectionFailure as e: |
| 103 |
prompt = self._get_prompt() |
| 104 |
raise_from(AnsibleConnectionFailure( |
| 105 |
"unable to elevate privilege to enable mode, at prompt [%s] with error: %s" |
| 106 |
% (prompt, e.message) |
| 107 |
), e) |
| 108 |
|
| 109 |
def on_unbecome(self): |
| 110 |
prompt = self._get_prompt() |
| 111 |
if prompt is None: |
| 112 |
# if prompt is None most likely the terminal is hung up at a prompt |
| 113 |
return |
| 114 |
|
| 115 |
if b"(config" in prompt: |
| 116 |
self._exec_cli_command(b"end") |
| 117 |
self._exec_cli_command(b"disable") |
| 118 |
|
| 119 |
elif prompt.endswith(b"#"): |
| 120 |
self._exec_cli_command(b"disable") |