1#
2# (c) 2016 Red Hat Inc.
3#
4# This file is part of Ansible
5#
6# Ansible is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Ansible is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18#
19from __future__ import (absolute_import, division, print_function)
20__metaclass__ = type
21
22import sys
23import copy
24
25from ansible import constants as C
26from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule
27from ansible_collections.community.network.plugins.module_utils.network.aireos.aireos import aireos_provider_spec
28from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider
29from ansible.utils.display import Display
30
31display = Display()
32
33
34class ActionModule(ActionNetworkModule):
35
36    def run(self, tmp=None, task_vars=None):
37        del tmp  # tmp no longer has any effect
38
39        module_name = self._task.action.split('.')[-1]
40        self._config_module = True if module_name == 'aireos_config' else False
41        persistent_connection = self._play_context.connection.split('.')[-1]
42
43        if persistent_connection == 'network_cli':
44            provider = self._task.args.get('provider', {})
45            if any(provider.values()):
46                display.warning('provider is unnecessary when using network_cli and will be ignored')
47                del self._task.args['provider']
48        elif self._play_context.connection == 'local':
49            provider = load_provider(aireos_provider_spec, self._task.args)
50            pc = copy.deepcopy(self._play_context)
51            pc.connection = 'network_cli'
52            pc.network_os = 'aireos'
53            pc.remote_addr = provider['host'] or self._play_context.remote_addr
54            pc.port = int(provider['port'] or self._play_context.port or 22)
55            pc.remote_user = provider['username'] or self._play_context.connection_user
56            pc.password = provider['password'] or self._play_context.password
57            command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
58
59            connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin,
60                                                                       task_uuid=self._task._uuid)
61
62            display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
63            connection.set_options(direct={'persistent_command_timeout': command_timeout})
64
65            socket_path = connection.run()
66            display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
67            if not socket_path:
68                return {'failed': True,
69                        'msg': 'unable to open shell. Please see: ' +
70                               'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
71
72            task_vars['ansible_socket'] = socket_path
73            msg = "connection local support for this module is deprecated use either" \
74                  " 'network_cli' or 'ansible.netcommon.network_cli' connection"
75            display.deprecated(msg, version='4.0.0', collection_name='community.network')
76
77        else:
78            return dict(
79                failed=True,
80                msg='invalid connection specified, expected connection is either network_cli or ansible.netcommon.network_cli'
81                    'got %s' % self._play_context.connection
82            )
83
84        if self._play_context.become_method and self._play_context.become_method.split('.')[-1] == 'enable':
85            self._play_context.become = False
86            self._play_context.become_method = None
87
88        result = super(ActionModule, self).run(task_vars=task_vars)
89        return result
90