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:
Ganesh Nalawade 2018-07-30 10:24:58 +05:30 committed by GitHub
commit 21dcaa4349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 241 additions and 117 deletions

View file

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