diff --git a/lib/ansible/plugins/action/enos.py b/lib/ansible/plugins/action/enos.py index f0c9c4bdef..5753505b4f 100644 --- a/lib/ansible/plugins/action/enos.py +++ b/lib/ansible/plugins/action/enos.py @@ -41,6 +41,8 @@ from ansible import constants as C from ansible.plugins.action.normal import ActionModule as _ActionModule from ansible.module_utils.enos import enos_provider_spec 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: @@ -53,6 +55,8 @@ except ImportError: class ActionModule(_ActionModule): def run(self, tmp=None, task_vars=None): + + socket_path = None if self._play_context.connection == 'local': provider = load_provider(enos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) @@ -79,5 +83,18 @@ class ActionModule(_ActionModule): 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) return result