cisco.nxos.nxos_command 模块 – 在 Cisco NXOS 设备上运行任意命令

注意

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

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

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

要在 playbook 中使用它,请指定:cisco.nxos.nxos_command

cisco.nxos 1.0.0 中的新增功能

概要

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

参数

参数

注释

commands

列表 / 元素=任意 / 必需

要发送到远程 NXOS 设备的命令。返回命令的输出结果。如果提供了 wait_for 参数,则在满足条件或重试次数过期之前,模块不会返回。

commands 参数还接受另一种形式,允许指定要运行的命令和返回的输出格式的复杂值。这可以在每个命令的基础上完成。复杂参数支持关键字 commandoutput,其中 command 是要运行的命令,output 是 ‘text’ 或 ‘json’ 之一。

如果发送到设备的命令需要回答提示,则可以传递一个包含命令、答案和提示的字典。常见的答案是 ‘y’ 或 “\r”(回车符,必须使用双引号)。请参阅示例。

interval

整数

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

默认值: 1

match

字符串

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

选择

  • "any"

  • "all" ← (默认)

retries

整数

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

retries 设置为 0 时,命令只运行一次。

默认值: 9

wait_for

别名:waitfor

列表 / 元素=字符串

指定要从命令的输出中评估的内容以及要应用的条件。此参数将导致任务等待特定的条件为真,然后再继续。如果配置的重试次数未能满足条件,则任务将失败。请参阅示例。

注释

注意

  • 对 Cisco MDS 的有限支持

  • 有关使用 CLI 和 NX-API 的信息,请参阅:ref:`NXOS 平台选项指南 `

  • 有关使用 Ansible 管理网络设备的更多信息,请参阅:ref:`Ansible 网络指南 `

  • 有关使用 Ansible 管理 Cisco 设备的更多信息,请参阅 `Cisco 集成页面 https://ansible.org.cn/integrations/networks/cisco>`_。

示例

- name: run show version on remote devices
  cisco.nxos.nxos_command:
    commands: show version

- name: run show version and check to see if output contains Cisco
  cisco.nxos.nxos_command:
    commands: show version
    wait_for: result[0] contains Cisco

- name: run multiple commands on remote nodes
  cisco.nxos.nxos_command:
    commands:
      - show version
      - show interfaces

- name: run multiple commands and evaluate the output
  cisco.nxos.nxos_command:
    commands:
      - show version
      - show interfaces
    wait_for:
      - result[0] contains Cisco
      - result[1] contains loopback0

- name: run commands and specify the output format
  cisco.nxos.nxos_command:
    commands:
      - command: show version
        output: json

- name: run commands that require answering a prompt
  cisco.nxos.nxos_command:
    commands:
      - configure terminal
      - command: no feature npv
        prompt: Do you want to continue
        answer: y

返回值

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

描述

failed_conditions

列表 / 元素=字符串

已失败的条件列表

返回:失败

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

stdout

列表 / 元素=字符串

来自命令的响应集

返回:始终,除了低级别错误(例如操作插件)

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

stdout_lines

列表 / 元素=字符串

stdout 的值拆分为列表

返回:始终,除了低级别错误(例如操作插件)

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

作者

  • Peter Sprygada (@privateip)