使用 the juniper.device.command 和 juniper.device.rpc ansible 模块在 Junos 设备上执行命令和 RPC
您可以使用和 juniper.device.command juniper.device.rpc Ansible 模块在 Junos 设备上执行操作模式命令和 RPC。
瞻博网络提供 Ansible 模块,可用于在运行 Junos OS 的设备和运行 Junos OS Evolved 的设备上执行操作模式命令和远程过程调用 (RPC)。 表 1 概述了模块。如果您已经在使用集合中的 juniper.device 给定模块集,请使用该集的模块。
|
收藏 |
模块集 |
模块名称 |
|---|---|---|
|
|
||
|
|
以下部分讨论如何使用juniper.device.commandjuniper.device.rpc和模块、解析模块响应、指定输出格式以及将输出保存到文件。
为了更轻松地从操作输出中提取目标数据,您还可以将该 juniper.device.table 模块与自定义或预定义的 Junos PyEZ 操作表搭配使用。有关更多信息,请参阅将 Ansible 与 Junos PyEZ 表结合使用,从 Junos 设备检索操作信息。
如何使用模块执行 juniper.device.command 命令
该 juniper.device.command 模块允许您在 Junos 设备上执行操作模式命令。该模块需要一个参数, commands即要在设备上执行的一个或多个 Junos OS 操作模式命令的列表。
以下操作指南会在清单组中的每台设备上执行两个操作模式命令。操作指南以标准输出 (stdout) 显示模块响应。
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and uptime information
juniper.device.command:
commands:
- "show version"
- "show system uptime"
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
有关模块的响应和输出格式的信息,请参阅 了解模块响应 以及如何 指定命令或 RPC 输出的格式。
如何使用模块执行 juniper.device.rpc RPC
Junos XML API 是 Junos OS 配置语句和操作模式命令的 XML 表示形式。它为 Junos OS 配置层次结构中的所有语句以及您在 Junos OS CLI 中发出的许多操作模式命令定义了等效的 XML。每个带有 Junos XML 对应项的操作模式命令都会映射到请求标记元素,必要时还映射到响应标记元素。请求标记用于 NETCONF 或 Junos XML 协议会话中的 RPC,以从 Junos 设备请求信息。服务器使用包含在相应响应标记元素中的 Junos XML 元素返回响应。
该 juniper.device.rpc 模块允许您在 Junos 设备上执行 RPC。该模块需要一个参数, rpcs即要在设备上执行的一个或多个 Junos OS RPC 的列表。
以下操作指南在 get-interface-information 清单组中的每台设备上执行 RPC,并在标准输出中显示模块响应。RPC 等同于操作模式命令 show interfaces 。
---
- name: Execute RPC
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get interface information
juniper.device.rpc:
rpcs: "get-interface-information"
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
有关将 CLI 命令映射到 RPC 请求标记的信息,请参阅 Junos XML API 资源管理器中的 操作标记。
有关模块的响应和输出格式的信息,请参阅 了解模块响应 以及如何 指定命令或 RPC 输出的格式。
该 juniper.device.rpc 模块支持该 kwargs 选项,使您能够为 RPC 指定关键字参数和值。的 kwargs 值可以是:
-
关键字和值的单一词典
-
为多个 RPC 提供参数的字典列表
列表中的 kwargs 项和列表中的 RPC rpcs 之间必须有一对一的对应关系。如果执行多个RPC,并且RPC不需要任何参数,请将相应 kwargs 的列表项设置为等于空字典 {}。如果单个 RPC 参数不需要值,请将其值设置为等于 true。
您必须在 RPC 参数中使用下划线代替连字符,这在某些情况下可能会导致异常或错误。
以下操作指南在清单组中的每台设备上执行指定的 RPC,并在标准输出中显示模块响应。RPC 请求 get-interface-information lo0.0 接口的简洁级别输出。RPC 请求 get-lldp-interface-neighbors ge-0/0/0 接口的信息。 get-software-information RPC 使用空字典来执行 RPC,而无需附加参数。
---
- name: Get Device Information
hosts: dc1a
connection: local
gather_facts: no
tasks:
- name: Get device information
juniper.device.rpc:
rpcs:
- "get-interface-information"
- "get-lldp-interface-neighbors"
- "get-software-information"
kwargs:
- interface_name: "lo0.0"
terse: true
- interface_device: "ge-0/0/0"
- {}
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
了解模块响应
和juniper.device.rpc模块将juniper.device.command来自设备的 RPC 回复存储在模块响应中的几个不同键中。每个键的数据结构如下:
-
stdout— RPC 回复是一个多行字符串。 -
stdout_lines— RPC reply 是单行字符串的列表。 -
parsed_output— RPC 回复被解析为 JavaScript 对象标记 (JSON) 数据结构。仅当数据格式为 XML 或 JSON 时,此键才存在。
如果模块执行单个命令或 RPC,则模块的响应会将返回的键放在顶级级别。如果模块执行多个命令或 RPC,则模块的响应将包含一个 results 键,即字典列表。列表中的每个元素都对应于单个命令或 RPC。它包括为该命令或 RPC 返回的所有密钥。
例如,以下响应对应于执行单个 RPC:
"junos_result": {
"attrs": null,
"changed": false,
"failed": false,
"format": "xml",
"kwargs": null,
"msg": "The RPC executed successfully.",
"parsed_output": {
"interface-information": {
"physical-interface": [
[...output omitted...]
}
},
"rpc": "get-interface-information",
"stdout": "<interface-information style=\"normal\">\n <physical-interface>\n
[...output omitted...] </physical-interface>\n</interface-information>\n"
"stdout_lines": [
"<interface-information style=\"normal\">",
" <physical-interface>",
[...output omitted...]
" </physical-interface>",
"</interface-information>"
]
}
在某些情况下,命令或 RPC 输出可能很广泛,可能需要在模块的响应中抑制输出。要省略模块响应中的输出键,请包含在 return_output: false 该模块的参数列表中。
tasks:
- name: Get interface information
juniper.device.rpc:
rpcs: "get-interface-information"
return_output: false
register: junos_result
如何指定命令或 RPC 输出的格式
和模块将command来自设备的 RPC 回复存储在模块响应中的几个不同键中:stdout、 stdout_lines和 parsed_output。rpcparsed_output仅当命令或 RPC 输出格式为 XML 或 JSON 时,密钥才存在,其中包含分析为 JSON 数据结构的数据。
stdout和stdout_lines键包含为模块定义的默认格式的数据。默认情况下,command模块以文本格式返回命令输出,模块以 rpc XML 格式返回 RPC 输出。
要指定不同的输出格式,请包含 formats 参数,并将值设置为等于所需格式。支持的格式包括:
-
json -
text -
xml
该 formats 参数采用字符串或字符串列表。如果仅指定一种格式,则所有已执行的命令或 RPC 都使用该格式。若要为每个命令或 RPC 指定不同的格式,请将参数设置为 formats 所需格式的列表。格式的数量必须与命令或 RPC 的数量相同。
以下操作指南在清单组中的每台设备上执行两个 RPC。该操作指南请求所有已执行 RPC 的输出的文本格式。
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and system uptime information
juniper.device.rpc:
rpcs:
- "get-software-information"
- "get-system-uptime-information"
formats: text
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
在模块的响应中,和stdoutstdout_lines键包含文本格式的 RPC 回复。
以下操作指南在清单组中的每台设备上执行两个 RPC。该模块以文本格式请求第一个 RPC 的输出,并以 JSON 格式请求第二个 RPC 的输出。
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and system uptime information
juniper.device.rpc:
rpcs:
- "get-software-information"
- "get-system-uptime-information"
formats:
- "text"
- "json"
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
如何将命令或 RPC 输出保存到文件中
使用 or juniper.device.command juniper.device.rpc 模块时,您可以将返回的数据保存到本地 Ansible 控制节点上的文件中。要保存数据,请包含 dest or dest_dir module 参数。该 dest_dir 选项将每个命令或 RPC 的输出保存在设备的单独文件中。该 dest 选项会将所有命令和 RPC 的输出保存在设备的同一文件中。如果已存在具有目标名称的输出文件,则模块将覆盖该文件。
如果将数据保存到文件中,并且不想在模块的响应中复制输出,则可以选择包含在return_output: false模块的参数列表中。设置为return_outputfalse会导致模块省略模块响应中的输出键。如果设备返回大量数据,则可能需要省略密钥。
以下部分概述了如何使用 dest_dir 和 dest 选项。
dest_dir
要在本地 Ansible 控制节点上指定保存检索到的数据的目录,请包含 dest_dir 参数并定义目标目录的路径。该模块将设备上执行的每个命令或 RPC 的输出存储在名为 hostname_name 的单独文件中。format 其中:
-
hostname— 执行命令或 RPC 的设备的主机名。
-
name— 在托管设备上执行的命令或 RPC 的名称。该模块将命令名称中的空格替换为下划线 ( _ )。
-
format- 输出的格式,可以是 json、 text 或 xml。
以下操作指南在清单组中的每台设备上执行两个 RPC。该操作指南会将每个设备的每个 RPC 的输出保存在 Ansible 控制节点上操作指南目录中的单独文件中。
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and uptime information
juniper.device.rpc:
rpcs:
- "get-software-information"
- "get-system-uptime-information"
dest_dir: "{{ playbook_dir }}"
主机 dc1a.example.net 生成的输出文件为:
-
dc1a.example.net_get-software-information.xml
-
dc1a.example.net_get-system-正常运行时间-information.xml
同样,以下操作指南会在清单组中的每台设备上执行等效命令。该操作指南会将每个设备的每个命令的输出保存在 Ansible 控制节点上操作指南目录中的单独文件中:
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and uptime information
juniper.device.command:
commands:
- "show version"
- "show system uptime"
dest_dir: "{{ playbook_dir }}"
主机 dc1a.example.net 生成的输出文件为:
-
dc1a.example.net_show_version.text
-
dc1a.example.net_show_system_正常运行时间.text
目的地
您可以使用该 dest 选项指定将目标节点的所有命令或 RPC 输出保存到本地 Ansible 控制节点上的输出文件。定义文件名或文件的完整路径。如果包含参数, dest 但省略目录,则文件将保存在 playbook 目录中。如果在多个设备上执行命令或 RPC, dest 则参数必须包含一个变量,例如 {{ inventory_hostname }} 用于区分每个设备的文件名。如果不区分文件名,则每个设备的输出文件将覆盖其他设备的输出文件。
以下操作指南在清单组中的每台设备上执行 RPC。该操作指南将所有 RPC 的输出保存在每个设备的单独文件中。该文件将放置在 Ansible 控制节点上的 playbook 目录中。每个文件都由设备主机名唯一标识。
---
- name: Get device information
hosts: dc1
connection: local
gather_facts: no
tasks:
- name: Get software and uptime information
juniper.device.rpc:
rpcs:
- "get-software-information"
- "get-system-uptime-information"
dest: "{{ inventory_hostname }}-system-information.xml"
例如,主机 dc1a.example.net 生成的输出文件是 dc1a.example.net-system-information.xml 的,其中包含设备上执行的所有 RPC 的输出。