PageRenderTime 35ms CodeModel.GetById 16ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/ansible/plugins/action/iosxr.py

https://github.com/debfx/ansible
Python | 102 lines | 66 code | 16 blank | 20 comment | 28 complexity | 418a51fb46941f40711e07fcf1a81478 MD5 | raw file
  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.module_utils._text import to_text
 27from ansible.module_utils.connection import Connection
 28from ansible.module_utils.network.iosxr.iosxr import iosxr_provider_spec
 29from ansible.plugins.action.network import ActionModule as ActionNetworkModule
 30from ansible.module_utils.network.common.utils import load_provider
 31from ansible.utils.display import Display
 32
 33display = Display()
 34
 35
 36class ActionModule(ActionNetworkModule):
 37
 38    def run(self, tmp=None, task_vars=None):
 39        del tmp  # tmp no longer has any effect
 40
 41        self._config_module = True if self._task.action == 'iosxr_config' else False
 42        socket_path = None
 43        force_cli = self._task.action in ('iosxr_netconf', 'iosxr_config', 'iosxr_command', 'iosxr_facts')
 44
 45        if self._play_context.connection == 'local':
 46            provider = load_provider(iosxr_provider_spec, self._task.args)
 47            pc = copy.deepcopy(self._play_context)
 48            if force_cli or provider['transport'] == 'cli':
 49                pc.connection = 'network_cli'
 50                pc.port = int(provider['port'] or self._play_context.port or 22)
 51            elif provider['transport'] == 'netconf':
 52                pc.connection = 'netconf'
 53                pc.port = int(provider['port'] or self._play_context.port or 830)
 54            else:
 55                return {'failed': True, 'msg': 'Transport type %s is not valid for this module' % provider['transport']}
 56
 57            pc.network_os = 'iosxr'
 58            pc.remote_addr = provider['host'] or self._play_context.remote_addr
 59            pc.port = int(provider['port'] or self._play_context.port or 22)
 60            pc.remote_user = provider['username'] or self._play_context.connection_user
 61            pc.password = provider['password'] or self._play_context.password
 62
 63            display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
 64            connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
 65
 66            command_timeout = int(provider['timeout']) if provider['timeout'] else connection.get_option('persistent_command_timeout')
 67            connection.set_options(direct={'persistent_command_timeout': command_timeout})
 68
 69            socket_path = connection.run()
 70            display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
 71            if not socket_path:
 72                return {'failed': True,
 73                        'msg': 'unable to open shell. Please see: ' +
 74                               'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
 75
 76            task_vars['ansible_socket'] = socket_path
 77        elif self._play_context.connection in ('netconf', 'network_cli'):
 78            if force_cli and self._play_context.connection != 'network_cli':
 79                return {'failed': True, 'msg': 'Connection type %s is not valid for module %s' %
 80                        (self._play_context.connection, self._task.action)}
 81            provider = self._task.args.get('provider', {})
 82            if any(provider.values()):
 83                display.warning('provider is unnecessary when using {0} and will be ignored'.format(self._play_context.connection))
 84                del self._task.args['provider']
 85        else:
 86            return {'failed': True, 'msg': 'Connection type %s is not valid for this module' % self._play_context.connection}
 87
 88        # make sure we are in the right cli context which should be
 89        # enable mode and not config module
 90        if (self._play_context.connection == 'local' and pc.connection == 'network_cli') or self._play_context.connection == 'network_cli':
 91            if socket_path is None:
 92                socket_path = self._connection.socket_path
 93
 94            conn = Connection(socket_path)
 95            out = conn.get_prompt()
 96            while to_text(out, errors='surrogate_then_replace').strip().endswith(')#'):
 97                display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
 98                conn.send_command('abort')
 99                out = conn.get_prompt()
100
101        result = super(ActionModule, self).run(task_vars=task_vars)
102        return result