From e25d8e2b99721ea3b97ac1cede8a40403a2a0012 Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Thu, 23 Aug 2018 19:16:42 +0530 Subject: [PATCH] Add support for multiple prompt answers in network_cli (#44492) * Currently network_cli support multiple prompts single answer as response. This PR adds support for multiple answers. * In case of multiple prompts and mulitple answers the index of a particular prompt in the prompts list should match with the index in the answer list. --- lib/ansible/plugins/cliconf/__init__.py | 5 ++++- lib/ansible/plugins/connection/network_cli.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ansible/plugins/cliconf/__init__.py b/lib/ansible/plugins/cliconf/__init__.py index 35babaa5003..57aa4201946 100644 --- a/lib/ansible/plugins/cliconf/__init__.py +++ b/lib/ansible/plugins/cliconf/__init__.py @@ -124,7 +124,10 @@ class CliconfBase(AnsiblePlugin): else: kwargs['prompt'] = to_bytes(prompt) if answer is not None: - kwargs['answer'] = to_bytes(answer) + if isinstance(answer, list): + kwargs['answer'] = [to_bytes(p) for p in answer] + else: + kwargs['answer'] = to_bytes(answer) resp = self._connection.send(**kwargs) diff --git a/lib/ansible/plugins/connection/network_cli.py b/lib/ansible/plugins/connection/network_cli.py index d486ccfb0ff..7dfdcbb78cd 100644 --- a/lib/ansible/plugins/connection/network_cli.py +++ b/lib/ansible/plugins/connection/network_cli.py @@ -404,19 +404,22 @@ class Connection(NetworkConnectionBase): :arg resp: Byte string containing the raw response from the remote :arg prompts: Sequence of byte strings that we consider prompts for input - :arg answer: Byte string to send back to the remote if we find a prompt. + :arg answer: Sequence of Byte string to send back to the remote if we find a prompt. A carriage return is automatically appended to this string. :returns: True if a prompt was found in ``resp``. False otherwise ''' if not isinstance(prompts, list): prompts = [prompts] + if not isinstance(answer, list): + answer = [answer] prompts = [re.compile(r, re.I) for r in prompts] - for regex in prompts: + for index, regex in enumerate(prompts): match = regex.search(resp) if match: # if prompt_retry_check is enabled to check if same prompt is # repeated don't send answer again. if not prompt_retry_check: + answer = answer[index] if len(answer) > index else answer[0] self._ssh_shell.sendall(b'%s' % answer) if newline: self._ssh_shell.sendall(b'\r')