cisco.ios.ios_command 模块 – 用于在远程设备上运行命令的模块。

注意

此模块是 cisco.ios 集合 (版本 9.0.3) 的一部分。

如果您使用的是 ansible 软件包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install cisco.ios

要在剧本中使用它,请指定:cisco.ios.ios_command

cisco.ios 1.0.0 中的新增功能

概要

  • 将任意命令发送到 ios 节点并返回从设备读取的结果。此模块包含一个参数,该参数将导致模块在返回之前等待特定条件,或者如果未满足条件则超时。

  • 此模块不支持在配置模式下运行命令。请使用 ios_config 来配置 IOS 设备。

参数

参数

注释

commands

列表 / 元素=任意 / 必需

通过已配置的提供程序发送到远程 ios 设备的命令列表。返回命令的结果输出。如果提供了 *wait_for* 参数,则在满足条件或重试次数过期之前,模块不会返回。如果发送到设备的命令需要回答提示,则可以传递包含 *command*、*answer* 和 *prompt* 的字典。常见的答案是“y”或“\r”(回车符,必须使用双引号)。请参见示例。

interval

整数

配置命令重试之间等待的秒数间隔。如果命令未通过指定的条件,则间隔表示在再次尝试命令之前要等待的时间。

默认值: 1

match

字符串

*match* 参数与 *wait_for* 参数结合使用,用于指定匹配策略。有效值为 allany。如果值为 all,则必须满足 *wait_for* 中的所有条件。如果值为 any,则只需满足其中一个值即可。

选项

  • "any"

  • "all" ← (默认)

retries

整数

指定在命令被认为失败之前应尝试的重试次数。该命令在每次重试时都会在目标设备上运行,并根据 *wait_for* 条件进行评估。

默认值: 9

wait_for

别名:waitfor

列表 / 元素=字符串

要根据命令输出评估的条件列表。任务将等待每个条件都为真,然后才能继续执行。如果在配置的重试次数内条件不为真,则任务失败。请参见示例。

备注

注意

示例

- name: Run show version on remote devices
  cisco.ios.ios_command:
    commands: show version

# output-

# ok: [iosxeappliance] => {
#     "changed": false,
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 "show version"
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": null
#         }
#     },
#     "stdout": [
#         "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software ... register is 0x2102"
#     ],
#     "stdout_lines": [
#         [
#             "Cisco IOS XE Software, Version 17.03.04a",
#             "Cisco IOS Software [Amsterdam], Virtual XE Software",
#             "..."
#             "Configuration register is 0x2102"
#         ]
#     ]
# }

- name: Run show version and check to see if output contains IOS
  cisco.ios.ios_command:
    commands: show version
    wait_for: result[0] contains IOS

# output-

# ok: [iosxeappliance] => {
#     "changed": false,
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 "show version"
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": [
#                 "result[0] contains IOS"
#             ]
#         }
#     },
#     "stdout": [
#         "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software ... register is 0x2102"
#     ],
#     "stdout_lines": [
#         [
#             "Cisco IOS XE Software, Version 17.03.04a",
#             "Cisco IOS Software [Amsterdam], Virtual XE Software",
#             "..."
#             "Configuration register is 0x2102"
#         ]
#     ]
# }

- name: Run multiple commands on remote nodes
  cisco.ios.ios_command:
    commands:
      - show version
      - show interfaces

# output-

# ok: [iosxeappliance] => {
#     "changed": false,
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 "show version",
#                 "show interfaces"
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": null
#         }
#     },
#     "stdout": [
#         "Cisco IOS XE Software, Version 17.03.04a\nCisco IOS Software [Amsterdam], Virtual XE Software Configuration register is 0x2102",
#         "Loopback999 is up, line protocol is up ...failures, 0 output buffers swapped out"
#     ],
#     "stdout_lines": [
#         [
#             "Cisco IOS XE Software, Version 17.03.04a",
#             "Cisco IOS Software [Amsterdam], Virtual XE Software",
#             "..."
#             "Configuration register is 0x2102"
#         ],
#         [
#             "Loopback999 is up, line protocol is up ",
#             "  Hardware is Loopback",
#             "  Description: this is a test",
#             "  MTU 1514 bytes, BW 8000000 Kbit/sec, DLY 5000 usec, ",
#             "     reliability 255/255, txload 1/255, rxload 1/255",
#             "  Encapsulation LOOPBACK, loopback not set",
#             "  Keepalive set (10 sec)",
#             "  Last input never, output never, output hang never",
#             "  Last clearing of \"show interface\" counters never",
#             "  Input queue: 0/75/0/0 (size/max/drops/flushes); Total output drops: 0",
#             "  Queueing strategy: fifo",
#             "  Output queue: 0/0 (size/max)",
#             "  5 minute input rate 0 bits/sec, 0 packets/sec",
#             "  5 minute output rate 0 bits/sec, 0 packets/sec",
#             "     0 packets input, 0 bytes, 0 no buffer",
#             "     Received 0 broadcasts (0 IP multicasts)",
#             "     0 runts, 0 giants, 0 throttles ",
#             "     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored, 0 abort",
#             "     0 packets output, 0 bytes, 0 underruns",
#             "     Output 0 broadcasts (0 IP multicasts)",
#             "     0 output errors, 0 collisions, 0 interface resets",
#             "     0 unknown protocol drops",
#             "     0 output buffer failures, 0 output buffers swapped out"
#         ]
#     ]
# }

