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

注意

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

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

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

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

cisco.ios 1.0.0 中的新增功能

概要

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

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

参数

参数

注释

命令

列表 / 元素=任何 / 必需

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

间隔

整数

配置命令重试之间等待的间隔(以秒为单位)。如果命令未通过指定条件,则间隔表示在再次尝试命令之前等待多长时间。

默认值: 1

匹配

字符串

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

选择

  • “any”

  • "all" ← (默认)

重试

整数

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

默认值: 9

wait_for

别名:waitfor

列表 / 元素=字符串

要针对命令输出评估的条件列表。该任务将等待每个条件为 true 后再继续。如果条件在配置的重试次数内不为 true,则任务失败。请参见示例。

说明

注意

示例

- 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)