/lib/ansible/plugins/action/aruba.py

https://github.com/debfx/ansible · Python · 88 lines · 52 code · 16 blank · 20 comment · 13 complexity · fb24bfc7175562e70b36f032bb6cfe7e 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. #
  19. from __future__ import (absolute_import, division, print_function)
  20. __metaclass__ = type
  21. import sys
  22. import copy
  23. from ansible import constants as C
  24. from ansible.module_utils._text import to_text
  25. from ansible.module_utils.connection import Connection
  26. from ansible.plugins.action.network import ActionModule as ActionNetworkModule
  27. from ansible.module_utils.network.aruba.aruba import aruba_provider_spec
  28. from ansible.module_utils.network.common.utils import load_provider
  29. from ansible.utils.display import Display
  30. display = Display()
  31. class ActionModule(ActionNetworkModule):
  32. def run(self, tmp=None, task_vars=None):
  33. del tmp # tmp no longer has any effect
  34. self._config_module = True if self._task.action == 'aruba_config' else False
  35. if self._play_context.connection != 'local':
  36. return dict(
  37. failed=True,
  38. msg='invalid connection specified, expected connection=local, '
  39. 'got %s' % self._play_context.connection
  40. )
  41. provider = load_provider(aruba_provider_spec, self._task.args)
  42. pc = copy.deepcopy(self._play_context)
  43. pc.connection = 'network_cli'
  44. pc.network_os = 'aruba'
  45. pc.remote_addr = provider['host'] or self._play_context.remote_addr
  46. pc.port = int(provider['port'] or self._play_context.port or 22)
  47. pc.remote_user = provider['username'] or self._play_context.connection_user
  48. pc.password = provider['password'] or self._play_context.password
  49. pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
  50. command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
  51. display.vvv('using connection plugin %s (was local)' % pc.connection, pc.remote_addr)
  52. connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
  53. connection.set_options(direct={'persistent_command_timeout': command_timeout})
  54. socket_path = connection.run()
  55. display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
  56. if not socket_path:
  57. return {'failed': True,
  58. 'msg': 'unable to open shell. Please see: ' +
  59. 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
  60. # make sure we are in the right cli context which should be
  61. # enable mode and not config module
  62. conn = Connection(socket_path)
  63. out = conn.get_prompt()
  64. if to_text(out, errors='surrogate_then_replace').strip().endswith(')#'):
  65. display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
  66. conn.send_command('exit')
  67. task_vars['ansible_socket'] = socket_path
  68. if self._play_context.become_method == 'enable':
  69. self._play_context.become = False
  70. self._play_context.become_method = None
  71. result = super(ActionModule, self).run(task_vars=task_vars)
  72. return result