def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect if self._play_context.connection == 'local': provider = load_provider(asa_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'asa' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) pc.become = provider['authorize'] or False pc.become_pass = provider['auth_pass'] pc.become_method = 'enable' 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) 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 result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect self._config_module = True if self._task.action == 'ironware_config' else False socket_path = None 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') del self._task.args['provider'] elif self._play_context.connection == 'local': provider = load_provider(ironware_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ironware' 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 pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] 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) command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout') 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 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 if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) try: out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() except ConnectionError as exc: return {'failed': True, 'msg': to_text(exc)} result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect if self._play_context.connection == 'local': provider = load_provider(asa_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'asa' 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.become = provider['authorize'] or False pc.become_pass = provider['auth_pass'] pc.become_method = 'enable' 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) 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 result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None if self._play_context.connection == 'local': provider = load_provider(enos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'enos' pc.remote_addr = provider['host'] or self._play_context.remote_addr pc.port = 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.become = provider['authorize'] or True pc.become_pass = provider['auth_pass'] pc.become_method = 'enable' 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) 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 # make sure we are in the right cli context which should be # enable mode and not config module or exec mode if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('In Config mode, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') else: conn.send_command('enable') result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect if (self._play_context.connection == 'local' or self._play_context.connection == 'network_cli'): provider = load_provider(arubaoss_provider_spec, self._task.args) transport = provider['transport'] or 'aossapi' display.vvvv('connection transport is %s for %s' % (transport, self._play_context.remote_addr)) if not provider.get('api_version') is None: api = provider.get('api_version') if api not in ['v1.0', 'v2.0', 'v2.1', 'v2.2', 'v3.0', 'v3.1', 'v4.0', 'v5.0']: provider['api_version'] = None display.vvvv('%s is not valid api version.' 'using aossapi v6.0 instead' % api) else: display.vvvv('%s is not latest version (v6.0),' 'arubaoss module may not work as intended' % api) if transport == 'cli': return dict( failed=True, msg='invalid connection specified, expected' 'connection=local, got %s' % self._play_context.connection) else: self._task.args['provider'] = \ ActionModule.aossapi_implementation(provider, self._play_context) result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'ce_config' else False socket_path = None persistent_connection = self._play_context.connection.split('.')[-1] if self._play_context.connection == 'local': provider = load_provider(ce_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ce' 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) self._task.args['provider'] = provider.update( host=pc.remote_addr, port=pc.port, username=pc.remote_user, password=pc.password ) 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}) 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 # make sure a transport value is set in args self._task.args['transport'] = transport self._task.args['provider'] = provider elif persistent_connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if (persistent_connection == 'network_cli' and module_name not in CLI_SUPPORTED_MODULES) or \ (persistent_connection == 'netconf' and module_name in CLI_SUPPORTED_MODULES): return {'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module." % (self._play_context.connection, self._task.action)} result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): 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(dellos10_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'dellos10' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) pc.become = provider['authorize'] or False pc.become_pass = provider['auth_pass'] 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) 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' } # make sure we are in the right cli context which should be # enable mode and not config module conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() task_vars['ansible_socket'] = socket_path if self._play_context.become_method == 'enable': self._play_context.become = False self._play_context.become_method = None result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'junos_config' else False if self._play_context.connection == 'local': provider = load_provider(junos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.network_os = 'junos' pc.remote_addr = provider['host'] or self._play_context.remote_addr if provider['transport'] == 'cli' and module_name not in CLI_SUPPORTED_MODULES: return {'failed': True, 'msg': "Transport type '%s' is not valid for '%s' module. " "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" % (provider['transport'], module_name)} if module_name == 'junos_netconf' or (provider['transport'] == 'cli' and module_name == 'junos_command'): pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) else: pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) 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 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) command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout') 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 elif self._play_context.connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): # for legacy reasons provider value is required for junos_facts(optional) and junos_package # modules as it uses junos_eznc library to connect to remote host if not (module_name == 'junos_facts' or module_name == 'junos_package'): display.warning('provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if (self._play_context.connection == 'network_cli' and module_name not in CLI_SUPPORTED_MODULES) or \ (self._play_context.connection == 'netconf' and module_name in CLI_SUPPORTED_MODULES[0:2]): return {'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module. " "Please see https://docs.ansible.com/ansible/latest/network/user_guide/platform_junos.html" % (self._play_context.connection, module_name)} result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'bigip_imish_config' else False persistent_connection = self._play_context.connection.split('.')[-1] socket_path = None transport = 'rest' 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") elif self._play_context.connection == 'local': provider = load_provider(f5_provider_spec, self._task.args) transport = provider['transport'] or transport display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'bigip' pc.remote_addr = provider.get('server', self._play_context.remote_addr) pc.port = int(provider['server_port'] or self._play_context.port or 22) pc.remote_user = provider.get('user', self._play_context.connection_user) pc.password = provider.get('password', 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) display.vvv('using connection plugin %s' % 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.connection == 'local' and transport == 'cli') or persistent_connection == 'network_cli': # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while '(config' in to_text(out, errors='surrogate_then_replace').strip(): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): socket_path = None 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(vyos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'vyos' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) connection = self._shared_loader_obj.connection_loader.get( 'persistent', pc, sys.stdin) 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 # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('abort') out = conn.get_prompt() result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect 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(ce_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ce' 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.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) self._task.args['provider'] = provider.update( host=pc.remote_addr, port=pc.port, username=pc.remote_user, password=pc.password ) 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) 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'} # make sure we are in the right cli context which should be # enable mode and not config module conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(']'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() task_vars['ansible_socket'] = socket_path # make sure a transport value is set in args self._task.args['transport'] = transport self._task.args['provider'] = provider result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect 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' ) del self._task.args['provider'] elif self._play_context.connection == 'local': provider = load_provider(sros_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'sros' 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) 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) 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 else: return { 'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection } result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'eos_config' else False if self._play_context.connection in ('network_cli', 'httpapi'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if self._task.args.get('transport'): display.warning('transport is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['transport'] elif self._play_context.connection == 'local': provider = load_provider(eos_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'eos' 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 pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] 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) command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout') 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 else: self._task.args['provider'] = ActionModule.eapi_implementation(provider, self._play_context) else: return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): transport = self._task.args.get('transport', 'rest') display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': provider = load_provider(F5_COMMON_ARGS, self._task.args) self._task.args.pop('provider', None) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'bigip' pc.remote_addr = provider.get('server', self._play_context.remote_addr) pc.port = int(provider['server_port'] or self._play_context.port or 22) pc.remote_user = provider.get('user', self._play_context.connection_user) pc.password = provider.get('password', self._play_context.password) pc.timeout = int( provider.get('timeout', 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) 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' } # make sure we are in the right cli context which should be # enable mode and not config module conn = Connection(socket_path) out = conn.get_prompt() while '(config' in to_text( out, errors='surrogate_then_replace').strip(): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() task_vars['ansible_socket'] = socket_path result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None 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') del self._task.args['provider'] elif self._play_context.connection == 'local': provider = load_provider(ios_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ios' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] 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) 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 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 if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'aruba_config' else False 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) 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) 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': self._play_context.become = False self._play_context.become_method = None result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): socket_path = None transport = 'rest' 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(f5_provider_spec, self._task.args) transport = provider['transport'] or transport display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'bigip' pc.remote_addr = provider.get('server', self._play_context.remote_addr) pc.port = int(provider['server_port'] or self._play_context.port or 22) pc.remote_user = provider.get('user', self._play_context.connection_user) pc.password = provider.get('password', self._play_context.password) pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) 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 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 # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while '(config' in to_text(out, errors='surrogate_then_replace').strip(): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): 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(mlnxos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'mlnxos' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) connection = self._shared_loader_obj.connection_loader.get( 'persistent', pc, sys.stdin) 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': self._play_context.become = False self._play_context.become_method = None result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): if self._play_context.connection == 'local': provider = load_provider(sros_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'sros' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) connection = self._shared_loader_obj.connection_loader.get( 'persistent', pc, sys.stdin) 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 result = super(ActionModule, self).run(tmp, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect 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') del self._task.args['provider'] elif self._play_context.connection == 'local': provider = load_provider(sros_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'sros' 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 pc.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) 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 else: return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection} result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'vyos_config' else False persistent_connection = self._play_context.connection.split('.')[-1] warnings = [] 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(vyos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.connection = 'ansible.netcommon.network_cli' pc.network_os = 'vyos.vyos.vyos' 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 connection = self._shared_loader_obj.connection_loader.get( 'ansible.netcommon.persistent', pc, sys.stdin, task_uuid=self._task._uuid) # TODO: Remove below code after ansible minimal is cut out if connection is None: pc.connection = 'network_cli' pc.network_os = 'vyos' 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) command_timeout = int( provider['timeout'] ) if provider['timeout'] else connection.get_option( 'persistent_command_timeout') 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 warnings.append([ 'connection local support for this module is deprecated and will be removed in version 2.14, use connection %s' % pc.connection ]) else: return { 'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection } result = super(ActionModule, self).run(task_vars=task_vars) if warnings: if 'warnings' in result: result['warnings'].extend(warnings) else: result['warnings'] = warnings return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module = module_loader._load_module_source( self._task.action, module_loader.find_plugin(self._task.action)) if not getattr(module, 'USE_PERSISTENT_CONNECTION', False): return super(ActionModule, self).run(task_vars=task_vars) socket_path = None if self._play_context.connection == 'local': provider = load_provider(junos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.network_os = 'junos' pc.remote_addr = provider['host'] or self._play_context.remote_addr if self._task.action == 'junos_netconf' or ( provider['transport'] == 'cli' and self._task.action == 'junos_command'): pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) else: pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) 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 pc.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) 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 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 # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text( out, errors='surrogate_then_replace').strip().endswith('#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(None, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None if self._play_context.connection == 'local': provider = load_provider(ce_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ce' 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) self._task.args['provider'] = provider.update( host=pc.remote_addr, port=pc.port, username=pc.remote_user, password=pc.password ) if self._task.action in ['ce_netconf'] or self._task.action 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) 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 # make sure a transport value is set in args self._task.args['transport'] = transport self._task.args['provider'] = provider elif self._play_context.connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnessary whene using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if (self._play_context.connection == 'network_cli' and self._task.action not in CLI_SUPPORTED_MODULES) or \ (self._play_context.connection == 'netconf' and self._task.action in CLI_SUPPORTED_MODULES): return {'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module." % (self._play_context.connection, self._task.action)} if (self._play_context.connection == 'local' and transport == 'cli' and self._task.action in CLI_SUPPORTED_MODULES) \ or self._play_context.connection == 'network_cli': # make sure we are in the right cli context whitch should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith(']'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None play_context = copy.deepcopy(self._play_context) play_context.network_os = self._get_network_os(task_vars) if play_context.connection == 'local': # we should be able to stream line this a bit by creating a common # provider argument spec in module_utils/network/common/utils.py or another # option is that there isn't a need to push provider into the module # since the connection is started in the action handler. module_name = 'ansible.module_utils.network.{0}.{0}'.format(play_context.network_os) f, p, d = find_module('ansible') for package in module_name.split('.')[1:]: f, p, d = find_module(package, [p]) module = load_module(module_name, f, p, d) self.provider = load_provider(module.get_provider_argspec(), self._task.args) if self.provider.get('transport') == 'netconf' and play_context.network_os in _NETCONF_SUPPORTED_PLATFORMS \ and self._task.action not in _CLI_ONLY_MODULES: play_context.connection = 'netconf' play_context.port = int(self.provider['port'] or self._play_context.port or 830) elif self.provider.get('transport') in ('nxapi', 'eapi') and play_context.network_os in ('nxos', 'eos'): play_context.connection = play_context.connection play_context.port = int(self.provider['port'] or self._play_context.port or 22) else: play_context.connection = 'network_cli' play_context.port = int(self.provider['port'] or self._play_context.port or 22) play_context.remote_addr = self.provider['host'] or self._play_context.remote_addr play_context.remote_user = self.provider['username'] or self._play_context.connection_user play_context.password = self.provider['password'] or self._play_context.password play_context.private_key_file = self.provider['ssh_keyfile'] or self._play_context.private_key_file play_context.timeout = int(self.provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) if 'authorize' in self.provider.keys(): play_context.become = self.provider['authorize'] or False play_context.become_pass = self.provider['auth_pass'] play_context.become_method = 'enable' if self._play_context.connection == 'local': if self.provider.get('transport') == 'nxapi' and play_context.network_os == 'nxos': self._task.args['provider'] = _NxosActionModule.nxapi_implementation(self.provider, self._play_context) elif self.provider.get('transport') == 'eapi' and play_context.network_os == 'eos': self._task.args['provider'] = _EosActionModule.eapi_implementation(self.provider, self._play_context) else: socket_path = self._start_connection(play_context) task_vars['ansible_socket'] = socket_path else: provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnecessary when using %s and will be ignored' % play_context.connection) del self._task.args['provider'] if play_context.connection == 'network_cli': # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') if 'fail_on_missing_module' not in self._task.args: self._task.args['fail_on_missing_module'] = False result = super(ActionModule, self).run(task_vars=task_vars) module = self._get_implementation_module(play_context.network_os, self._task.action) if not module: if self._task.args['fail_on_missing_module']: result['failed'] = True else: result['failed'] = False result['msg'] = ('Could not find implementation module %s for %s' % (self._task.action, play_context.network_os)) else: new_module_args = self._task.args.copy() # perhaps delete the provider argument here as well since the # module code doesn't need the information, the connection is # already started if 'network_os' in new_module_args: del new_module_args['network_os'] del new_module_args['fail_on_missing_module'] display.vvvv('Running implementation module %s' % module) result.update(self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val)) display.vvvv('Caching network OS %s in facts' % play_context.network_os) result['ansible_facts'] = {'network_os': play_context.network_os} return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect self._config_module = True if self._task.action == 'nxos_config' else False socket_path = None if (self._play_context.connection == 'httpapi' or self._task.args.get('provider', {}).get('transport') == 'nxapi') \ and self._task.action in ('nxos_file_copy', 'nxos_nxapi'): return {'failed': True, 'msg': "Transport type 'nxapi' is not valid for '%s' module." % (self._task.action)} if self._task.action == 'nxos_file_copy': self._task.args['host'] = self._play_context.remote_addr self._task.args['password'] = self._play_context.password if self._play_context.connection == 'network_cli': self._task.args['username'] = self._play_context.remote_user elif self._play_context.connection == 'local': self._task.args['username'] = self._play_context.connection_user if self._task.action == 'nxos_install_os': persistent_command_timeout = 0 persistent_connect_timeout = 0 connection = self._connection if connection.transport == 'local': persistent_command_timeout = C.PERSISTENT_COMMAND_TIMEOUT persistent_connect_timeout = C.PERSISTENT_CONNECT_TIMEOUT else: persistent_command_timeout = connection.get_option('persistent_command_timeout') persistent_connect_timeout = connection.get_option('persistent_connect_timeout') display.vvvv('PERSISTENT_COMMAND_TIMEOUT is %s' % str(persistent_command_timeout), self._play_context.remote_addr) display.vvvv('PERSISTENT_CONNECT_TIMEOUT is %s' % str(persistent_connect_timeout), self._play_context.remote_addr) if persistent_command_timeout < 600 or persistent_connect_timeout < 600: msg = 'PERSISTENT_COMMAND_TIMEOUT and PERSISTENT_CONNECT_TIMEOUT' msg += ' must be set to 600 seconds or higher when using nxos_install_os module.' msg += ' Current persistent_command_timeout setting:' + str(persistent_command_timeout) msg += ' Current persistent_connect_timeout setting:' + str(persistent_connect_timeout) return {'failed': True, 'msg': msg} if self._play_context.connection in ('network_cli', 'httpapi'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning('provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if self._task.args.get('transport'): display.warning('transport is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['transport'] elif self._play_context.connection == 'local': provider = load_provider(nxos_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'nxos' 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 pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] 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) command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout') 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 else: self._task.args['provider'] = ActionModule.nxapi_implementation(provider, self._play_context) 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 # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) # Match prompts ending in )# except those with (maint-mode)# config_prompt = re.compile(r'^.*\((?!maint-mode).*\)#$') out = conn.get_prompt() while config_prompt.match(to_text(out, errors='surrogate_then_replace').strip()): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None play_context = copy.deepcopy(self._play_context) play_context.network_os = self._get_network_os(task_vars) if play_context.connection == 'local': # we should be able to stream line this a bit by creating a common # provider argument spec in module_utils/network/common/utils.py or another # option is that there isn't a need to push provider into the module # since the connection is started in the action handler. module_name = 'ansible.module_utils.network.{0}.{0}'.format( play_context.network_os) f, p, d = find_module('ansible') for package in module_name.split('.')[1:]: f, p, d = find_module(package, [p]) module = load_module(module_name, f, p, d) self.provider = load_provider(module.get_provider_argspec(), self._task.args) if self.provider.get('transport') == 'netconf' and play_context.network_os in _NETCONF_SUPPORTED_PLATFORMS \ and self._task.action not in _CLI_ONLY_MODULES: play_context.connection = 'netconf' play_context.port = int(self.provider['port'] or self._play_context.port or 830) elif self.provider.get('transport') in ( 'nxapi', 'eapi') and play_context.network_os in ('nxos', 'eos'): play_context.connection = play_context.connection play_context.port = int(self.provider['port'] or self._play_context.port or 22) else: play_context.connection = 'network_cli' play_context.port = int(self.provider['port'] or self._play_context.port or 22) play_context.remote_addr = self.provider[ 'host'] or self._play_context.remote_addr play_context.remote_user = self.provider[ 'username'] or self._play_context.connection_user play_context.password = self.provider[ 'password'] or self._play_context.password play_context.private_key_file = self.provider[ 'ssh_keyfile'] or self._play_context.private_key_file play_context.timeout = int(self.provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) if 'authorize' in self.provider.keys(): play_context.become = self.provider['authorize'] or False play_context.become_pass = self.provider['auth_pass'] play_context.become_method = 'enable' if self._play_context.connection == 'local': if self.provider.get( 'transport' ) == 'nxapi' and play_context.network_os == 'nxos': self._task.args[ 'provider'] = _NxosActionModule.nxapi_implementation( self.provider, self._play_context) elif self.provider.get( 'transport' ) == 'eapi' and play_context.network_os == 'eos': self._task.args[ 'provider'] = _EosActionModule.eapi_implementation( self.provider, self._play_context) else: socket_path = self._start_connection(play_context) task_vars['ansible_socket'] = socket_path else: provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning( 'provider is unnecessary when using %s and will be ignored' % play_context.connection) del self._task.args['provider'] if play_context.connection == 'network_cli': # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') if 'fail_on_missing_module' not in self._task.args: self._task.args['fail_on_missing_module'] = True result = super(ActionModule, self).run(task_vars=task_vars) module = self._get_implementation_module(play_context.network_os, self._task.action) if not module: if self._task.args['fail_on_missing_module']: result['failed'] = True else: result['failed'] = False result['msg'] = ('Could not find implementation module %s for %s' % (self._task.action, play_context.network_os)) else: new_module_args = self._task.args.copy() # perhaps delete the provider argument here as well since the # module code doesn't need the information, the connection is # already started if 'network_os' in new_module_args: del new_module_args['network_os'] del new_module_args['fail_on_missing_module'] display.vvvv('Running implementation module %s' % module) result.update( self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val)) display.vvvv('Caching network OS %s in facts' % play_context.network_os) result['ansible_facts'] = {'network_os': play_context.network_os} return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect 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(ce_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'ce' 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.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) self._task.args['provider'] = provider.update( host=pc.remote_addr, port=pc.port, username=pc.remote_user, password=pc.password) 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) 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' } # make sure we are in the right cli context which should be # enable mode and not config module conn = Connection(socket_path) out = conn.get_prompt() while to_text( out, errors='surrogate_then_replace').strip().endswith(']'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() task_vars['ansible_socket'] = socket_path # make sure a transport value is set in args self._task.args['transport'] = transport self._task.args['provider'] = provider result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None 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(eos_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'eos' 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 pc.timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] 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) 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 else: self._task.args['provider'] = ActionModule.eapi_implementation( provider, self._play_context) 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 # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while '(config' in to_text( out, errors='surrogate_then_replace').strip(): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('abort') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module = module_loader._load_module_source( self._task.action, module_loader.find_plugin(self._task.action)) if not getattr(module, 'USE_PERSISTENT_CONNECTION', False): return super(ActionModule, self).run(task_vars=task_vars) socket_path = None if self._play_context.connection == 'local': provider = load_provider(junos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.network_os = 'junos' pc.remote_addr = provider['host'] or self._play_context.remote_addr if provider[ 'transport'] == 'cli' and self._task.action not in CLI_SUPPORTED_MODULES: return { 'failed': True, 'msg': "Transport type '%s' is not valid for '%s' module. " "Please see https://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'): pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) else: pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) 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 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) command_timeout = int( provider['timeout'] ) if provider['timeout'] else connection.get_option( 'persistent_command_timeout') 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 elif self._play_context.connection in ('netconf', 'network_cli'): provider = self._task.args.get('provider', {}) if any(provider.values()): # for legacy reasons provider value is required for junos_facts(optional) and junos_package # modules as it uses junos_eznc library to connect to remote host if not (self._task.action == 'junos_facts' or self._task.action == 'junos_package'): display.warning( 'provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if (self._play_context.connection == 'network_cli' and self._task.action not in CLI_SUPPORTED_MODULES) or \ (self._play_context.connection == 'netconf' and self._task.action == 'junos_netconf'): return { 'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module. " "Please see https://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': # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text( out, errors='surrogate_then_replace').strip().endswith('#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(None, task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'nxos_config' else False persistent_connection = self._play_context.connection.split('.')[-1] warnings = [] if (self._play_context.connection in ('httpapi', 'local') or self._task.args.get('provider', {}).get('transport') == 'nxapi') \ and module_name in ('nxos_file_copy', 'nxos_nxapi'): return { 'failed': True, 'msg': "Transport type 'nxapi' is not valid for '%s' module." % (module_name) } if module_name == 'nxos_file_copy': self._task.args['host'] = self._play_context.remote_addr self._task.args['password'] = self._play_context.password if self._play_context.connection == 'network_cli': self._task.args['username'] = self._play_context.remote_user elif self._play_context.connection == 'local': self._task.args[ 'username'] = self._play_context.connection_user if module_name == 'nxos_install_os': connection = self._connection if connection.transport == 'local': persistent_command_timeout = C.PERSISTENT_COMMAND_TIMEOUT persistent_connect_timeout = C.PERSISTENT_CONNECT_TIMEOUT else: persistent_command_timeout = connection.get_option( 'persistent_command_timeout') persistent_connect_timeout = connection.get_option( 'persistent_connect_timeout') display.vvvv( 'PERSISTENT_COMMAND_TIMEOUT is %s' % str(persistent_command_timeout), self._play_context.remote_addr) display.vvvv( 'PERSISTENT_CONNECT_TIMEOUT is %s' % str(persistent_connect_timeout), self._play_context.remote_addr) if persistent_command_timeout < 600 or persistent_connect_timeout < 600: msg = 'PERSISTENT_COMMAND_TIMEOUT and PERSISTENT_CONNECT_TIMEOUT' msg += ' must be set to 600 seconds or higher when using nxos_install_os module.' msg += ' Current persistent_command_timeout setting:' + str( persistent_command_timeout) msg += ' Current persistent_connect_timeout setting:' + str( persistent_connect_timeout) return {'failed': True, 'msg': msg} if persistent_connection in ('network_cli', 'httpapi'): provider = self._task.args.get('provider', {}) if any(provider.values()): display.warning( 'provider is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if self._task.args.get('transport'): display.warning( 'transport is unnecessary when using %s and will be ignored' % self._play_context.connection) del self._task.args['transport'] if module_name == 'nxos_gir': conn = Connection(self._connection.socket_path) persistent_command_timeout = conn.get_option( 'persistent_command_timeout') gir_timeout = 200 if persistent_command_timeout < gir_timeout: conn.set_option('persistent_command_timeout', gir_timeout) msg = "timeout value extended to %ss for nxos_gir" % gir_timeout display.warning(msg) elif self._play_context.connection == 'local': provider = load_provider(nxos_provider_spec, self._task.args) transport = provider['transport'] or 'cli' display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'ansible.netcommon.network_cli' pc.network_os = 'cisco.nxos.nxos' 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 pc.become = provider['authorize'] or False if pc.become: pc.become_method = 'enable' pc.become_pass = provider['auth_pass'] connection = self._shared_loader_obj.connection_loader.get( 'ansible.netcommon.persistent', pc, sys.stdin, task_uuid=self._task._uuid) # TODO: Remove below code after ansible minimal is cut out if connection is None: pc.connection = 'network_cli' pc.network_os = 'nxos' 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) command_timeout = int( provider['timeout'] ) if provider['timeout'] else connection.get_option( 'persistent_command_timeout') 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 else: self._task.args[ 'provider'] = ActionModule.nxapi_implementation( provider, self._play_context) warnings.append([ 'connection local support for this module is deprecated and will be removed in version 2.14,' ' use connection either httpapi or ansible.netcommon.httpapi (whichever is applicable)' ]) else: return { 'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection } result = super(ActionModule, self).run(task_vars=task_vars) if warnings: if 'warnings' in result: result['warnings'].extend(warnings) else: result['warnings'] = warnings return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect socket_path = None force_cli = self._task.action in ('iosxr_netconf', 'iosxr_config', 'iosxr_command', 'iosxr_facts') if self._play_context.connection == 'local': provider = load_provider(iosxr_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) if force_cli or provider['transport'] == 'cli': pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) elif provider['transport'] == 'netconf': pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) else: return { 'failed': True, 'msg': 'Transport type %s is not valid for this module' % provider['transport'] } pc.network_os = 'iosxr' 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.timeout = int( provider['timeout']) if provider['timeout'] else None 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) if connection._play_context.timeout is None: connection._play_context.timeout = connection.get_option( 'persistent_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 elif self._play_context.connection in ('netconf', 'network_cli'): if force_cli and self._play_context.connection != 'network_cli': return { 'failed': True, 'msg': 'Connection type %s is not valid for module %s' % (self._play_context.connection, self._task.action) } 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)) del self._task.args['provider'] 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 if (self._play_context.connection == 'local' and pc.connection == 'network_cli' ) or self._play_context.connection == 'network_cli': if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text( out, errors='surrogate_then_replace').strip().endswith(')#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('abort') out = conn.get_prompt() result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module_name = self._task.action.split('.')[-1] self._config_module = True if module_name == 'iosxr_config' else False force_cli = module_name in ('iosxr_netconf', 'iosxr_config', 'iosxr_command', 'iosxr_facts') if self._play_context.connection == 'local': provider = load_provider(iosxr_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) if force_cli or provider['transport'] == 'cli': pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) elif provider['transport'] == 'netconf': pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) else: return { 'failed': True, 'msg': 'Transport type %s is not valid for this module' % provider['transport'] } pc.network_os = 'iosxr' 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 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) command_timeout = int( provider['timeout'] ) if provider['timeout'] else connection.get_option( 'persistent_command_timeout') 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 elif self._play_context.connection in ('netconf', 'network_cli'): if force_cli and self._play_context.connection != 'network_cli': return { 'failed': True, 'msg': 'Connection type %s is not valid for module %s' % (self._play_context.connection, module_name) } 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)) del self._task.args['provider'] else: return { 'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection } result = super(ActionModule, self).run(task_vars=task_vars) return result
def run(self, tmp=None, task_vars=None): del tmp # tmp no longer has any effect module = module_loader._load_module_source(self._task.action, module_loader.find_plugin(self._task.action)) if not getattr(module, 'USE_PERSISTENT_CONNECTION', False): return super(ActionModule, self).run(task_vars=task_vars) socket_path = None if self._play_context.connection == 'local': provider = load_provider(junos_provider_spec, self._task.args) pc = copy.deepcopy(self._play_context) pc.network_os = 'junos' pc.remote_addr = provider['host'] or self._play_context.remote_addr if provider['transport'] == 'cli' and self._task.action not in CLI_SUPPORTED_MODULES: return {'failed': True, 'msg': "Transport type '%s' is not valid for '%s' module. " "Please see https://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'): pc.connection = 'network_cli' pc.port = int(provider['port'] or self._play_context.port or 22) else: pc.connection = 'netconf' pc.port = int(provider['port'] or self._play_context.port or 830) 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 pc.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) 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 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 %s and will be ignored' % self._play_context.connection) del self._task.args['provider'] if (self._play_context.connection == 'network_cli' and self._task.action not in CLI_SUPPORTED_MODULES) or \ (self._play_context.connection == 'netconf' and self._task.action == 'junos_netconf'): return {'failed': True, 'msg': "Connection type '%s' is not valid for '%s' module. " "Please see https://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': # make sure we are in the right cli context which should be # enable mode and not config module if socket_path is None: socket_path = self._connection.socket_path conn = Connection(socket_path) out = conn.get_prompt() while to_text(out, errors='surrogate_then_replace').strip().endswith('#'): display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) conn.send_command('exit') out = conn.get_prompt() result = super(ActionModule, self).run(None, task_vars) return result