mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
To set the right CLI context i.e. Privilege (Enable) Mode soon after connection. (#32966)
This commit is contained in:
parent
47eaf662a7
commit
e24e771b88
1 changed files with 17 additions and 0 deletions
|
@ -41,6 +41,8 @@ from ansible import constants as C
|
||||||
from ansible.plugins.action.normal import ActionModule as _ActionModule
|
from ansible.plugins.action.normal import ActionModule as _ActionModule
|
||||||
from ansible.module_utils.enos import enos_provider_spec
|
from ansible.module_utils.enos import enos_provider_spec
|
||||||
from ansible.module_utils.network_common import load_provider
|
from ansible.module_utils.network_common import load_provider
|
||||||
|
from ansible.module_utils.connection import Connection
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -53,6 +55,8 @@ except ImportError:
|
||||||
class ActionModule(_ActionModule):
|
class ActionModule(_ActionModule):
|
||||||
|
|
||||||
def run(self, tmp=None, task_vars=None):
|
def run(self, tmp=None, task_vars=None):
|
||||||
|
|
||||||
|
socket_path = None
|
||||||
if self._play_context.connection == 'local':
|
if self._play_context.connection == 'local':
|
||||||
provider = load_provider(enos_provider_spec, self._task.args)
|
provider = load_provider(enos_provider_spec, self._task.args)
|
||||||
pc = copy.deepcopy(self._play_context)
|
pc = copy.deepcopy(self._play_context)
|
||||||
|
@ -79,5 +83,18 @@ class ActionModule(_ActionModule):
|
||||||
|
|
||||||
task_vars['ansible_socket'] = socket_path
|
task_vars['ansible_socket'] = socket_path
|
||||||
|
|
||||||
|
# make sure we are in the right cli context which should be
|
||||||
|
# enable mode and not config module or exec mode
|
||||||
|
if socket_path is None:
|
||||||
|
socket_path = self._connection.socket_path
|
||||||
|
|
||||||
|
conn = Connection(socket_path)
|
||||||
|
out = conn.get_prompt()
|
||||||
|
if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'):
|
||||||
|
display.vvvv('In Config mode, sending exit to device', self._play_context.remote_addr)
|
||||||
|
conn.send_command('exit')
|
||||||
|
else:
|
||||||
|
conn.send_command('enable')
|
||||||
|
|
||||||
result = super(ActionModule, self).run(tmp, task_vars)
|
result = super(ActionModule, self).run(tmp, task_vars)
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Reference in a new issue