Expose newline option to cli_command module (#55451)

* Add newline to all cliconf plugins

* Expose newline in cli_command

* Also hook up to anything using transform_commands directly
This commit is contained in:
Nathaniel Case 2019-05-07 17:25:57 -04:00 committed by GitHub
parent 8ff27c4e0c
commit 38890ddcaf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 67 additions and 53 deletions

View file

@ -80,6 +80,7 @@ def transform_commands(module):
output=dict(), output=dict(),
prompt=dict(type='list'), prompt=dict(type='list'),
answer=dict(type='list'), answer=dict(type='list'),
newline=dict(type='bool', default=True),
sendonly=dict(type='bool', default=False), sendonly=dict(type='bool', default=False),
check_all=dict(type='bool', default=False), check_all=dict(type='bool', default=False),
), module) ), module)

View file

@ -46,6 +46,14 @@ options:
type: bool type: bool
default: false default: false
required: false required: false
newline:
description:
- The boolean value, that when set to false will send I(answer) to the
device without a trailing newline.
type: bool
default: true
required: false
version_added: 2.9
check_all: check_all:
description: description:
- By default if any one of the prompts mentioned in C(prompt) option is matched it won't check - By default if any one of the prompts mentioned in C(prompt) option is matched it won't check
@ -71,6 +79,13 @@ EXAMPLES = """
prompt: This commit will replace or remove the entire running configuration prompt: This commit will replace or remove the entire running configuration
answer: yes answer: yes
- name: run command expecting user confirmation
cli_command:
command: show interface summary
prompt: Press any key to continue
answer: y
newline: false
- name: run config mode command and handle prompt/answer - name: run config mode command and handle prompt/answer
cli_command: cli_command:
command: "{{ item }}" command: "{{ item }}"
@ -129,6 +144,7 @@ def main():
command=dict(type='str', required=True), command=dict(type='str', required=True),
prompt=dict(type='list', required=False), prompt=dict(type='list', required=False),
answer=dict(type='list', required=False), answer=dict(type='list', required=False),
newline=dict(type='bool', default=True, required=False),
sendonly=dict(type='bool', default=False, required=False), sendonly=dict(type='bool', default=False, required=False),
check_all=dict(type='bool', default=False, required=False), check_all=dict(type='bool', default=False, required=False),
) )

View file

@ -79,8 +79,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['config'], to_list(command), ['end']): for cmd in chain(['config'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -80,8 +80,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -77,8 +77,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -92,8 +92,8 @@ class Cliconf(CliconfBase):
results.append(self.send_command(command, prompt, answer, False, newline)) results.append(self.send_command(command, prompt, answer, False, newline))
return results[1:-1] return results[1:-1]
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -110,10 +110,8 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
check_all=False): return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
return self.send_command(command=command, prompt=prompt, answer=answer,
sendonly=sendonly, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -81,8 +81,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(to_bytes(cmd)) self.send_command(to_bytes(cmd))
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -81,8 +81,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -81,8 +81,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -55,8 +55,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure'], to_list(candidate)): for cmd in chain(['configure'], to_list(candidate)):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def commit(self, comment=None): def commit(self, comment=None):
if comment: if comment:

View file

@ -104,13 +104,13 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if not command: if not command:
raise ValueError('must provide value of command to execute') raise ValueError('must provide value of command to execute')
if output: if output:
raise ValueError("'output' value %s is not supported for get" % output) raise ValueError("'output' value %s is not supported for get" % output)
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -77,8 +77,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -138,10 +138,10 @@ class Cliconf(CliconfBase):
self.send_command('end') self.send_command('end')
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if output: if output:
command = self._get_command_with_output(command, output) command = self._get_command_with_output(command, output)
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def commit(self): def commit(self):
self.send_command('commit') self.send_command('commit')

View file

@ -143,10 +143,10 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if output: if output:
command = self._get_command_with_output(command, output) command = self._get_command_with_output(command, output)
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def run_commands(self, commands=None, check_rc=True): def run_commands(self, commands=None, check_rc=True):
if commands is None: if commands is None:

View file

@ -179,13 +179,13 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if not command: if not command:
raise ValueError('must provide value of command to execute') raise ValueError('must provide value of command to execute')
if output: if output:
raise ValueError("'output' value %s is not supported for get" % output) raise ValueError("'output' value %s is not supported for get" % output)
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def run_commands(self, commands=None, check_rc=True): def run_commands(self, commands=None, check_rc=True):
if commands is None: if commands is None:

View file

@ -189,13 +189,13 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if not command: if not command:
raise ValueError('must provide value of command to execute') raise ValueError('must provide value of command to execute')
if output: if output:
raise ValueError("'output' value %s is not supported for get" % output) raise ValueError("'output' value %s is not supported for get" % output)
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_device_info(self): def get_device_info(self):
device_info = {} device_info = {}

View file

@ -80,8 +80,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['end']): for cmd in chain(['configure terminal'], to_list(command), ['end']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -140,10 +140,10 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if output: if output:
command = self._get_command_with_output(command, output) command = self._get_command_with_output(command, output)
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
@configure @configure
def commit(self, comment=None, confirmed=False, at_time=None, synchronize=False): def commit(self, comment=None, confirmed=False, at_time=None, synchronize=False):

View file

@ -35,13 +35,13 @@ from ansible.plugins.cliconf import CliconfBase
class Cliconf(CliconfBase): class Cliconf(CliconfBase):
def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if not command: if not command:
raise ValueError('must provide value of command to execute') raise ValueError('must provide value of command to execute')
if output: if output:
raise ValueError("'output' value %s is not supported for get" % output) raise ValueError("'output' value %s is not supported for get" % output)
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_option_values(self): def get_option_values(self):
return { return {

View file

@ -105,8 +105,8 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -189,10 +189,10 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if output: if output:
command = self._get_command_with_output(command, output) command = self._get_command_with_output(command, output)
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def run_commands(self, commands=None, check_rc=True): def run_commands(self, commands=None, check_rc=True):
if commands is None: if commands is None:

View file

@ -70,8 +70,8 @@ class Cliconf(CliconfBase):
for cmd in chain(['configure terminal'], to_list(command), ['exit']): for cmd in chain(['configure terminal'], to_list(command), ['exit']):
self.send_command(cmd) self.send_command(cmd)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -71,8 +71,8 @@ class Cliconf(CliconfBase):
def edit_config(self, command): def edit_config(self, command):
return return
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -97,8 +97,8 @@ class Cliconf(CliconfBase):
self.send_command(command, prompt, answer, False, newline) self.send_command(command, prompt, answer, False, newline)
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_capabilities(self): def get_capabilities(self):
result = super(Cliconf, self).get_capabilities() result = super(Cliconf, self).get_capabilities()

View file

@ -154,8 +154,8 @@ class Cliconf(CliconfBase):
resp['response'] = results resp['response'] = results
return resp return resp
def get(self, command, prompt=None, answer=None, sendonly=False, check_all=False): def get(self, command, prompt=None, answer=None, sendonly=False, newline=True, check_all=False):
return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def get_device_info(self): def get_device_info(self):
device_info = {} device_info = {}

View file

@ -116,14 +116,13 @@ class Cliconf(CliconfBase):
resp['request'] = requests resp['request'] = requests
return resp return resp
def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, check_all=False): def get(self, command=None, prompt=None, answer=None, sendonly=False, output=None, newline=True, check_all=False):
if not command: if not command:
raise ValueError('must provide value of command to execute') raise ValueError('must provide value of command to execute')
if output: if output:
raise ValueError("'output' value %s is not supported for get" % output) raise ValueError("'output' value %s is not supported for get" % output)
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly, check_all=check_all) return self.send_command(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline, check_all=check_all)
def commit(self, comment=None): def commit(self, comment=None):
if comment: if comment: