Network load_config: Capture configuration output and display as warnings (#27851)

* Capture configuration output and display as warnings

* Don't break on nxapi

nxapi errors very loudly instead, so no need to muck about with warnings
This commit is contained in:
Nathaniel Case 2017-08-09 09:43:04 -04:00 committed by GitHub
parent 2af759e9f9
commit c1bf74283e
4 changed files with 9 additions and 3 deletions

View file

@ -165,16 +165,21 @@ class Cli:
def load_config(self, config): def load_config(self, config):
"""Sends configuration commands to the remote device """Sends configuration commands to the remote device
""" """
rc, out, err = self.exec_command('configure') rc, out, err = self.exec_command('configure')
if rc != 0: if rc != 0:
self._module.fail_json(msg='unable to enter configuration mode', output=to_text(err, errors='surrogate_then_replace')) self._module.fail_json(msg='unable to enter configuration mode', output=to_text(err, errors='surrogate_then_replace'))
msgs = []
for cmd in config: for cmd in config:
rc, out, err = self.exec_command(cmd) rc, out, err = self.exec_command(cmd)
if rc != 0: if rc != 0:
self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace')) self._module.fail_json(msg=to_text(err, errors='surrogate_then_replace'))
elif out:
msgs.append(out)
self.exec_command('end') self.exec_command('end')
return msgs
class Nxapi: class Nxapi:
@ -343,6 +348,7 @@ class Nxapi:
""" """
commands = to_list(commands) commands = to_list(commands)
self.send_request(commands, output='config') self.send_request(commands, output='config')
return []
def is_json(cmd): def is_json(cmd):

View file

@ -748,7 +748,7 @@ def main():
if candidate: if candidate:
candidate = candidate.items_text() candidate = candidate.items_text()
load_config(module, candidate) warnings.extend(load_config(module, candidate))
result['changed'] = True result['changed'] = True
result['commands'] = candidate result['commands'] = candidate
else: else:

View file

@ -204,7 +204,7 @@ class Connection(Rpc, _Connection):
"""Removes elements from the response before returning to the caller""" """Removes elements from the response before returning to the caller"""
cleaned = [] cleaned = []
for line in resp.splitlines(): for line in resp.splitlines():
if (command and line.startswith(command.strip())) or self._matched_prompt.strip() in line: if (command and line.strip() == command.strip()) or self._matched_prompt.strip() in line:
continue continue
cleaned.append(line) cleaned.append(line)
return b'\n'.join(cleaned).strip() return b'\n'.join(cleaned).strip()

View file

@ -43,7 +43,7 @@ class TestNxosBgpNeighborAfModule(TestNxosModule):
def load_fixtures(self, commands=None, device=''): def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg') self.get_config.return_value = load_fixture('', 'nxos_bgp_config.cfg')
self.load_config.return_value = None self.load_config.return_value = []
def test_nxos_bgp_neighbor_af(self): def test_nxos_bgp_neighbor_af(self):
set_module_args(dict(asn=65535, neighbor='3.3.3.3', afi='ipv4', set_module_args(dict(asn=65535, neighbor='3.3.3.3', afi='ipv4',