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_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule 26from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider 27from ansible_collections.community.network.plugins.module_utils.network.ironware.ironware import ironware_provider_spec 28from ansible.utils.display import Display 29 30display = Display() 31 32 33class ActionModule(ActionNetworkModule): 34 35 def run(self, tmp=None, task_vars=None): 36 del tmp # tmp no longer has any effect 37 38 module_name = self._task.action.split('.')[-1] 39 self._config_module = True if module_name == 'ironware_config' else False 40 persistent_connection = self._play_context.connection.split('.')[-1] 41 42 if persistent_connection == 'network_cli': 43 provider = self._task.args.get('provider', {}) 44 if any(provider.values()): 45 display.warning('provider is unnecessary when using network_cli and will be ignored') 46 del self._task.args['provider'] 47 elif self._play_context.connection == 'local': 48 provider = load_provider(ironware_provider_spec, self._task.args) 49 pc = copy.deepcopy(self._play_context) 50 pc.connection = 'network_cli' 51 pc.network_os = 'ironware' 52 pc.remote_addr = provider['host'] or self._play_context.remote_addr 53 pc.port = int(provider['port'] or self._play_context.port or 22) 54 pc.remote_user = provider['username'] or self._play_context.connection_user 55 pc.password = provider['password'] or self._play_context.password 56 pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file 57 pc.become = provider['authorize'] or False 58 if pc.become: 59 pc.become_method = 'enable' 60 pc.become_pass = provider['auth_pass'] 61 62 display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr) 63 connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin, task_uuid=self._task._uuid) 64 65 command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout') 66 connection.set_options(direct={'persistent_command_timeout': command_timeout}) 67 68 socket_path = connection.run() 69 display.vvvv('socket_path: %s' % socket_path, pc.remote_addr) 70 if not socket_path: 71 return {'failed': True, 72 'msg': 'unable to open shell. Please see: ' + 73 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'} 74 75 task_vars['ansible_socket'] = socket_path 76 msg = "connection local support for this module is deprecated use either" \ 77 " 'network_cli' or 'ansible.netcommon.network_cli' connection" 78 display.deprecated(msg, version='4.0.0', collection_name='community.network') 79 else: 80 return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} 81 82 result = super(ActionModule, self).run(task_vars=task_vars) 83 return result 84