| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import (absolute_import, division, print_function) |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
import sys |
| 23 |
import copy |
| 24 |
|
| 25 |
from ansible import constants as C |
| 26 |
from ansible.module_utils._text import to_text |
| 27 |
from ansible.module_utils.connection import Connection |
| 28 |
from ansible.utils.display import Display |
| 29 |
|
| 30 |
|
| 31 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import load_provider |
| 32 |
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule |
| 33 |
|
| 34 |
|
| 35 |
from ansible_collections.f5networks.f5_modules.plugins.module_utils.common import f5_provider_spec |
| 36 |
|
| 37 |
display = Display() |
| 38 |
|
| 39 |
|
| 40 |
class ActionModule(ActionNetworkModule): |
| 41 |
|
| 42 |
def run(self, task_vars=None): |
| 43 |
|
| 44 |
self._config_module = True if self._task.action == 'bigip_imish_config' else False |
| 45 |
socket_path = None |
| 46 |
transport = 'rest' |
| 47 |
|
| 48 |
if self._play_context.connection == 'network_cli': |
| 49 |
provider = self._task.args.get('provider', {}) |
| 50 |
if any(provider.values()): |
| 51 |
display.warning("'provider' is unnecessary when using 'network_cli' and will be ignored") |
| 52 |
elif self._play_context.connection == 'local': |
| 53 |
provider = load_provider(f5_provider_spec, self._task.args) |
| 54 |
|
| 55 |
|
| 56 |
transport = provider['transport'] or transport |
| 57 |
|
| 58 |
display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) |
| 59 |
|
| 60 |
if transport == 'cli': |
| 61 |
pc = copy.deepcopy(self._play_context) |
| 62 |
pc.connection = 'network_cli' |
| 63 |
pc.network_os = 'bigip' |
| 64 |
pc.remote_addr = provider['server'] or self._play_context.remote_addr |
| 65 |
pc.port = int(provider['server_port'] or self._play_context.port or 22) |
| 66 |
pc.remote_user = provider['user'] or self._play_context.connection_user |
| 67 |
pc.password = provider['password'] or self._play_context.password |
| 68 |
pc.private_key_file = provider.get('ssh_keyfile', None) or self._play_context.private_key_file |
| 69 |
command_timeout = int(provider['timeout'] or C.PERSISTENT_COMMAND_TIMEOUT) |
| 70 |
|
| 71 |
display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) |
| 72 |
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) |
| 73 |
connection.set_options(direct={'persistent_command_timeout': command_timeout}) |
| 74 |
|
| 75 |
socket_path = connection.run() |
| 76 |
display.vvvv('socket_path: %s' % socket_path, pc.remote_addr) |
| 77 |
if not socket_path: |
| 78 |
return { |
| 79 |
'failed': True, |
| 80 |
'msg': 'Unable to open shell. Please see: ' |
| 81 |
'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell' |
| 82 |
} |
| 83 |
|
| 84 |
task_vars['ansible_socket'] = socket_path |
| 85 |
|
| 86 |
if (self._play_context.connection == 'local' and transport == 'cli') or self._play_context.connection == 'network_cli': |
| 87 |
|
| 88 |
|
| 89 |
if socket_path is None: |
| 90 |
socket_path = self._connection.socket_path |
| 91 |
conn = Connection(socket_path) |
| 92 |
out = conn.get_prompt() |
| 93 |
while '(config' in to_text(out, errors='surrogate_then_replace').strip(): |
| 94 |
display.vvvv('wrong context, sending exit to device', self._play_context.remote_addr) |
| 95 |
conn.send_command('exit') |
| 96 |
out = conn.get_prompt() |
| 97 |
|
| 98 |
if self._play_context.connection == 'network_cli': |
| 99 |
p = load_provider(f5_provider_spec, self._task.args) |
| 100 |
p['server'] = task_vars['ansible_host'] |
| 101 |
p['user'] = task_vars['ansible_user'] |
| 102 |
p['password'] = task_vars['ansible_password'] |
| 103 |
task_vars['provider'] = p |
| 104 |
|
| 105 |
result = super(ActionModule, self).run(task_vars=task_vars) |
| 106 |
return result |