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
This commit is contained in:
Ganesh Nalawade 2018-12-19 14:46:44 +05:30 committed by GitHub
parent 97de7c133e
commit cc8e90395a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 4 deletions

View file

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

View file

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

View file

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