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

(cherry picked from commit cc8e90395a)
This commit is contained in:
Ganesh Nalawade 2018-12-19 14:46:44 +05:30 committed by Toshio Kuratomi
parent 82661d5cf4
commit 60a867441e
3 changed files with 20 additions and 4 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Fix mandatory statement error for junos modules (https://github.com/ansible/ansible/pull/50138)

View file

@ -23,6 +23,7 @@ import json
import re
from itertools import chain
from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils._text import to_text
from ansible.module_utils.network.common.utils import to_list
from ansible.plugins.cliconf import CliconfBase
@ -67,7 +68,12 @@ class Cliconf(CliconfBase):
def edit_config(self, command):
for cmd in chain(['configure'], to_list(command)):
self.send_command(cmd)
try:
self.send_command(cmd)
except AnsibleConnectionFailure as exc:
if "error: commit failed" in exc.message:
self.discard_changes()
raise
def get(self, command, prompt=None, answer=None, sendonly=False):
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly)
@ -82,11 +88,18 @@ class Cliconf(CliconfBase):
if comment:
command += ' comment {0}'.format(comment)
command += ' and-quit'
return self.send_command(command)
try:
response = self.send_command(command)
except AnsibleConnectionFailure:
self.discard_changes()
raise
return response
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)
def get_capabilities(self):

View file

@ -41,7 +41,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"[\r\n]error:")
]
def on_open_shell(self):