PageRenderTime 37ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ansible/plugins/action/cnos.py

https://github.com/debfx/ansible
Python | 84 lines | 52 code | 15 blank | 17 comment | 15 complexity | 5c24c9230a70c445fd1dc304caf413e8 MD5 | raw file
 1# (C) 2017 Red Hat Inc.
 2# Copyright (C) 2017 Lenovo.
 3#
 4# GNU General Public License v3.0+
 5#
 6# This program is distributed in the hope that it will be useful,
 7# but WITHOUT ANY WARRANTY; without even the implied warranty of
 8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 9# GNU General Public License for more details.
10#
11# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
12#
13# Contains Action Plugin methods for CNOS Config Module
14# Lenovo Networking
15#
16
17from __future__ import (absolute_import, division, print_function)
18__metaclass__ = type
19
20import sys
21import copy
22
23from ansible import constants as C
24from ansible.plugins.action.network import ActionModule as ActionNetworkModule
25from ansible.module_utils.network.cnos.cnos import cnos_provider_spec
26from ansible.module_utils.network.common.utils import load_provider
27from ansible.module_utils.connection import Connection
28from ansible.module_utils._text import to_text
29from ansible.utils.display import Display
30
31display = Display()
32
33
34class ActionModule(ActionNetworkModule):
35
36    def run(self, tmp=None, task_vars=None):
37        del tmp  # tmp no longer has any effect
38
39        self._config_module = True if self._task.action == 'cnos_config' else False
40        socket_path = None
41
42        if self._play_context.connection == 'local':
43            provider = load_provider(cnos_provider_spec, self._task.args)
44            pc = copy.deepcopy(self._play_context)
45            pc.connection = 'network_cli'
46            pc.network_os = 'cnos'
47            pc.remote_addr = provider['host'] or self._play_context.remote_addr
48            pc.port = provider['port'] or self._play_context.port or 22
49            pc.remote_user = provider['username'] or self._play_context.connection_user
50            pc.password = provider['password'] or self._play_context.password
51            pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
52            command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
53            pc.become = provider['authorize'] or True
54            pc.become_pass = provider['auth_pass']
55            pc.become_method = 'enable'
56
57            display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
58            connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
59            connection.set_options(direct={'persistent_command_timeout': command_timeout})
60
61            socket_path = connection.run()
62            display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
63            if not socket_path:
64                return {'failed': True,
65                        'msg': 'unable to open shell. Please see: ' +
66                               'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
67
68            task_vars['ansible_socket'] = socket_path
69
70        # make sure we are in the right cli context which should be
71        # enable mode and not config module or exec mode
72        if socket_path is None:
73            socket_path = self._connection.socket_path
74
75        conn = Connection(socket_path)
76        out = conn.get_prompt()
77        if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'):
78            display.vvvv('In Config mode, sending exit to device', self._play_context.remote_addr)
79            conn.send_command('exit')
80        else:
81            conn.send_command('enable')
82
83        result = super(ActionModule, self).run(task_vars=task_vars)
84        return result