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

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