mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 04:11:25 -07:00
Check for ConnectionError on change of config or commands (#41504)
This commit is contained in:
parent
5814b90835
commit
50e776877d
5 changed files with 45 additions and 14 deletions
|
@ -83,7 +83,10 @@ def run_commands(module, commands, check_rc=True):
|
||||||
prompt = None
|
prompt = None
|
||||||
answer = None
|
answer = None
|
||||||
|
|
||||||
out = connection.get(command, prompt, answer)
|
try:
|
||||||
|
out = connection.get(command, prompt, answer)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
out = to_text(out, errors='surrogate_or_strict')
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
@ -99,7 +102,10 @@ def run_commands(module, commands, check_rc=True):
|
||||||
def load_config(module, commands, commit=False, comment=None):
|
def load_config(module, commands, commit=False, comment=None):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
|
||||||
out = connection.edit_config(commands)
|
try:
|
||||||
|
out = connection.edit_config(commands)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
diff = None
|
diff = None
|
||||||
if module._diff:
|
if module._diff:
|
||||||
|
@ -113,7 +119,7 @@ def load_config(module, commands, commit=False, comment=None):
|
||||||
if commit:
|
if commit:
|
||||||
try:
|
try:
|
||||||
out = connection.commit(comment)
|
out = connection.commit(comment)
|
||||||
except:
|
except ConnectionError:
|
||||||
connection.discard_changes()
|
connection.discard_changes()
|
||||||
module.fail_json(msg='commit failed: %s' % out)
|
module.fail_json(msg='commit failed: %s' % out)
|
||||||
|
|
||||||
|
|
|
@ -146,11 +146,11 @@ def run_commands(module, commands, check_rc=True):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
out = connection.get(command, prompt, answer)
|
out = connection.get(command, prompt, answer)
|
||||||
except ConnectionError as e:
|
except ConnectionError as exc:
|
||||||
if check_rc:
|
if check_rc:
|
||||||
raise
|
module.fail_json(msg=to_text(exc))
|
||||||
else:
|
else:
|
||||||
out = e
|
out = exc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
out = to_text(out, errors='surrogate_or_strict')
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
@ -165,4 +165,7 @@ def run_commands(module, commands, check_rc=True):
|
||||||
def load_config(module, commands):
|
def load_config(module, commands):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
|
||||||
return connection.edit_config(commands)
|
try:
|
||||||
|
return connection.edit_config(commands)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
|
@ -401,6 +401,8 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
commit_config(module)
|
commit_config(module)
|
||||||
else:
|
else:
|
||||||
discard_config(module)
|
discard_config(module)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
finally:
|
finally:
|
||||||
# conn.unlock(target = 'candidate')
|
# conn.unlock(target = 'candidate')
|
||||||
pass
|
pass
|
||||||
|
@ -411,7 +413,11 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
cmd_filter.insert(0, 'configure terminal')
|
cmd_filter.insert(0, 'configure terminal')
|
||||||
if admin:
|
if admin:
|
||||||
cmd_filter.insert(0, 'admin')
|
cmd_filter.insert(0, 'admin')
|
||||||
conn.edit_config(cmd_filter)
|
|
||||||
|
try:
|
||||||
|
conn.edit_config(cmd_filter)
|
||||||
|
except ConnectionError:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
if module._diff:
|
if module._diff:
|
||||||
diff = get_config_diff(module)
|
diff = get_config_diff(module)
|
||||||
|
@ -454,12 +460,18 @@ def run_command(module, commands):
|
||||||
sendonly = False
|
sendonly = False
|
||||||
newline = True
|
newline = True
|
||||||
|
|
||||||
out = conn.get(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline)
|
try:
|
||||||
|
out = conn.get(command=command, prompt=prompt, answer=answer, sendonly=sendonly, newline=newline)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
responses.append(to_text(out, errors='surrogate_or_strict'))
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
module.fail_json(msg=u'failed to decode output from {0}:{1}'.format(cmd, to_text(out)))
|
module.fail_json(msg=u'Failed to decode output from {0}: {1}'.format(cmd, to_text(out)))
|
||||||
|
|
||||||
|
responses.append(out)
|
||||||
|
|
||||||
return responses
|
return responses
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -147,7 +147,11 @@ class Cli:
|
||||||
"""Run list of commands on remote device and return results
|
"""Run list of commands on remote device and return results
|
||||||
"""
|
"""
|
||||||
connection = self._get_connection()
|
connection = self._get_connection()
|
||||||
return connection.run_commands(commands, check_rc)
|
|
||||||
|
try:
|
||||||
|
return connection.run_commands(commands, check_rc)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
def load_config(self, config, return_error=False, opts=None):
|
def load_config(self, config, return_error=False, opts=None):
|
||||||
"""Sends configuration commands to the remote device
|
"""Sends configuration commands to the remote device
|
||||||
|
|
|
@ -114,7 +114,10 @@ def run_commands(module, commands, check_rc=True):
|
||||||
prompt = None
|
prompt = None
|
||||||
answer = None
|
answer = None
|
||||||
|
|
||||||
out = connection.get(command, prompt, answer)
|
try:
|
||||||
|
out = connection.get(command, prompt, answer)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
out = to_text(out, errors='surrogate_or_strict')
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
@ -129,7 +132,10 @@ def run_commands(module, commands, check_rc=True):
|
||||||
def load_config(module, commands, commit=False, comment=None):
|
def load_config(module, commands, commit=False, comment=None):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
|
||||||
out = connection.edit_config(commands)
|
try:
|
||||||
|
out = connection.edit_config(commands)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc))
|
||||||
|
|
||||||
diff = None
|
diff = None
|
||||||
if module._diff:
|
if module._diff:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue