| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: command |
| 12 |
author: "Egor Zaitsev (@heuels)" |
| 13 |
short_description: Run commands on remote devices running MikroTik RouterOS |
| 14 |
description: |
| 15 |
- Sends arbitrary commands to an RouterOS node and returns the results read from the device. This module includes an argument |
| 16 |
that will cause the module to wait for a specific condition before returning or timing out if the condition is not met. |
| 17 |
- The module always indicates a (changed) status. You can use R(the changed_when task property,override_the_changed_result) |
| 18 |
to determine whether a command task actually resulted in a change or not. |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.routeros.attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
details: |
| 25 |
- Before community.routeros 3.0.0, the module claimed to support check mode. It simply executed the command in check |
| 26 |
mode. |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
platform: |
| 30 |
support: full |
| 31 |
platforms: RouterOS |
| 32 |
idempotent: |
| 33 |
support: N/A |
| 34 |
details: |
| 35 |
- Whether the executed command is idempotent depends on the command. |
| 36 |
options: |
| 37 |
commands: |
| 38 |
description: |
| 39 |
- List of commands to send to the remote RouterOS device over the configured provider. The resulting output from the |
| 40 |
command is returned. If the O(wait_for) argument is provided, the module is not returned until the condition is satisfied |
| 41 |
or the number of retries has expired. |
| 42 |
required: true |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
wait_for: |
| 46 |
description: |
| 47 |
- List of conditions to evaluate against the output of the command. The task will wait for each condition to be true |
| 48 |
before moving forward. If the conditional is not true within the configured number of retries, the task fails. See |
| 49 |
examples. |
| 50 |
type: list |
| 51 |
elements: str |
| 52 |
match: |
| 53 |
description: |
| 54 |
- The O(match) argument is used in conjunction with the O(wait_for) argument to specify the match policy. Valid values |
| 55 |
are V(all) or V(any). If the value is set to V(all) then all conditionals in the wait_for must be satisfied. If the |
| 56 |
value is set to V(any) then only one of the values must be satisfied. |
| 57 |
default: all |
| 58 |
choices: ['any', 'all'] |
| 59 |
type: str |
| 60 |
retries: |
| 61 |
description: |
| 62 |
- Specifies the number of retries a command should by tried before it is considered failed. The command is run on the |
| 63 |
target device every retry and evaluated against the O(wait_for) conditions. |
| 64 |
default: 10 |
| 65 |
type: int |
| 66 |
interval: |
| 67 |
description: |
| 68 |
- Configures the interval in seconds to wait between retries of the command. If the command does not pass the specified |
| 69 |
conditions, the interval indicates how long to wait before trying the command again. |
| 70 |
default: 1 |
| 71 |
type: int |
| 72 |
seealso: |
| 73 |
- ref: ansible_collections.community.routeros.docsite.ssh-guide |
| 74 |
description: How to connect to RouterOS devices with SSH. |
| 75 |
- ref: ansible_collections.community.routeros.docsite.quoting |
| 76 |
description: How to quote and unquote commands and arguments. |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
--- |
| 81 |
- name: Run command on remote devices |
| 82 |
community.routeros.command: |
| 83 |
commands: /system routerboard print |
| 84 |
|
| 85 |
- name: Run command and check to see if output contains routeros |
| 86 |
community.routeros.command: |
| 87 |
commands: /system resource print |
| 88 |
wait_for: result[0] contains MikroTik |
| 89 |
|
| 90 |
- name: Run multiple commands on remote nodes |
| 91 |
community.routeros.command: |
| 92 |
commands: |
| 93 |
- /system routerboard print |
| 94 |
- /system identity print |
| 95 |
|
| 96 |
- name: Run multiple commands and evaluate the output |
| 97 |
community.routeros.command: |
| 98 |
commands: |
| 99 |
- /system routerboard print |
| 100 |
- /interface ethernet print |
| 101 |
wait_for: |
| 102 |
- result[0] contains x86 |
| 103 |
- result[1] contains ether1 |
| 104 |
""" |
| 105 |
|
| 106 |
RETURN = r""" |
| 107 |
stdout: |
| 108 |
description: The set of responses from the commands. |
| 109 |
returned: always apart from low level errors (such as action plugin) |
| 110 |
type: list |
| 111 |
sample: ['...', '...'] |
| 112 |
stdout_lines: |
| 113 |
description: The value of stdout split into a list. |
| 114 |
returned: always apart from low level errors (such as action plugin) |
| 115 |
type: list |
| 116 |
sample: [['...', '...'], ['...'], ['...']] |
| 117 |
failed_conditions: |
| 118 |
description: The list of conditionals that have failed. |
| 119 |
returned: failed |
| 120 |
type: list |
| 121 |
sample: ['...', '...'] |
| 122 |
""" |
| 123 |
|
| 124 |
import sys |
| 125 |
import time |
| 126 |
|
| 127 |
from ansible_collections.community.routeros.plugins.module_utils.routeros import run_commands |
| 128 |
from ansible_collections.community.routeros.plugins.module_utils.routeros import routeros_argument_spec |
| 129 |
from ansible.module_utils.basic import AnsibleModule |
| 130 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.parsing import Conditional |
| 131 |
|
| 132 |
if sys.version_info[0] == 2: |
| 133 |
string_types = (basestring,) # noqa: F821, pylint: disable=undefined-variable |
| 134 |
else: |
| 135 |
string_types = (str,) |
| 136 |
|
| 137 |
|
| 138 |
def to_lines(stdout): |
| 139 |
for item in stdout: |
| 140 |
if isinstance(item, string_types): |
| 141 |
item = str(item).split('\n') |
| 142 |
yield item |
| 143 |
|
| 144 |
|
| 145 |
def main(): |
| 146 |
"""main entry point for module execution |
| 147 |
""" |
| 148 |
argument_spec = dict( |
| 149 |
commands=dict(type='list', elements='str', required=True), |
| 150 |
|
| 151 |
wait_for=dict(type='list', elements='str'), |
| 152 |
match=dict(type='str', default='all', choices=['all', 'any']), |
| 153 |
|
| 154 |
retries=dict(default=10, type='int'), |
| 155 |
interval=dict(default=1, type='int') |
| 156 |
) |
| 157 |
|
| 158 |
argument_spec.update(routeros_argument_spec) |
| 159 |
|
| 160 |
module = AnsibleModule(argument_spec=argument_spec, |
| 161 |
supports_check_mode=False) |
| 162 |
|
| 163 |
result = {'changed': False} |
| 164 |
|
| 165 |
wait_for = module.params['wait_for'] or list() |
| 166 |
conditionals = [Conditional(c) for c in wait_for] |
| 167 |
|
| 168 |
retries = module.params['retries'] |
| 169 |
interval = module.params['interval'] |
| 170 |
match = module.params['match'] |
| 171 |
|
| 172 |
while retries > 0: |
| 173 |
responses = run_commands(module, module.params['commands']) |
| 174 |
|
| 175 |
for item in list(conditionals): |
| 176 |
if item(responses): |
| 177 |
if match == 'any': |
| 178 |
conditionals = list() |
| 179 |
break |
| 180 |
conditionals.remove(item) |
| 181 |
|
| 182 |
if not conditionals: |
| 183 |
break |
| 184 |
|
| 185 |
time.sleep(interval) |
| 186 |
retries -= 1 |
| 187 |
|
| 188 |
if conditionals: |
| 189 |
failed_conditions = [item.raw for item in conditionals] |
| 190 |
msg = 'One or more conditional statements have not been satisfied' |
| 191 |
module.fail_json(msg=msg, failed_conditions=failed_conditions) |
| 192 |
|
| 193 |
result.update({ |
| 194 |
'changed': True, |
| 195 |
'stdout': responses, |
| 196 |
'stdout_lines': list(to_lines(responses)) |
| 197 |
}) |
| 198 |
|
| 199 |
module.exit_json(**result) |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == '__main__': |
| 203 |
main() |