From bd989b3088f3a8273db3a4721a15fa4bee53f6c1 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Thu, 11 Jan 2018 16:47:48 -0500 Subject: [PATCH] Report invalid connection types for network modules (#34755) * Emit error on wrong connection type --- lib/ansible/plugins/action/eos.py | 2 ++ lib/ansible/plugins/action/ios.py | 2 ++ lib/ansible/plugins/action/iosxr.py | 4 +++- lib/ansible/plugins/action/junos.py | 4 +++- lib/ansible/plugins/action/nxos.py | 2 ++ lib/ansible/plugins/action/sros.py | 8 +++++++- lib/ansible/plugins/action/vyos.py | 2 ++ 7 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/ansible/plugins/action/eos.py b/lib/ansible/plugins/action/eos.py index aa980044d9..51bff914a8 100644 --- a/lib/ansible/plugins/action/eos.py +++ b/lib/ansible/plugins/action/eos.py @@ -101,6 +101,8 @@ class ActionModule(_ActionModule): provider['authorize'] = False self._task.args['provider'] = provider + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} if (self._play_context.connection == 'local' and transport == 'cli') or self._play_context.connection == 'network_cli': # make sure we are in the right cli context which should be diff --git a/lib/ansible/plugins/action/ios.py b/lib/ansible/plugins/action/ios.py index f8060b175e..13dbb47ac5 100644 --- a/lib/ansible/plugins/action/ios.py +++ b/lib/ansible/plugins/action/ios.py @@ -72,6 +72,8 @@ class ActionModule(_ActionModule): 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} task_vars['ansible_socket'] = socket_path + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} # make sure we are in the right cli context which should be # enable mode and not config module diff --git a/lib/ansible/plugins/action/iosxr.py b/lib/ansible/plugins/action/iosxr.py index 3fed63f5e7..8c37bad99a 100644 --- a/lib/ansible/plugins/action/iosxr.py +++ b/lib/ansible/plugins/action/iosxr.py @@ -73,10 +73,12 @@ class ActionModule(_ActionModule): 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} task_vars['ansible_socket'] = socket_path - else: + elif self._play_context.connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnecessary when using {0} and will be ignored'.format(self._play_context.connection)) + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} # make sure we are in the right cli context which should be # enable mode and not config module diff --git a/lib/ansible/plugins/action/junos.py b/lib/ansible/plugins/action/junos.py index 86ac4eae36..2fa40a5e67 100644 --- a/lib/ansible/plugins/action/junos.py +++ b/lib/ansible/plugins/action/junos.py @@ -75,10 +75,12 @@ class ActionModule(_ActionModule): 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} task_vars['ansible_socket'] = socket_path - else: + elif self._play_context.connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): 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 == '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 diff --git a/lib/ansible/plugins/action/nxos.py b/lib/ansible/plugins/action/nxos.py index 874cde018b..cc6d757c2f 100644 --- a/lib/ansible/plugins/action/nxos.py +++ b/lib/ansible/plugins/action/nxos.py @@ -101,6 +101,8 @@ class ActionModule(_ActionModule): provider['validate_certs'] = True self._task.args['provider'] = provider + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} if (self._play_context.connection == 'local' and transport == 'cli') or self._play_context.connection == 'network_cli': # make sure we are in the right cli context which should be diff --git a/lib/ansible/plugins/action/sros.py b/lib/ansible/plugins/action/sros.py index a2e3ecadc3..ae044d636f 100644 --- a/lib/ansible/plugins/action/sros.py +++ b/lib/ansible/plugins/action/sros.py @@ -38,7 +38,11 @@ class ActionModule(_ActionModule): def run(self, tmp=None, task_vars=None): - if self._play_context.connection == 'local': + if self._play_context.connection == 'network_cli': + provider = self._task.args.get('provider', {}) + if any(provider.values()): + display.warning('provider is unnecessary when using network_cli and will be ignored') + elif self._play_context.connection == 'local': provider = load_provider(sros_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) @@ -62,6 +66,8 @@ class ActionModule(_ActionModule): 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} task_vars['ansible_socket'] = socket_path + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} result = super(ActionModule, self).run(tmp, task_vars) return result diff --git a/lib/ansible/plugins/action/vyos.py b/lib/ansible/plugins/action/vyos.py index 74414b8eb5..965cd75cb0 100644 --- a/lib/ansible/plugins/action/vyos.py +++ b/lib/ansible/plugins/action/vyos.py @@ -68,6 +68,8 @@ class ActionModule(_ActionModule): 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} task_vars['ansible_socket'] = socket_path + else: + return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} # make sure we are in the right cli context which should be # enable mode and not config module