From cc8e90395a9892eb58efac02ae1ac9737853d694 Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Wed, 19 Dec 2018 14:46:44 +0530 Subject: [PATCH] Fix mandatory statement error for junos modules (#50074) * Fix mandatory statement error for junos modules Fixes #40267 * Add error regex in junos terminal plugin to error out in case of commit fails * If commit fails add logic to discard changes before existing else next task will result in error * Add integration test * Minor update --- lib/ansible/plugins/cliconf/junos.py | 19 ++++++++++++++++--- lib/ansible/plugins/terminal/junos.py | 3 ++- .../tests/cli_config/cli_basic.yaml | 11 +++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/ansible/plugins/cliconf/junos.py b/lib/ansible/plugins/cliconf/junos.py index ad04c65e91c..dadbaa600cd 100644 --- a/lib/ansible/plugins/cliconf/junos.py +++ b/lib/ansible/plugins/cliconf/junos.py @@ -25,6 +25,7 @@ import re from itertools import chain from functools import wraps +from ansible.errors import AnsibleConnectionFailure from ansible.module_utils._text import to_text from ansible.module_utils.common._collections_compat import Mapping from ansible.module_utils.network.common.utils import to_list @@ -103,7 +104,12 @@ class Cliconf(CliconfBase): if not isinstance(line, Mapping): line = {'command': line} cmd = line['command'] - results.append(self.send_command(**line)) + try: + results.append(self.send_command(**line)) + except AnsibleConnectionFailure as exc: + if "error: commit failed" in exc.message: + self.discard_changes() + raise requests.append(cmd) diff = self.compare_configuration() @@ -149,12 +155,19 @@ class Cliconf(CliconfBase): command += ' peers-synchronize' command += ' and-quit' - return self.send_command(command) + + try: + response = self.send_command(command) + except AnsibleConnectionFailure: + self.discard_changes() + raise + + return response @configure def discard_changes(self): command = 'rollback 0' - for cmd in chain(to_list(command), 'exit'): + for cmd in chain(to_list(command), ['exit']): self.send_command(cmd) @configure diff --git a/lib/ansible/plugins/terminal/junos.py b/lib/ansible/plugins/terminal/junos.py index 9f4d79276df..f614e298e97 100644 --- a/lib/ansible/plugins/terminal/junos.py +++ b/lib/ansible/plugins/terminal/junos.py @@ -37,7 +37,8 @@ class TerminalModule(TerminalBase): terminal_stderr_re = [ re.compile(br"unknown command"), - re.compile(br"syntax error,") + re.compile(br"syntax error"), + re.compile(br"error: commit failed") ] def on_open_shell(self): diff --git a/test/integration/targets/junos_config/tests/cli_config/cli_basic.yaml b/test/integration/targets/junos_config/tests/cli_config/cli_basic.yaml index e7a413b5a25..4782c87a611 100644 --- a/test/integration/targets/junos_config/tests/cli_config/cli_basic.yaml +++ b/test/integration/targets/junos_config/tests/cli_config/cli_basic.yaml @@ -43,6 +43,17 @@ - "result.changed == true" - "'ge-0/0/2' in result.diff.prepared" +- name: remove root-authethication (test error scenario) + cli_config: + config: "delete system root-authentication" + ignore_errors: True + register: result + +- assert: + that: + - "result.failed == true" + - "'Missing mandatory statement' in result.msg" + - name: teardown cli_config: *rm1