Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow network_cli type for platforms using persistent connection #120

Merged
merged 7 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelogs/fragments/119_fix_connection_check_issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bugfixes:
- action pugins - add check for network_cli connection type (https://github.com/ansible-collections/community.network/issues/119, https://github.com/ansible-collections/community.network/pull/120).

deprecated_features:
- Deprecate connection=local support for network platforms using persistent framework (https://github.com/ansible-collections/community.network/pull/120).
69 changes: 40 additions & 29 deletions plugins/action/aireos.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.aireos.aireos import aireos_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand All @@ -38,42 +39,52 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action.split('.')[-1]
self._config_module = True if module_name == 'aireos_config' else False

if self._play_context.connection != 'local':
persistent_connection = self._play_context.connection.split('.')[-1]

if persistent_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')
del self._task.args['provider']
elif self._play_context.connection == 'local':
provider = load_provider(aireos_provider_spec, self._task.args)
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'aireos'
pc.remote_addr = provider['host'] or self._play_context.remote_addr
pc.port = int(provider['port'] or self._play_context.port or 22)
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)

connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin,
task_uuid=self._task._uuid)

display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
connection.set_options(direct={'persistent_command_timeout': command_timeout})

socket_path = connection.run()
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not socket_path:
return {'failed': True,
'msg': 'unable to open shell. Please see: ' +
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this method has any effect (it stores its result in global variables which aren't read). Better use display.deprecated().


else:
return dict(
failed=True,
msg='invalid connection specified, expected connection=local, '
msg='invalid connection specified, expected connection is either network_cli or ansible.netcommon.network_cli'
'got %s' % self._play_context.connection
)

provider = load_provider(aireos_provider_spec, self._task.args)

pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'aireos'
pc.remote_addr = provider['host'] or self._play_context.remote_addr
pc.port = int(provider['port'] or self._play_context.port or 22)
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)

display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin, task_uuid=self._task._uuid)
connection.set_options(direct={'persistent_command_timeout': command_timeout})

socket_path = connection.run()
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not socket_path:
return {'failed': True,
'msg': 'unable to open shell. Please see: ' +
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path

if self._play_context.become_method == 'enable':
if self._play_context.become_method and self._play_context.become_method.split('.')[-1] == 'enable':
self._play_context.become = False
self._play_context.become_method = None

result = super(ActionModule, self).run(task_vars=task_vars)

return result
66 changes: 38 additions & 28 deletions plugins/action/aruba.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.aruba.aruba import aruba_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand All @@ -38,40 +39,49 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action.split('.')[-1]
self._config_module = True if module_name == 'aruba_config' else False
persistent_connection = self._play_context.connection.split('.')[-1]

if self._play_context.connection != 'local':
return dict(
failed=True,
msg='invalid connection specified, expected connection=local, '
'got %s' % self._play_context.connection
)

provider = load_provider(aruba_provider_spec, self._task.args)
if persistent_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')
del self._task.args['provider']
elif self._play_context.connection == 'local':
provider = load_provider(aruba_provider_spec, self._task.args)
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'aruba'
pc.remote_addr = provider['host'] or self._play_context.remote_addr
pc.port = int(provider['port'] or self._play_context.port or 22)
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)

pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
pc.network_os = 'aruba'
pc.remote_addr = provider['host'] or self._play_context.remote_addr
pc.port = int(provider['port'] or self._play_context.port or 22)
pc.remote_user = provider['username'] or self._play_context.connection_user
pc.password = provider['password'] or self._play_context.password
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin, task_uuid=self._task._uuid)

display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin, task_uuid=self._task._uuid)
connection.set_options(direct={'persistent_command_timeout': command_timeout})
display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
connection.set_options(direct={'persistent_command_timeout': command_timeout})

socket_path = connection.run()
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not socket_path:
return {'failed': True,
'msg': 'unable to open shell. Please see: ' +
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
socket_path = connection.run()
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
if not socket_path:
return {'failed': True,
'msg': 'unable to open shell. Please see: ' +
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')
else:
return dict(
failed=True,
msg='invalid connection specified, expected connection is either network_cli or ansible.netcommon.network_cli'
'got %s' % self._play_context.connection
)

if self._play_context.become_method == 'enable':
if self._play_context.become_method and self._play_context.become_method.split('.')[-1] == 'enable':
self._play_context.become = False
self._play_context.become_method = None

Expand Down
6 changes: 6 additions & 0 deletions plugins/action/ce.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.cloudengine.ce import ce_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand Down Expand Up @@ -59,6 +60,7 @@ def run(self, tmp=None, task_vars=None):
)
if module_name in ['ce_netconf'] or module_name not in CLI_SUPPORTED_MODULES:
pc.connection = 'netconf'

display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin, task_uuid=self._task._uuid)
connection.set_options(direct={'persistent_command_timeout': command_timeout})
Expand All @@ -74,6 +76,10 @@ def run(self, tmp=None, task_vars=None):
# make sure a transport value is set in args
self._task.args['transport'] = transport
self._task.args['provider'] = provider
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')

