Add error handling for junos in case wrong connection type (#38023)

* Add error handling for junos in case wrong connection type

Fixes #37990

If a junos module doesn't support given connection/transport type
return appropriate error message.

* Fix CI issues

* Fix review comment
pull/38305/head
Ganesh Nalawade 7 years ago committed by GitHub
parent d653d52dd8
commit 3a4fc4af08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,6 +37,8 @@ except ImportError:
from ansible.utils.display import Display from ansible.utils.display import Display
display = Display() display = Display()
CLI_SUPPORTED_MODULES = ['junos_netconf', 'junos_command']
class ActionModule(_ActionModule): class ActionModule(_ActionModule):
@ -54,6 +56,13 @@ class ActionModule(_ActionModule):
pc = copy.deepcopy(self._play_context) pc = copy.deepcopy(self._play_context)
pc.network_os = 'junos' pc.network_os = 'junos'
pc.remote_addr = provider['host'] or self._play_context.remote_addr pc.remote_addr = provider['host'] or self._play_context.remote_addr
if (provider['transport'] == 'cli' and self._task.action not in CLI_SUPPORTED_MODULES) or \
(provider['transport'] == 'netconf' and self._task.action == 'junos_netconf'):
return {'failed': True, 'msg': "Transport type '%s' is not valid for '%s' module. "
"Please see http://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html"
% (provider['transport'], self._task.action)}
if self._task.action == 'junos_netconf' or (provider['transport'] == 'cli' and self._task.action == 'junos_command'): if self._task.action == 'junos_netconf' or (provider['transport'] == 'cli' and self._task.action == 'junos_command'):
pc.connection = 'network_cli' pc.connection = 'network_cli'
pc.port = int(provider['port'] or self._play_context.port or 22) pc.port = int(provider['port'] or self._play_context.port or 22)
@ -81,8 +90,11 @@ class ActionModule(_ActionModule):
provider = self._task.args.get('provider', {}) provider = self._task.args.get('provider', {})
if any(provider.values()): if any(provider.values()):
display.warning('provider is unnecessary when using connection=%s and will be ignored' % self._play_context.connection) display.warning('provider is unnecessary when using connection=%s and will be ignored' % self._play_context.connection)
else:
return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} if self._play_context.connection == 'network_cli' and self._task.action not in CLI_SUPPORTED_MODULES:
return {'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module. "
"Please see http://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html"
% (self._play_context.connection, self._task.action)}
if (self._play_context.connection == 'local' and pc.connection == 'network_cli') or self._play_context.connection == 'network_cli': if (self._play_context.connection == 'local' and pc.connection == 'network_cli') or self._play_context.connection == 'network_cli':
# make sure we are in the right cli context which should be # make sure we are in the right cli context which should be

Loading…
Cancel
Save