Add prompt check in action plugin for network platform (#32787)

* Add prompt check in action plugin for network platform

In case of ignore_errors for a wrong configuration
the prompt is left in configuration mode and moved to
next task, if the next taks requires prompt to be
in operational state it results in failure.
Hence add a check to ensure right prompt at start of
each task run.

* Add prompt check in action plugin for network platform

*  In case of ignore_errors for a wrong configuration
   the prompt is left in configuration mode and moved to
   next task, if the next taks requires prompt to be
   in operational state it results in failure.
*  Hence add a check to ensure right prompt at start of
   each task run.
*  Fix CI issue

* Fix CI issues
Fix review comment
Change iosxr exit command to abort as per review comment
This commit is contained in:
Ganesh Nalawade 2017-11-14 17:37:52 +05:30 committed by Kedar K
parent 414eaefcb5
commit 8472a53bea
14 changed files with 149 additions and 49 deletions

View file

@ -21,6 +21,8 @@ import sys
import copy
from ansible import constants as C
from ansible.module_utils._text import to_text
from ansible.module_utils.connection import Connection
from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.module_utils.network_common import load_provider
@ -67,10 +69,23 @@ class ActionModule(ActionBase):
play_context.become = self.provider['authorize'] or False
play_context.become_pass = self.provider['auth_pass']
socket_path = None
if self._play_context.connection == 'local':
socket_path = self._start_connection(play_context)
task_vars['ansible_socket'] = socket_path
if play_context.connection == 'network_cli':
# make sure we are in the right cli context which should be
# enable mode and not config module
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('wrong context, sending exit to device', self._play_context.remote_addr)
conn.send_command('exit')
if 'fail_on_missing_module' not in self._task.args:
self._task.args['fail_on_missing_module'] = False
@ -119,13 +134,6 @@ class ActionModule(ActionBase):
'msg': 'unable to open shell. Please see: ' +
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
# make sure we are in the right cli context which should be
# enable mode and not config module
rc, out, err = connection.exec_command('prompt()')
if str(out).strip().endswith(')#'):
display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
connection.exec_command('exit')
if self._play_context.become_method == 'enable':
self._play_context.become = False
self._play_context.become_method = None