elif persistent_connection in ('netconf', 'network_cli'):
provider = self._task.args.get('provider', {})
if any(provider.values()):
Expand Down
12 changes: 11 additions & 1 deletion plugins/action/cnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.cnos.cnos import cnos_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand All @@ -36,8 +37,14 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action.split('.')[-1]
self._config_module = True if module_name == 'cnos_config' else False
persistent_connection = self._play_context.connection.split('.')[-1]

if self._play_context.connection == 'local':
if persistent_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')
del self._task.args['provider']
elif self._play_context.connection == 'local':
provider = load_provider(cnos_provider_spec, self._task.args)
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
Expand All @@ -64,6 +71,9 @@ def run(self, tmp=None, task_vars=None):
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')

result = super(ActionModule, self).run(task_vars=task_vars)
return result
12 changes: 11 additions & 1 deletion plugins/action/enos.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.enos.enos import enos_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand All @@ -36,8 +37,14 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action.split('.')[-1]
self._config_module = True if module_name == 'enos_config' else False
persistent_connection = self._play_context.connection.split('.')[-1]

if self._play_context.connection == 'local':
if persistent_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')
del self._task.args['provider']
elif self._play_context.connection == 'local':
provider = load_provider(enos_provider_spec, self._task.args)
pc = copy.deepcopy(self._play_context)
pc.connection = 'network_cli'
Expand All @@ -64,6 +71,9 @@ def run(self, tmp=None, task_vars=None):
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')

result = super(ActionModule, self).run(task_vars=task_vars)
return result
4 changes: 4 additions & 0 deletions plugins/action/ironware.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import copy

from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible_collections.community.network.plugins.module_utils.network.ironware.ironware import ironware_provider_spec
from ansible.utils.display import Display
Expand Down Expand Up @@ -73,6 +74,9 @@ def run(self, tmp=None, task_vars=None):
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')
else:
return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection}

Expand Down
6 changes: 5 additions & 1 deletion plugins/action/sros.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ansible import constants as C
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
from ansible_collections.community.network.plugins.module_utils.network.sros.sros import sros_provider_spec
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.warnings import deprecate
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
from ansible.utils.display import Display

Expand All @@ -38,8 +39,8 @@ def run(self, tmp=None, task_vars=None):

module_name = self._task.action.split('.')[-1]
persistent_connection = self._play_context.connection.split('.')[-1]

self._config_module = True if module_name == 'sros_config' else False

if persistent_connection == 'network_cli':
provider = self._task.args.get('provider', {})
if any(provider.values()):
Expand Down Expand Up @@ -70,6 +71,9 @@ def run(self, tmp=None, task_vars=None):
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}

task_vars['ansible_socket'] = socket_path
msg = "connection local support for this module is deprecated use either" \
" 'network_cli' or 'ansible.netcommon.network_cli' connection"
deprecate(msg, version='4.0.0', collection_name='community.network')
else:
return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection}

Expand Down
3 changes: 2 additions & 1 deletion plugins/module_utils/network/aireos/aireos.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
'timeout': dict(type='int'),
}
aireos_argument_spec = {
'provider': dict(type='dict', options=aireos_provider_spec)
'provider': dict(type='dict', options=aireos_provider_spec, removed_in_version='4.0.0',
removed_from_collection='community.network')
}

aireos_top_spec = {
Expand Down
3 changes: 2 additions & 1 deletion plugins/module_utils/network/aruba/aruba.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
'timeout': dict(type='int'),
}
aruba_argument_spec = {
'provider': dict(type='dict', options=aruba_provider_spec)
'provider': dict(type='dict', options=aruba_provider_spec, removed_in_version='4.0.0',
removed_from_collection='community.network')
}

aruba_top_spec = {
Expand Down
3 changes: 2 additions & 1 deletion plugins/module_utils/network/cloudengine/ce.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
'transport': dict(default='cli', choices=['cli', 'netconf']),
}
ce_argument_spec = {
'provider': dict(type='dict', options=ce_provider_spec),
'provider': dict(type='dict', options=ce_provider_spec, removed_in_version='4.0.0',
removed_from_collection='community.network'),
}


Expand Down
3 changes: 2 additions & 1 deletion plugins/module_utils/network/cnos/cnos.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
}

cnos_argument_spec = {
'provider': dict(type='dict', options=cnos_provider_spec),
'provider': dict(type='dict', options=cnos_provider_spec, removed_in_version='4.0.0',
removed_from_collection='community.network'),
}

command_spec = {
Expand Down
Loading