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.
This commit is contained in:
Ganesh Nalawade 2018-08-23 19:16:42 +05:30 committed by Nathaniel Case
parent 24c26aded8
commit e25d8e2b99
2 changed files with 9 additions and 3 deletions

View file

@ -124,6 +124,9 @@ class CliconfBase(AnsiblePlugin):
else:
kwargs['prompt'] = to_bytes(prompt)
if answer is not None:
if isinstance(answer, list):
kwargs['answer'] = [to_bytes(p) for p in answer]
else:
kwargs['answer'] = to_bytes(answer)
resp = self._connection.send(**kwargs)

View file

@ -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')