- name: Run multiple commands and evaluate the output
  cisco.ios.ios_command:
    commands:
      - show version
      - show interfaces
    wait_for:
      - result[0] contains IOS
      - result[1] contains Loopback0

# output-
# failed play as result[1] contains Loopback0 is false

# fatal: [iosxeappliance]: FAILED! => {
#     "changed": false,
#     "failed_conditions": [
#         "result[1] contains Loopback0"
#     ],
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 "show version",
#                 "show interfaces"
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": [
#                 "result[0] contains IOS",
#                 "result[1] contains Loopback0"
#             ]
#         }
#     },
#     "msg": "One or more conditional statements have not been satisfied"
# }

- name: Run commands that require answering a prompt
  cisco.ios.ios_command:
    commands:
      - command: "clear counters GigabitEthernet2"
        prompt: 'Clear "show interface" counters on this interface \[confirm\]'
        answer: "y"
      - command: "clear counters GigabitEthernet3"
        prompt: "[confirm]"
        answer: "\r"

# output-

# ok: [iosxeappliance] => {
#     "changed": false,
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 {
#                     "answer": "y",
#                     "check_all": false,
#                     "command": "clear counters GigabitEthernet2",
#                     "newline": true,
#                     "output": null,
#                     "prompt": "Clear \"show interface\" counters on this interface \\[confirm\\]",
#                     "sendonly": false
#                 },
#                 {
#                     "answer": "\r",
#                     "check_all": false,
#                     "command": "clear counters GigabitEthernet3",
#                     "newline": true,
#                     "output": null,
#                     "prompt": "[confirm]",
#                     "sendonly": false
#                 }
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": null
#         }
#     },
#     "stdout": [
#         "Clear \"show interface\" counters on this interface [confirm]y",
#         "Clear \"show interface\" counters on this interface [confirm]"
#     ],
#     "stdout_lines": [
#         [
#             "Clear \"show interface\" counters on this interface [confirm]y"
#         ],
#         [
#             "Clear \"show interface\" counters on this interface [confirm]"
#         ]
#     ]
# }

- name: Run commands with complex values like special characters in variables
  cisco.ios.ios_command:
    commands:
      ["{{ 'test aaa group TEST ' ~ user ~ ' ' ~ password ~ ' new-code' }}"]
  vars:
    user: "dummy"
    password: "!dummy"

# ok: [iosxeappliance] => {
#     "changed": false,
#     "invocation": {
#         "module_args": {
#             "commands": [
#                 "test aaa group group test !dummy new-code"
#             ],
#             "interval": 1,
#             "match": "all",
#             "retries": 10,
#             "wait_for": null
#         }
#     },
#     "stdout": [
#         "User was successfully authenticated."
#     ],
#     "stdout_lines": [
#         [
#             "User was successfully authenticated."
#         ]
#     ]
# }

返回值

常用的返回值已在此处记录 此处,以下是此模块特有的字段

描述

failed_conditions

列表 / 元素=字符串

已失败的条件列表

返回:失败

示例: ["...", "..."]

stdout

列表 / 元素=字符串

命令的响应集

返回:除了低级错误(例如动作插件)之外始终返回

示例: ["...", "..."]

stdout_lines

列表 / 元素=字符串

stdout 的值拆分为列表

返回:除了低级错误(例如动作插件)之外始终返回

示例: [["...", "..."], ["..."], ["..."]]

作者

  • Peter Sprygada (@privateip)