| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
DOCUMENTATION = """ |
| 24 |
module: ios_command |
| 25 |
author: Peter Sprygada (@privateip) |
| 26 |
short_description: Module to run commands on remote devices. |
| 27 |
description: |
| 28 |
- Sends arbitrary commands to an ios node and returns the results read from the device. |
| 29 |
This module includes an argument that will cause the module to wait for a specific |
| 30 |
condition before returning or timing out if the condition is not met. |
| 31 |
- This module does not support running commands in configuration mode. Please use |
| 32 |
L(ios_config,https://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_config_module.html#ansible-collections-cisco-ios-ios-config-module) |
| 33 |
to configure IOS devices. |
| 34 |
version_added: 1.0.0 |
| 35 |
extends_documentation_fragment: |
| 36 |
- cisco.ios.ios |
| 37 |
notes: |
| 38 |
- Tested against Cisco IOSXE Version 17.3 on CML. |
| 39 |
- This module works with connection C(network_cli). |
| 40 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 41 |
options: |
| 42 |
commands: |
| 43 |
description: |
| 44 |
- List of commands to send to the remote ios device over the configured provider. |
| 45 |
The resulting output from the command is returned. If the I(wait_for) argument |
| 46 |
is provided, the module is not returned until the condition is satisfied or |
| 47 |
the number of retries has expired. If a command sent to the device requires |
| 48 |
answering a prompt, it is possible to pass a dict containing I(command), I(answer) |
| 49 |
and I(prompt). Common answers are 'y' or "\\r" (carriage return, must be double |
| 50 |
quotes). See examples. |
| 51 |
required: true |
| 52 |
type: list |
| 53 |
elements: raw |
| 54 |
wait_for: |
| 55 |
description: |
| 56 |
- List of conditions to evaluate against the output of the command. The task will |
| 57 |
wait for each condition to be true before moving forward. If the conditional |
| 58 |
is not true within the configured number of retries, the task fails. See examples. |
| 59 |
aliases: |
| 60 |
- waitfor |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
match: |
| 64 |
description: |
| 65 |
- The I(match) argument is used in conjunction with the I(wait_for) argument to |
| 66 |
specify the match policy. Valid values are C(all) or C(any). If the value |
| 67 |
is set to C(all) then all conditionals in the wait_for must be satisfied. If |
| 68 |
the value is set to C(any) then only one of the values must be satisfied. |
| 69 |
default: all |
| 70 |
type: str |
| 71 |
choices: |
| 72 |
- any |
| 73 |
- all |
| 74 |
retries: |
| 75 |
description: |
| 76 |
- Specifies the number of retries a command should by tried before it is considered |
| 77 |
failed. The command is run on the target device every retry and evaluated against |
| 78 |
the I(wait_for) conditions. |
| 79 |
default: 9 |
| 80 |
type: int |
| 81 |
interval: |
| 82 |
description: |
| 83 |
- Configures the interval in seconds to wait between retries of the command. If |
| 84 |
the command does not pass the specified conditions, the interval indicates how |
| 85 |
long to wait before trying the command again. |
| 86 |
default: 1 |
| 87 |
type: int |
| 88 |
""" |
| 89 |
|
| 90 |
EXAMPLES = r""" |
| 91 |
- name: Run show version on remote devices |
| 92 |
cisco.ios.ios_command: |
| 93 |
commands: show version |
| 94 |
|
| 95 |
# output- |
| 96 |
|
| 97 |
# ok: [iosxeappliance] => { |
| 98 |
# "changed": false, |
| 99 |
# "invocation": { |
| 100 |
# "module_args": { |
| 101 |
# "commands": [ |
| 102 |
# "show version" |
| 103 |
# ], |
| 104 |
# "interval": 1, |
| 105 |
# "match": "all", |
| 106 |
# "retries": 10, |
| 107 |
# "wait_for": null |
| 108 |
# } |
| 109 |
# }, |
| 110 |
# "stdout": [ |
| 111 |
# "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software ... register is 0x2102" |
| 112 |
# ], |
| 113 |
# "stdout_lines": [ |
| 114 |
# [ |
| 115 |
# "Cisco IOS XE Software, Version 17.03.04a", |
| 116 |
# "Cisco IOS Software [Amsterdam], Virtual XE Software", |
| 117 |
# "..." |
| 118 |
# "Configuration register is 0x2102" |
| 119 |
# ] |
| 120 |
# ] |
| 121 |
# } |
| 122 |
|
| 123 |
- name: Run show version and check to see if output contains IOS |
| 124 |
cisco.ios.ios_command: |
| 125 |
commands: show version |
| 126 |
wait_for: result[0] contains IOS |
| 127 |
|
| 128 |
# output- |
| 129 |
|
| 130 |
# ok: [iosxeappliance] => { |
| 131 |
# "changed": false, |
| 132 |
# "invocation": { |
| 133 |
# "module_args": { |
| 134 |
# "commands": [ |
| 135 |
# "show version" |
| 136 |
# ], |
| 137 |
# "interval": 1, |
| 138 |
# "match": "all", |
| 139 |
# "retries": 10, |
| 140 |
# "wait_for": [ |
| 141 |
# "result[0] contains IOS" |
| 142 |
# ] |
| 143 |
# } |
| 144 |
# }, |
| 145 |
# "stdout": [ |
| 146 |
# "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software ... register is 0x2102" |
| 147 |
# ], |
| 148 |
# "stdout_lines": [ |
| 149 |
# [ |
| 150 |
# "Cisco IOS XE Software, Version 17.03.04a", |
| 151 |
# "Cisco IOS Software [Amsterdam], Virtual XE Software", |
| 152 |
# "..." |
| 153 |
# "Configuration register is 0x2102" |
| 154 |
# ] |
| 155 |
# ] |
| 156 |
# } |
| 157 |
|
| 158 |
- name: Run multiple commands on remote nodes |
| 159 |
cisco.ios.ios_command: |
| 160 |
commands: |
| 161 |
- show version |
| 162 |
- show interfaces |
| 163 |
|
| 164 |
# output- |
| 165 |
|
| 166 |
# ok: [iosxeappliance] => { |
| 167 |
# "changed": false, |
| 168 |
# "invocation": { |
| 169 |
# "module_args": { |
| 170 |
# "commands": [ |
| 171 |
# "show version", |
| 172 |
# "show interfaces" |
| 173 |
# ], |
| 174 |
# "interval": 1, |
| 175 |
# "match": "all", |
| 176 |
# "retries": 10, |
| 177 |
# "wait_for": null |
| 178 |
# } |
| 179 |
# }, |
| 180 |
# "stdout": [ |
| 181 |
# "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software Configuration register is 0x2102", |
| 182 |
# "Loopback999 is up, line protocol is up ...failures, 0 output buffers swapped out" |
| 183 |
# ], |
| 184 |
# "stdout_lines": [ |
| 185 |
# [ |
| 186 |
# "Cisco IOS XE Software, Version 17.03.04a", |
| 187 |
# "Cisco IOS Software [Amsterdam], Virtual XE Software", |
| 188 |
# "..." |
| 189 |
# "Configuration register is 0x2102" |
| 190 |
# ], |
| 191 |
# [ |
| 192 |
# "Loopback999 is up, line protocol is up ", |
| 193 |
# " Hardware is Loopback", |
| 194 |
# " Description: this is a test", |
| 195 |
# " MTU 1514 bytes, BW 8000000 Kbit/sec, DLY 5000 usec, ", |
| 196 |
# " reliability 255/255, txload 1/255, rxload 1/255", |
| 197 |
# " Encapsulation LOOPBACK, loopback not set", |
| 198 |
# " Keepalive set (10 sec)", |
| 199 |
# " Last input never, output never, output hang never", |
| 200 |
# " Last clearing of \"show interface\" counters never", |
| 201 |
# " Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0", |
| 202 |
# " Queueing strategy: fifo", |
| 203 |
# " Output queue: 0/0 (size/max)", |
| 204 |
# " 5 minute input rate 0 bits/sec, 0 packets/sec", |
| 205 |
# " 5 minute output rate 0 bits/sec, 0 packets/sec", |
| 206 |
# " 0 packets input, 0 bytes, 0 no buffer", |
| 207 |
# " Received 0 broadcasts (0 IP multicasts)", |
| 208 |
# " 0 runts, 0 giants, 0 throttles ", |
| 209 |
# " 0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort", |
| 210 |
# " 0 packets output, 0 bytes, 0 underruns", |
| 211 |
# " Output 0 broadcasts (0 IP multicasts)", |
| 212 |
# " 0 output errors, 0 collisions, 0 interface resets", |
| 213 |
# " 0 unknown protocol drops", |
| 214 |
# " 0 output buffer failures, 0 output buffers swapped out" |
| 215 |
# ] |
| 216 |
# ] |
| 217 |
# } |
| 218 |
|
| 219 |
- name: Run multiple commands and evaluate the output |
| 220 |
cisco.ios.ios_command: |
| 221 |
commands: |
| 222 |
- show version |
| 223 |
- show interfaces |
| 224 |
wait_for: |
| 225 |
- result[0] contains IOS |
| 226 |
- result[1] contains Loopback0 |
| 227 |
|
| 228 |
# output- |
| 229 |
# failed play as result[1] contains Loopback0 is false |
| 230 |
|
| 231 |
# fatal: [iosxeappliance]: FAILED! => { |
| 232 |
# "changed": false, |
| 233 |
# "failed_conditions": [ |
| 234 |
# "result[1] contains Loopback0" |
| 235 |
# ], |
| 236 |
# "invocation": { |
| 237 |
# "module_args": { |
| 238 |
# "commands": [ |
| 239 |
# "show version", |
| 240 |
# "show interfaces" |
| 241 |
# ], |
| 242 |
# "interval": 1, |
| 243 |
# "match": "all", |
| 244 |
# "retries": 10, |
| 245 |
# "wait_for": [ |
| 246 |
# "result[0] contains IOS", |
| 247 |
# "result[1] contains Loopback0" |
| 248 |
# ] |
| 249 |
# } |
| 250 |
# }, |
| 251 |
# "msg": "One or more conditional statements have not been satisfied" |
| 252 |
# } |
| 253 |
|
| 254 |
- name: Run commands that require answering a prompt |
| 255 |
cisco.ios.ios_command: |
| 256 |
commands: |
| 257 |
- command: "clear counters GigabitEthernet2" |
| 258 |
prompt: 'Clear "show interface" counters on this interface \[confirm\]' |
| 259 |
answer: "y" |
| 260 |
- command: "clear counters GigabitEthernet3" |
| 261 |
prompt: "[confirm]" |
| 262 |
answer: "\r" |
| 263 |
|
| 264 |
# output- |
| 265 |
|
| 266 |
# ok: [iosxeappliance] => { |
| 267 |
# "changed": false, |
| 268 |
# "invocation": { |
| 269 |
# "module_args": { |
| 270 |
# "commands": [ |
| 271 |
# { |
| 272 |
# "answer": "y", |
| 273 |
# "check_all": false, |
| 274 |
# "command": "clear counters GigabitEthernet2", |
| 275 |
# "newline": true, |
| 276 |
# "output": null, |
| 277 |
# "prompt": "Clear \"show interface\" counters on this interface \\[confirm\\]", |
| 278 |
# "sendonly": false |
| 279 |
# }, |
| 280 |
# { |
| 281 |
# "answer": "\r", |
| 282 |
# "check_all": false, |
| 283 |
# "command": "clear counters GigabitEthernet3", |
| 284 |
# "newline": true, |
| 285 |
# "output": null, |
| 286 |
# "prompt": "[confirm]", |
| 287 |
# "sendonly": false |
| 288 |
# } |
| 289 |
# ], |
| 290 |
# "interval": 1, |
| 291 |
# "match": "all", |
| 292 |
# "retries": 10, |
| 293 |
# "wait_for": null |
| 294 |
# } |
| 295 |
# }, |
| 296 |
# "stdout": [ |
| 297 |
# "Clear \"show interface\" counters on this interface [confirm]y", |
| 298 |
# "Clear \"show interface\" counters on this interface [confirm]" |
| 299 |
# ], |
| 300 |
# "stdout_lines": [ |
| 301 |
# [ |
| 302 |
# "Clear \"show interface\" counters on this interface [confirm]y" |
| 303 |
# ], |
| 304 |
# [ |
| 305 |
# "Clear \"show interface\" counters on this interface [confirm]" |
| 306 |
# ] |
| 307 |
# ] |
| 308 |
# } |
| 309 |
|
| 310 |
- name: Run commands with complex values like special characters in variables |
| 311 |
cisco.ios.ios_command: |
| 312 |
commands: |
| 313 |
["{{ 'test aaa group TEST ' ~ user ~ ' ' ~ password ~ ' new-code' }}"] |
| 314 |
vars: |
| 315 |
user: "dummy" |
| 316 |
password: "!dummy" |
| 317 |
|
| 318 |
# ok: [iosxeappliance] => { |
| 319 |
# "changed": false, |
| 320 |
# "invocation": { |
| 321 |
# "module_args": { |
| 322 |
# "commands": [ |
| 323 |
# "test aaa group group test !dummy new-code" |
| 324 |
# ], |
| 325 |
# "interval": 1, |
| 326 |
# "match": "all", |
| 327 |
# "retries": 10, |
| 328 |
# "wait_for": null |
| 329 |
# } |
| 330 |
# }, |
| 331 |
# "stdout": [ |
| 332 |
# "User was successfully authenticated." |
| 333 |
# ], |
| 334 |
# "stdout_lines": [ |
| 335 |
# [ |
| 336 |
# "User was successfully authenticated." |
| 337 |
# ] |
| 338 |
# ] |
| 339 |
# } |
| 340 |
""" |
| 341 |
|
| 342 |
RETURN = """ |
| 343 |
stdout: |
| 344 |
description: The set of responses from the commands |
| 345 |
returned: always apart from low level errors (such as action plugin) |
| 346 |
type: list |
| 347 |
sample: ['...', '...'] |
| 348 |
stdout_lines: |
| 349 |
description: The value of stdout split into a list |
| 350 |
returned: always apart from low level errors (such as action plugin) |
| 351 |
type: list |
| 352 |
sample: [['...', '...'], ['...'], ['...']] |
| 353 |
failed_conditions: |
| 354 |
description: The list of conditionals that have failed |
| 355 |
returned: failed |
| 356 |
type: list |
| 357 |
sample: ['...', '...'] |
| 358 |
""" |
| 359 |
import time |
| 360 |
|
| 361 |
from ansible.module_utils._text import to_text |
| 362 |
from ansible.module_utils.basic import AnsibleModule |
| 363 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.parsing import ( |
| 364 |
Conditional, |
| 365 |
) |
| 366 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( |
| 367 |
to_lines, |
| 368 |
transform_commands, |
| 369 |
) |
| 370 |
|
| 371 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.ios import run_commands |
| 372 |
|
| 373 |
|
| 374 |
def parse_commands(module, warnings): |
| 375 |
commands = transform_commands(module) |
| 376 |
if module.check_mode: |
| 377 |
for item in list(commands): |
| 378 |
if not item["command"].startswith("show"): |
| 379 |
warnings.append( |
| 380 |
"Only show commands are supported when using check mode, not executing %s" |
| 381 |
% item["command"], |
| 382 |
) |
| 383 |
commands.remove(item) |
| 384 |
return commands |
| 385 |
|
| 386 |
|
| 387 |
def main(): |
| 388 |
"""main entry point for module execution""" |
| 389 |
argument_spec = dict( |
| 390 |
commands=dict(type="list", elements="raw", required=True), |
| 391 |
wait_for=dict(type="list", elements="str", aliases=["waitfor"]), |
| 392 |
match=dict(default="all", choices=["all", "any"]), |
| 393 |
retries=dict(default=9, type="int"), |
| 394 |
interval=dict(default=1, type="int"), |
| 395 |
) |
| 396 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 397 |
warnings = list() |
| 398 |
result = {"changed": False, "warnings": warnings} |
| 399 |
commands = parse_commands(module, warnings) |
| 400 |
wait_for = module.params["wait_for"] or list() |
| 401 |
conditionals = [] |
| 402 |
try: |
| 403 |
conditionals = [Conditional(c) for c in wait_for] |
| 404 |
except AttributeError as exc: |
| 405 |
module.fail_json(msg=to_text(exc)) |
| 406 |
retries = module.params["retries"] |
| 407 |
interval = module.params["interval"] |
| 408 |
match = module.params["match"] |
| 409 |
while retries >= 0: |
| 410 |
responses = run_commands(module, commands) |
| 411 |
for item in list(conditionals): |
| 412 |
if item(responses): |
| 413 |
if match == "any": |
| 414 |
conditionals = list() |
| 415 |
break |
| 416 |
conditionals.remove(item) |
| 417 |
if not conditionals: |
| 418 |
break |
| 419 |
time.sleep(interval) |
| 420 |
retries -= 1 |
| 421 |
if conditionals: |
| 422 |
failed_conditions = [item.raw for item in conditionals] |
| 423 |
msg = "One or more conditional statements have not been satisfied" |
| 424 |
module.fail_json(msg=msg, failed_conditions=failed_conditions) |
| 425 |
result.update({"stdout": responses, "stdout_lines": list(to_lines(responses))}) |
| 426 |
module.exit_json(**result) |
| 427 |
|
| 428 |
|
| 429 |
if __name__ == "__main__": |
| 430 |
main() |