mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Handle ConnectionError exception in network modules (#43353)
* Handle ConnectionError exception in network modules * Catch ConnectionError expection and fail module in case expection is raised. * Fix CI failure
This commit is contained in:
parent
a44adc1dc9
commit
21dcaa4349
13 changed files with 241 additions and 117 deletions
|
@ -264,7 +264,9 @@ backup_path:
|
|||
type: string
|
||||
sample: /playbooks/ansible/backup/eos_config.2016-07-16@22:28:34
|
||||
"""
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
||||
from ansible.module_utils.network.eos.eos import get_config, load_config, get_connection
|
||||
from ansible.module_utils.network.eos.eos import run_commands
|
||||
|
@ -382,8 +384,12 @@ def main():
|
|||
candidate = get_candidate(module)
|
||||
running = get_running_config(module, contents, flags=flags)
|
||||
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
try:
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
config_diff = response['config_diff']
|
||||
|
||||
if config_diff:
|
||||
|
|
|
@ -293,6 +293,8 @@ backup_path:
|
|||
"""
|
||||
import json
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.ios.ios import run_commands, get_config
|
||||
from ansible.module_utils.network.ios.ios import get_defaults_flag, get_connection
|
||||
from ansible.module_utils.network.ios.ios import ios_argument_spec
|
||||
|
@ -419,9 +421,12 @@ def main():
|
|||
|
||||
candidate = get_candidate_config(module)
|
||||
running = get_running_config(module, contents, flags=flags)
|
||||
try:
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
config_diff = response['config_diff']
|
||||
banner_diff = response['banner_diff']
|
||||
|
||||
|
|
|
@ -164,6 +164,7 @@ import shlex
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.netconf import exec_rpc
|
||||
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_configuration, get_connection, get_capabilities, tostring
|
||||
from ansible.module_utils.network.common.parsing import Conditional, FailedConditionalError
|
||||
|
@ -373,7 +374,10 @@ def main():
|
|||
if ('display json' not in cmd) and ('display xml' not in cmd):
|
||||
if display and display != 'text':
|
||||
cmd += ' | display {0}'.format(display)
|
||||
output.append(conn.get(command=cmd))
|
||||
try:
|
||||
output.append(conn.get(command=cmd))
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
lines = [out.split('\n') for out in output]
|
||||
result = {'changed': False, 'stdout': output, 'stdout_lines': lines}
|
||||
|
|
|
@ -77,6 +77,8 @@ commands:
|
|||
"""
|
||||
import re
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_connection
|
||||
from ansible.module_utils.network.junos.junos import commit_configuration, discard_changes
|
||||
|
@ -146,9 +148,12 @@ def map_params_to_obj(module):
|
|||
|
||||
def load_config(module, config, commit=False):
|
||||
conn = get_connection(module)
|
||||
try:
|
||||
conn.edit_config(to_list(config) + ['top'])
|
||||
diff = conn.compare_configuration()
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
conn.edit_config(to_list(config) + ['top'])
|
||||
diff = conn.compare_configuration()
|
||||
if diff:
|
||||
if commit:
|
||||
commit_configuration(module)
|
||||
|
@ -156,7 +161,7 @@ def load_config(module, config, commit=False):
|
|||
else:
|
||||
discard_changes(module)
|
||||
|
||||
return str(diff).strip()
|
||||
return to_text(diff, errors='surrogate_then_replace').strip()
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
@ -138,7 +138,9 @@ from functools import partial
|
|||
|
||||
from copy import deepcopy
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.utils import remove_default_spec
|
||||
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_connection, tostring
|
||||
from ansible.module_utils.network.junos.junos import commit_configuration, discard_changes
|
||||
|
@ -160,7 +162,11 @@ def handle_purge(module, want):
|
|||
login = SubElement(element, 'login')
|
||||
|
||||
conn = get_connection(module)
|
||||
reply = conn.execute_rpc(tostring(Element('get-configuration')), ignore_warning=False)
|
||||
try:
|
||||
reply = conn.execute_rpc(tostring(Element('get-configuration')), ignore_warning=False)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
users = reply.xpath('configuration/system/login/user/name')
|
||||
if users:
|
||||
for item in users:
|
||||
|
|
|
@ -276,8 +276,7 @@ backup_path:
|
|||
type: string
|
||||
sample: /playbooks/ansible/backup/nxos_config.2016-07-16@22:28:34
|
||||
"""
|
||||
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
||||
|
@ -436,8 +435,12 @@ def main():
|
|||
|
||||
result['changed'] = True
|
||||
else:
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
try:
|
||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||
diff_replace=replace)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
config_diff = response['config_diff']
|
||||
if config_diff:
|
||||
commands = config_diff.split('\n')
|
||||
|
|
|
@ -131,7 +131,9 @@ backup_path:
|
|||
"""
|
||||
import re
|
||||
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.vyos.vyos import load_config, get_config, run_commands
|
||||
from ansible.module_utils.network.vyos.vyos import vyos_argument_spec, get_connection
|
||||
|
||||
|
@ -208,7 +210,11 @@ def run(module, result):
|
|||
|
||||
# create loadable config that includes only the configuration updates
|
||||
connection = get_connection(module)
|
||||
response = connection.get_diff(candidate=candidate, running=config, diff_match=module.params['match'])
|
||||
try:
|
||||
response = connection.get_diff(candidate=candidate, running=config, diff_match=module.params['match'])
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
commands = response.get('config_diff')
|
||||
sanitize_config(commands, result)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue