PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/ansible/plugins/action/netconf.py

https://github.com/debfx/ansible
Python | 73 lines | 40 code | 15 blank | 18 comment | 20 complexity | afa00b98c2204c79c1a3aaa7e0ebe53d MD5 | raw file
 1#
 2# Copyright 2018 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 copy
23import sys
24
25from ansible.plugins.action.network import ActionModule as ActionNetworkModule
26from ansible.utils.display import Display
27
28display = Display()
29
30
31class ActionModule(ActionNetworkModule):
32
33    def run(self, tmp=None, task_vars=None):
34        del tmp  # tmp no longer has any effect
35
36        self._config_module = True if self._task.action == 'netconf_config' else False
37
38        if self._play_context.connection not in ['netconf', 'local'] and self._task.action == 'netconf_config':
39            return {'failed': True, 'msg': 'Connection type %s is not valid for netconf_config module. '
40                                           'Valid connection type is netconf or local (deprecated)' % self._play_context.connection}
41        elif self._play_context.connection not in ['netconf'] and self._task.action != 'netconf_config':
42            return {'failed': True, 'msg': 'Connection type %s is not valid for %s module. '
43                                           'Valid connection type is netconf.' % (self._play_context.connection, self._task.action)}
44
45        if self._play_context.connection == 'local' and self._task.action == 'netconf_config':
46            args = self._task.args
47            pc = copy.deepcopy(self._play_context)
48            pc.connection = 'netconf'
49            pc.port = int(args.get('port') or self._play_context.port or 830)
50
51            pc.remote_user = args.get('username') or self._play_context.connection_user
52            pc.password = args.get('password') or self._play_context.password
53            pc.private_key_file = args.get('ssh_keyfile') or self._play_context.private_key_file
54
55            display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
56            connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
57
58            timeout = args.get('timeout')
59            command_timeout = int(timeout) if timeout else connection.get_option('persistent_command_timeout')
60            connection.set_options(direct={'persistent_command_timeout': command_timeout, 'look_for_keys': args.get('look_for_keys'),
61                                           'hostkey_verify': args.get('hostkey_verify'),
62                                           'allow_agent': args.get('allow_agent')})
63
64            socket_path = connection.run()
65            display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
66            if not socket_path:
67                return {'failed': True,
68                        'msg': 'unable to open shell. Please see: ' +
69                               'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
70
71            task_vars['ansible_socket'] = socket_path
72
73        return super(ActionModule, self).run(task_vars=task_vars)