/lib/ansible/plugins/action/bigiq.py

https://github.com/debfx/ansible · Python · 94 lines · 59 code · 15 blank · 20 comment · 17 complexity · b33baa0cdfd695120084a27800cd1bb7 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 os
  22. import sys
  23. import copy
  24. from ansible import constants as C
  25. from ansible.module_utils._text import to_text
  26. from ansible.module_utils.connection import Connection
  27. from ansible.module_utils.network.common.utils import load_provider
  28. from ansible.plugins.action.normal import ActionModule as _ActionModule
  29. from ansible.utils.display import Display
  30. try:
  31. from library.module_utils.network.f5.common import f5_provider_spec
  32. except Exception:
  33. from ansible.module_utils.network.f5.common import f5_provider_spec
  34. display = Display()
  35. class ActionModule(_ActionModule):
  36. def run(self, tmp=None, task_vars=None):
  37. socket_path = None
  38. transport = 'rest'
  39. if self._play_context.connection == 'network_cli':
  40. provider = self._task.args.get('provider', {})
  41. if any(provider.values()):
  42. display.warning("'provider' is unnecessary when using 'network_cli' and will be ignored")
  43. elif self._play_context.connection == 'local':
  44. provider = load_provider(f5_provider_spec, self._task.args)
  45. transport = provider['transport'] or transport
  46. display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr)
  47. if transport == 'cli':
  48. pc = copy.deepcopy(self._play_context)
  49. pc.connection = 'network_cli'
  50. pc.network_os = 'bigiq'
  51. pc.remote_addr = provider.get('server', self._play_context.remote_addr)
  52. pc.port = int(provider['server_port'] or self._play_context.port or 22)
  53. pc.remote_user = provider.get('user', self._play_context.connection_user)
  54. pc.password = provider.get('password', self._play_context.password)
  55. pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
  56. command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT)
  57. display.vvv('using connection plugin %s' % 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. socket_path = connection.run()
  61. display.vvvv('socket_path: %s' % socket_path, pc.remote_addr)
  62. if not socket_path:
  63. return {'failed': True,
  64. 'msg': 'Unable to open shell. Please see: '
  65. 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell'}
  66. task_vars['ansible_socket'] = socket_path
  67. if (self._play_context.connection == 'local' and transport == 'cli') or self._play_context.connection == 'network_cli':
  68. # make sure we are in the right cli context which should be
  69. # enable mode and not config module
  70. if socket_path is None:
  71. socket_path = self._connection.socket_path
  72. conn = Connection(socket_path)
  73. out = conn.get_prompt()
  74. while '(config' in to_text(out, errors='surrogate_then_replace').strip():
  75. display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr)
  76. conn.send_command('exit')
  77. out = conn.get_prompt()
  78. result = super(ActionModule, self).run(tmp, task_vars)
  79. return result