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
|
@ -135,7 +135,11 @@ class Cli:
|
||||||
return self._device_configs[cmd]
|
return self._device_configs[cmd]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
conn = self._get_connection()
|
conn = self._get_connection()
|
||||||
|
try:
|
||||||
out = conn.get_config(filter=flags)
|
out = conn.get_config(filter=flags)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
self._device_configs[cmd] = cfg
|
self._device_configs[cmd] = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
@ -144,7 +148,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=commands, check_rc=check_rc)
|
try:
|
||||||
|
response = connection.run_commands(commands=commands, check_rc=check_rc)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
def load_config(self, commands, commit=False, replace=False):
|
def load_config(self, commands, commit=False, replace=False):
|
||||||
"""Loads the config commands onto the remote device
|
"""Loads the config commands onto the remote device
|
||||||
|
@ -164,8 +172,12 @@ class Cli:
|
||||||
|
|
||||||
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'):
|
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'):
|
||||||
conn = self._get_connection()
|
conn = self._get_connection()
|
||||||
return conn.get_diff(candidate=candidate, running=running, diff_match=diff_match, diff_ignore_lines=diff_ignore_lines, path=path,
|
try:
|
||||||
|
diff = conn.get_diff(candidate=candidate, running=running, diff_match=diff_match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||||
diff_replace=diff_replace)
|
diff_replace=diff_replace)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return diff
|
||||||
|
|
||||||
|
|
||||||
class Eapi:
|
class Eapi:
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
#
|
#
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.basic import env_fallback, return_values
|
from ansible.module_utils.basic import env_fallback, return_values
|
||||||
from ansible.module_utils.network.common.utils import to_list, ComplexList
|
from ansible.module_utils.network.common.utils import to_list, ComplexList
|
||||||
|
@ -81,8 +82,10 @@ def get_connection(module):
|
||||||
def get_capabilities(module):
|
def get_capabilities(module):
|
||||||
if hasattr(module, '_ios_capabilities'):
|
if hasattr(module, '_ios_capabilities'):
|
||||||
return module._ios_capabilities
|
return module._ios_capabilities
|
||||||
|
try:
|
||||||
capabilities = Connection(module._socket_path).get_capabilities()
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
module._ios_capabilities = json.loads(capabilities)
|
module._ios_capabilities = json.loads(capabilities)
|
||||||
return module._ios_capabilities
|
return module._ios_capabilities
|
||||||
|
|
||||||
|
@ -93,7 +96,10 @@ def check_args(module, warnings):
|
||||||
|
|
||||||
def get_defaults_flag(module):
|
def get_defaults_flag(module):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
try:
|
||||||
out = connection.get_defaults_flag()
|
out = connection.get_defaults_flag()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
return to_text(out, errors='surrogate_then_replace').strip()
|
return to_text(out, errors='surrogate_then_replace').strip()
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,7 +110,10 @@ def get_config(module, flags=None):
|
||||||
return _DEVICE_CONFIGS[flag_str]
|
return _DEVICE_CONFIGS[flag_str]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
try:
|
||||||
out = connection.get_config(filter=flags)
|
out = connection.get_config(filter=flags)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
_DEVICE_CONFIGS[flag_str] = cfg
|
_DEVICE_CONFIGS[flag_str] = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
|
|
@ -30,12 +30,11 @@ import json
|
||||||
import re
|
import re
|
||||||
from difflib import Differ
|
from difflib import Differ
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from ansible.module_utils._text import to_text, to_bytes
|
from ansible.module_utils._text import to_text, to_bytes
|
||||||
from ansible.module_utils.basic import env_fallback
|
from ansible.module_utils.basic import env_fallback
|
||||||
from ansible.module_utils.network.common.utils import to_list
|
from ansible.module_utils.network.common.utils import to_list
|
||||||
from ansible.module_utils.connection import Connection
|
from ansible.module_utils.connection import Connection, ConnectionError
|
||||||
from ansible.module_utils.network.common.netconf import NetconfConnection
|
from ansible.module_utils.network.common.netconf import NetconfConnection
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -126,8 +125,10 @@ def get_connection(module):
|
||||||
def get_device_capabilities(module):
|
def get_device_capabilities(module):
|
||||||
if hasattr(module, 'capabilities'):
|
if hasattr(module, 'capabilities'):
|
||||||
return module.capabilities
|
return module.capabilities
|
||||||
|
try:
|
||||||
capabilities = Connection(module._socket_path).get_capabilities()
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
module.capabilities = json.loads(capabilities)
|
module.capabilities = json.loads(capabilities)
|
||||||
|
|
||||||
return module.capabilities
|
return module.capabilities
|
||||||
|
@ -317,7 +318,11 @@ def get_config_diff(module, running=None, candidate=None):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
|
||||||
if is_cliconf(module):
|
if is_cliconf(module):
|
||||||
return conn.get('show commit changes diff')
|
try:
|
||||||
|
response = conn.get('show commit changes diff')
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
elif is_netconf(module):
|
elif is_netconf(module):
|
||||||
if running and candidate:
|
if running and candidate:
|
||||||
running_data = running.split("\n", 1)[1].rsplit("\n", 1)[0]
|
running_data = running.split("\n", 1)[1].rsplit("\n", 1)[0]
|
||||||
|
@ -332,14 +337,17 @@ def get_config_diff(module, running=None, candidate=None):
|
||||||
|
|
||||||
def discard_config(module):
|
def discard_config(module):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
conn.discard_changes()
|
conn.discard_changes()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
|
|
||||||
def commit_config(module, comment=None, confirmed=False, confirm_timeout=None,
|
def commit_config(module, comment=None, confirmed=False, confirm_timeout=None,
|
||||||
persist=False, check=False, label=None):
|
persist=False, check=False, label=None):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
reply = None
|
reply = None
|
||||||
|
try:
|
||||||
if check:
|
if check:
|
||||||
reply = conn.validate()
|
reply = conn.validate()
|
||||||
else:
|
else:
|
||||||
|
@ -347,6 +355,8 @@ def commit_config(module, comment=None, confirmed=False, confirm_timeout=None,
|
||||||
reply = conn.commit(confirmed=confirmed, timeout=confirm_timeout, persist=persist)
|
reply = conn.commit(confirmed=confirmed, timeout=confirm_timeout, persist=persist)
|
||||||
elif is_cliconf(module):
|
elif is_cliconf(module):
|
||||||
reply = conn.commit(comment=comment, label=label)
|
reply = conn.commit(comment=comment, label=label)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
@ -355,7 +365,10 @@ def get_oper(module, filter=None):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
|
||||||
if filter is not None:
|
if filter is not None:
|
||||||
|
try:
|
||||||
response = conn.get(filter)
|
response = conn.get(filter)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -366,12 +379,14 @@ def get_config(module, config_filter=None, source='running'):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
|
||||||
# Note: Does not cache config in favour of latest config on every get operation.
|
# Note: Does not cache config in favour of latest config on every get operation.
|
||||||
|
try:
|
||||||
out = conn.get_config(source=source, filter=config_filter)
|
out = conn.get_config(source=source, filter=config_filter)
|
||||||
if is_netconf(module):
|
if is_netconf(module):
|
||||||
out = to_xml(conn.get_config(source=source, filter=config_filter))
|
out = to_xml(conn.get_config(source=source, filter=config_filter))
|
||||||
|
|
||||||
cfg = out.strip()
|
cfg = out.strip()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
|
|
||||||
|
@ -408,7 +423,7 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
else:
|
else:
|
||||||
discard_config(module)
|
discard_config(module)
|
||||||
except ConnectionError as exc:
|
except ConnectionError as exc:
|
||||||
module.fail_json(msg=to_text(exc))
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
finally:
|
finally:
|
||||||
# conn.unlock(target = 'candidate')
|
# conn.unlock(target = 'candidate')
|
||||||
pass
|
pass
|
||||||
|
@ -418,6 +433,7 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
cmd_filter = deepcopy(command_filter)
|
cmd_filter = deepcopy(command_filter)
|
||||||
# If label is present check if label already exist before entering
|
# If label is present check if label already exist before entering
|
||||||
# config mode
|
# config mode
|
||||||
|
try:
|
||||||
if label:
|
if label:
|
||||||
old_label = check_existing_commit_labels(conn, label)
|
old_label = check_existing_commit_labels(conn, label)
|
||||||
if old_label:
|
if old_label:
|
||||||
|
@ -430,11 +446,7 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
if admin:
|
if admin:
|
||||||
cmd_filter.insert(0, 'admin')
|
cmd_filter.insert(0, 'admin')
|
||||||
|
|
||||||
try:
|
|
||||||
conn.edit_config(cmd_filter)
|
conn.edit_config(cmd_filter)
|
||||||
except ConnectionError as exc:
|
|
||||||
module.fail_json(msg=to_text(exc))
|
|
||||||
|
|
||||||
if module._diff:
|
if module._diff:
|
||||||
diff = get_config_diff(module)
|
diff = get_config_diff(module)
|
||||||
|
|
||||||
|
@ -452,6 +464,8 @@ def load_config(module, command_filter, commit=False, replace=False,
|
||||||
conn.edit_config('exit')
|
conn.edit_config('exit')
|
||||||
else:
|
else:
|
||||||
conn.discard_changes()
|
conn.discard_changes()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
@ -493,9 +507,15 @@ def run_command(module, commands):
|
||||||
|
|
||||||
def copy_file(module, src, dst, proto='scp'):
|
def copy_file(module, src, dst, proto='scp'):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
conn.copy_file(source=src, destination=dst, proto=proto)
|
conn.copy_file(source=src, destination=dst, proto=proto)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
|
|
||||||
def get_file(module, src, dst, proto='scp'):
|
def get_file(module, src, dst, proto='scp'):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
conn.get_file(source=src, destination=dst, proto=proto)
|
conn.get_file(source=src, destination=dst, proto=proto)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
|
@ -22,7 +22,7 @@ from contextlib import contextmanager
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from ansible.module_utils.basic import env_fallback, return_values
|
from ansible.module_utils.basic import env_fallback, return_values
|
||||||
from ansible.module_utils.connection import Connection
|
from ansible.module_utils.connection import Connection, ConnectionError
|
||||||
from ansible.module_utils.network.common.netconf import NetconfConnection
|
from ansible.module_utils.network.common.netconf import NetconfConnection
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
|
|
||||||
|
@ -93,7 +93,10 @@ def get_capabilities(module):
|
||||||
if hasattr(module, '_junos_capabilities'):
|
if hasattr(module, '_junos_capabilities'):
|
||||||
return module._junos_capabilities
|
return module._junos_capabilities
|
||||||
|
|
||||||
|
try:
|
||||||
capabilities = Connection(module._socket_path).get_capabilities()
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
module._junos_capabilities = json.loads(capabilities)
|
module._junos_capabilities = json.loads(capabilities)
|
||||||
return module._junos_capabilities
|
return module._junos_capabilities
|
||||||
|
|
||||||
|
@ -125,12 +128,15 @@ def load_configuration(module, candidate=None, action='merge', rollback=None, fo
|
||||||
module.fail_json(msg='format must be text when action is set')
|
module.fail_json(msg='format must be text when action is set')
|
||||||
|
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
if rollback is not None:
|
if rollback is not None:
|
||||||
_validate_rollback_id(module, rollback)
|
_validate_rollback_id(module, rollback)
|
||||||
obj = Element('load-configuration', {'rollback': str(rollback)})
|
obj = Element('load-configuration', {'rollback': str(rollback)})
|
||||||
conn.execute_rpc(tostring(obj))
|
conn.execute_rpc(tostring(obj))
|
||||||
else:
|
else:
|
||||||
return conn.load_configuration(config=candidate, action=action, format=format)
|
return conn.load_configuration(config=candidate, action=action, format=format)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
|
|
||||||
def get_configuration(module, compare=False, format='xml', rollback='0', filter=None):
|
def get_configuration(module, compare=False, format='xml', rollback='0', filter=None):
|
||||||
|
@ -138,6 +144,7 @@ def get_configuration(module, compare=False, format='xml', rollback='0', filter=
|
||||||
module.fail_json(msg='invalid config format specified')
|
module.fail_json(msg='invalid config format specified')
|
||||||
|
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
if compare:
|
if compare:
|
||||||
xattrs = {'format': format}
|
xattrs = {'format': format}
|
||||||
_validate_rollback_id(module, rollback)
|
_validate_rollback_id(module, rollback)
|
||||||
|
@ -146,18 +153,21 @@ def get_configuration(module, compare=False, format='xml', rollback='0', filter=
|
||||||
reply = conn.execute_rpc(tostring(Element('get-configuration', xattrs)))
|
reply = conn.execute_rpc(tostring(Element('get-configuration', xattrs)))
|
||||||
else:
|
else:
|
||||||
reply = conn.get_configuration(format=format, filter=filter)
|
reply = conn.get_configuration(format=format, filter=filter)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
def commit_configuration(module, confirm=False, check=False, comment=None, confirm_timeout=None, synchronize=False,
|
def commit_configuration(module, confirm=False, check=False, comment=None, confirm_timeout=None, synchronize=False,
|
||||||
at_time=None, exit=False):
|
at_time=None, exit=False):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
if check:
|
if check:
|
||||||
reply = conn.validate()
|
reply = conn.validate()
|
||||||
else:
|
else:
|
||||||
reply = conn.commit(confirmed=confirm, timeout=confirm_timeout, comment=comment, synchronize=synchronize, at_time=at_time)
|
reply = conn.commit(confirmed=confirm, timeout=confirm_timeout, comment=comment, synchronize=synchronize, at_time=at_time)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
|
@ -165,17 +175,29 @@ def command(module, cmd, format='text', rpc_only=False):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
if rpc_only:
|
if rpc_only:
|
||||||
cmd += ' | display xml rpc'
|
cmd += ' | display xml rpc'
|
||||||
return conn.command(command=cmd, format=format)
|
try:
|
||||||
|
response = conn.command(command=cmd, format=format)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def lock_configuration(x):
|
def lock_configuration(module):
|
||||||
conn = get_connection(x)
|
conn = get_connection(module)
|
||||||
return conn.lock()
|
try:
|
||||||
|
response = conn.lock()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def unlock_configuration(x):
|
def unlock_configuration(module):
|
||||||
conn = get_connection(x)
|
conn = get_connection(module)
|
||||||
return conn.unlock()
|
try:
|
||||||
|
response = conn.unlock()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
@ -189,7 +211,11 @@ def locked_config(module):
|
||||||
|
|
||||||
def discard_changes(module):
|
def discard_changes(module):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
return conn.discard_changes()
|
try:
|
||||||
|
response = conn.discard_changes()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
def get_diff(module, rollback='0'):
|
def get_diff(module, rollback='0'):
|
||||||
|
|
|
@ -139,7 +139,11 @@ class Cli:
|
||||||
return self._device_configs[cmd]
|
return self._device_configs[cmd]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
connection = self._get_connection()
|
connection = self._get_connection()
|
||||||
|
try:
|
||||||
out = connection.get_config(filter=flags)
|
out = connection.get_config(filter=flags)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
self._device_configs[cmd] = cfg
|
self._device_configs[cmd] = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
@ -188,8 +192,12 @@ class Cli:
|
||||||
|
|
||||||
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'):
|
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'):
|
||||||
conn = self._get_connection()
|
conn = self._get_connection()
|
||||||
return conn.get_diff(candidate=candidate, running=running, diff_match=diff_match, diff_ignore_lines=diff_ignore_lines, path=path,
|
try:
|
||||||
|
response = conn.get_diff(candidate=candidate, running=running, diff_match=diff_match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||||
diff_replace=diff_replace)
|
diff_replace=diff_replace)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
def get_capabilities(self):
|
def get_capabilities(self):
|
||||||
"""Returns platform info of the remove device
|
"""Returns platform info of the remove device
|
||||||
|
@ -198,7 +206,10 @@ class Cli:
|
||||||
return self._module._capabilities
|
return self._module._capabilities
|
||||||
|
|
||||||
connection = self._get_connection()
|
connection = self._get_connection()
|
||||||
|
try:
|
||||||
capabilities = connection.get_capabilities()
|
capabilities = connection.get_capabilities()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
self._module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
self._module._capabilities = json.loads(capabilities)
|
self._module._capabilities = json.loads(capabilities)
|
||||||
return self._module._capabilities
|
return self._module._capabilities
|
||||||
|
|
||||||
|
|
|
@ -81,7 +81,11 @@ def get_capabilities(module):
|
||||||
if hasattr(module, '_vyos_capabilities'):
|
if hasattr(module, '_vyos_capabilities'):
|
||||||
return module._vyos_capabilities
|
return module._vyos_capabilities
|
||||||
|
|
||||||
|
try:
|
||||||
capabilities = Connection(module._socket_path).get_capabilities()
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
module._vyos_capabilities = json.loads(capabilities)
|
module._vyos_capabilities = json.loads(capabilities)
|
||||||
return module._vyos_capabilities
|
return module._vyos_capabilities
|
||||||
|
|
||||||
|
@ -93,7 +97,10 @@ def get_config(module):
|
||||||
return _DEVICE_CONFIGS
|
return _DEVICE_CONFIGS
|
||||||
else:
|
else:
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
try:
|
||||||
out = connection.get_config()
|
out = connection.get_config()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
_DEVICE_CONFIGS = cfg
|
_DEVICE_CONFIGS = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
@ -101,15 +108,19 @@ def get_config(module):
|
||||||
|
|
||||||
def run_commands(module, commands, check_rc=True):
|
def run_commands(module, commands, check_rc=True):
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
return connection.run_commands(commands=commands, check_rc=check_rc)
|
try:
|
||||||
|
response = connection.run_commands(commands=commands, check_rc=check_rc)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = connection.edit_config(candidate=commands, commit=commit, comment=comment)
|
response = connection.edit_config(candidate=commands, commit=commit, comment=comment)
|
||||||
except ConnectionError as exc:
|
except ConnectionError as exc:
|
||||||
module.fail_json(msg=to_text(exc))
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
return resp.get('diff')
|
return response.get('diff')
|
||||||
|
|
|
@ -264,7 +264,9 @@ backup_path:
|
||||||
type: string
|
type: string
|
||||||
sample: /playbooks/ansible/backup/eos_config.2016-07-16@22:28:34
|
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.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.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 get_config, load_config, get_connection
|
||||||
from ansible.module_utils.network.eos.eos import run_commands
|
from ansible.module_utils.network.eos.eos import run_commands
|
||||||
|
@ -382,8 +384,12 @@ def main():
|
||||||
candidate = get_candidate(module)
|
candidate = get_candidate(module)
|
||||||
running = get_running_config(module, contents, flags=flags)
|
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,
|
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||||
diff_replace=replace)
|
diff_replace=replace)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
config_diff = response['config_diff']
|
config_diff = response['config_diff']
|
||||||
|
|
||||||
if config_diff:
|
if config_diff:
|
||||||
|
|
|
@ -293,6 +293,8 @@ backup_path:
|
||||||
"""
|
"""
|
||||||
import json
|
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 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 get_defaults_flag, get_connection
|
||||||
from ansible.module_utils.network.ios.ios import ios_argument_spec
|
from ansible.module_utils.network.ios.ios import ios_argument_spec
|
||||||
|
@ -419,9 +421,12 @@ def main():
|
||||||
|
|
||||||
candidate = get_candidate_config(module)
|
candidate = get_candidate_config(module)
|
||||||
running = get_running_config(module, contents, flags=flags)
|
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,
|
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||||
diff_replace=replace)
|
diff_replace=replace)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
config_diff = response['config_diff']
|
config_diff = response['config_diff']
|
||||||
banner_diff = response['banner_diff']
|
banner_diff = response['banner_diff']
|
||||||
|
|
||||||
|
|
|
@ -164,6 +164,7 @@ import shlex
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils._text import to_text
|
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.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.junos.junos import junos_argument_spec, get_configuration, get_connection, get_capabilities, tostring
|
||||||
from ansible.module_utils.network.common.parsing import Conditional, FailedConditionalError
|
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 json' not in cmd) and ('display xml' not in cmd):
|
||||||
if display and display != 'text':
|
if display and display != 'text':
|
||||||
cmd += ' | display {0}'.format(display)
|
cmd += ' | display {0}'.format(display)
|
||||||
|
try:
|
||||||
output.append(conn.get(command=cmd))
|
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]
|
lines = [out.split('\n') for out in output]
|
||||||
result = {'changed': False, 'stdout': output, 'stdout_lines': lines}
|
result = {'changed': False, 'stdout': output, 'stdout_lines': lines}
|
||||||
|
|
|
@ -77,6 +77,8 @@ commands:
|
||||||
"""
|
"""
|
||||||
import re
|
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.basic import AnsibleModule
|
||||||
from ansible.module_utils.network.junos.junos import junos_argument_spec, get_connection
|
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
|
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):
|
def load_config(module, config, commit=False):
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
conn.edit_config(to_list(config) + ['top'])
|
conn.edit_config(to_list(config) + ['top'])
|
||||||
diff = conn.compare_configuration()
|
diff = conn.compare_configuration()
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
if diff:
|
if diff:
|
||||||
if commit:
|
if commit:
|
||||||
commit_configuration(module)
|
commit_configuration(module)
|
||||||
|
@ -156,7 +161,7 @@ def load_config(module, config, commit=False):
|
||||||
else:
|
else:
|
||||||
discard_changes(module)
|
discard_changes(module)
|
||||||
|
|
||||||
return str(diff).strip()
|
return to_text(diff, errors='surrogate_then_replace').strip()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
|
@ -138,7 +138,9 @@ from functools import partial
|
||||||
|
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
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.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 junos_argument_spec, get_connection, tostring
|
||||||
from ansible.module_utils.network.junos.junos import commit_configuration, discard_changes
|
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')
|
login = SubElement(element, 'login')
|
||||||
|
|
||||||
conn = get_connection(module)
|
conn = get_connection(module)
|
||||||
|
try:
|
||||||
reply = conn.execute_rpc(tostring(Element('get-configuration')), ignore_warning=False)
|
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')
|
users = reply.xpath('configuration/system/login/user/name')
|
||||||
if users:
|
if users:
|
||||||
for item in users:
|
for item in users:
|
||||||
|
|
|
@ -276,8 +276,7 @@ backup_path:
|
||||||
type: string
|
type: string
|
||||||
sample: /playbooks/ansible/backup/nxos_config.2016-07-16@22:28:34
|
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.basic import AnsibleModule
|
||||||
from ansible.module_utils.connection import ConnectionError
|
from ansible.module_utils.connection import ConnectionError
|
||||||
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
||||||
|
@ -436,8 +435,12 @@ def main():
|
||||||
|
|
||||||
result['changed'] = True
|
result['changed'] = True
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
|
||||||
diff_replace=replace)
|
diff_replace=replace)
|
||||||
|
except ConnectionError as exc:
|
||||||
|
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||||
|
|
||||||
config_diff = response['config_diff']
|
config_diff = response['config_diff']
|
||||||
if config_diff:
|
if config_diff:
|
||||||
commands = config_diff.split('\n')
|
commands = config_diff.split('\n')
|
||||||
|
|
|
@ -131,7 +131,9 @@ backup_path:
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
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 load_config, get_config, run_commands
|
||||||
from ansible.module_utils.network.vyos.vyos import vyos_argument_spec, get_connection
|
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
|
# create loadable config that includes only the configuration updates
|
||||||
connection = get_connection(module)
|
connection = get_connection(module)
|
||||||
|
try:
|
||||||
response = connection.get_diff(candidate=candidate, running=config, diff_match=module.params['match'])
|
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')
|
commands = response.get('config_diff')
|
||||||
sanitize_config(commands, result)
|
sanitize_config(commands, result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue