使用 Ansible 检索或比较 Junos OS 配置
总结 使用瞻博网络 Ansible 模块检索或比较 Junos 设备上的配置。
瞻博网络提供了一个 Ansible 模块,可用于管理 Junos 设备上的配置。 表 1 概述了可用于检索或比较 Junos 设备配置的可用模块。
内容集 |
模块名称 |
---|---|
您可以使用该juniper.device.config
模块为本机 Junos OS 配置以及与已添加到设备的第三方 YANG 数据模型对应的配置数据请求完整的配置或选定部分。要从 Junos 设备检索配置,请使用参数retrieve
执行config
模块。模块的响应在 和 键中config
包含文本格式的配置,除非该return_output
选项设置为 false
。config_lines
您还可以将活动配置与之前提交的配置进行比较。
以下各节讨论如何使用该模块来检索或比较配置。
如何为配置数据指定源数据库
使用 juniper.device.config
模块检索配置时,必须在模块的参数列表中包含该 retrieve
参数。该 retrieve
参数指定要从中检索数据的配置数据库。您可以设置为 retrieve
以下值以从相应的数据库返回配置数据:
-
retrieve: 'candidate'
- 从候选配置中检索数据。 -
retrieve: 'committed'
- 从提交的配置中检索数据。
已提交的配置数据库
以下操作指南以文本格式检索清单组中每台设备的完整提交配置:
--- - name: "Get Junos OS configuration" hosts: junos-all connection: local gather_facts: no tasks: - name: "Get committed configuration" juniper.device.config: retrieve: "committed" register: response - name: "Print result" ansible.builtin.debug: var: response
候选配置数据库
以下操作指南以文本格式检索清单组中每台设备的完整候选配置。如果数据库被锁定或修改,则该模块将返回错误。
--- - name: "Get Junos OS configuration" hosts: junos-all connection: local gather_facts: no tasks: - name: "Get candidate configuration" juniper.device.config: retrieve: "candidate" register: response - name: "Print result" ansible.builtin.debug: var: response
如何指定要返回的配置数据的作用域
除了检索完整的 Junos OS 配置外,还可以通过包含 config
模块的 filter
参数来检索配置的特定部分。 filter
参数的值是一个字符串,其中包含子树筛选器,用于选择要返回的配置语句。子树筛选器返回与选择条件匹配的配置数据。如果请求多个层次结构,则 的 filter
值必须表示从根开始(由 <configuration>
元素表示)到要显示的每个元素的配置层次结构的所有级别。
以下操作指南将在每个设备的已提交配置数据库中检索并打印和[edit protocols]
层次结构级别的配置[edit interfaces]
:
--- - name: "Get Junos OS configuration hierarchies" hosts: dc1 connection: local gather_facts: no tasks: - name: "Get selected configuration hierarchies" juniper.device.config: retrieve: "committed" filter: "<configuration><interfaces/><protocols/></configuration>" register: response - name: "Print result" ansible.builtin.debug: var: response
以下操作指南将检索并打印 ge-1/0/1 接口的配置:
--- - name: "Get Junos OS configuration hierarchies" hosts: dc1 connection: local gather_facts: no tasks: - name: "Get selected configuration hierarchies" juniper.device.config: retrieve: "committed" filter: "<interfaces><interface> <name>ge-1/0/1</name></interface></interfaces>" register: response - name: "Print result" ansible.builtin.debug: var: response
以下操作指南检索并打印在层次结构级别提交的 [edit system services]
配置:
--- - name: "Get Junos OS configuration hierarchies." hosts: dc1 connection: local gather_facts: no tasks: - name: "Get selected configuration hierarchies" juniper.device.config: retrieve: "committed" filter: "system/services" register: response - name: "Print result" ansible.builtin.debug: var: response
如何指定要返回的配置数据的格式
使用模块 juniper.device.config
检索配置时,该模块将调用 Junos XML 协议操作,该操作 <get-configuration>
可以返回不同格式的配置数据。默认情况下,模块以格式化文本形式返回配置数据。文本格式使用换行符、制表符和其他空格、大括号和方括号来指示语句之间的层次结构关系。
要指定返回配置数据的格式,请将模块的format
参数设置为config
所需的格式。可接受的值包括:
-
'json'
—JavaScript 对象标记 (JSON) -
'set'
—Junos OSset
命令 -
'text'
- 格式化文本(默认值) -
'xml'
—Junos XML 元素
在 playbook 输出中 config
,和 config_lines
键包含所需格式的配置。如果请求 Junos XML 或 JSON 格式, config_parsed
则密钥将包含 JSON 格式的等效配置。
以下操作指南以 XML 格式检索清单组中每个设备的完整提交配置:
--- - name: "Get Junos OS configuration." hosts: junos-all connection: local gather_facts: no tasks: - name: "Get configuration in XML format" juniper.device.config: retrieve: "committed" format: "xml" register: response - name: "Print result" ansible.builtin.debug: var: response
如何检索第三方 YANG 数据模型的配置数据
您可以在 Junos 设备上加载标准化或自定义 YANG 模块,以添加 Junos OS 本身不支持但可以通过转换支持的数据模型。您可以使用为候选模型定义的语法在候选配置中配置非本机数据模型。提交配置时,数据模型的转换脚本会转换该数据,并将相应的 Junos OS 配置提交为检出配置中的暂时性更改。
候选配置和活动配置包含非原生 YANG 数据模型的配置数据,其语法由这些模型定义。除了检索本机 Junos OS 配置之外,您还可使用该 juniper.device.config
模块检索标准(IETF、OpenConfig)和自定义 YANG 数据模型的配置数据。默认情况下,第三方 YANG 数据模型的配置数据不包含在模块的回复中。
要在检索 Junos OS 配置之余,要检索由非原生 YANG 数据模型定义的配置数据,请使用 model
该参数执行模块,并在适当的时候包含该 namespace
参数。该 model
参数采用以下值之一:
-
custom
- 检索由自定义 YANG 数据模型定义的配置数据。在为自定义 YANG 数据模型检索数据时,必须包含该namespace
参数。 -
ietf
- 检索由 IETF YANG 数据模型定义的配置数据。 -
openconfig
—检索由 OpenConfig YANG 数据模型定义的配置数据。 -
True
- 检索所有配置数据,包括完整的 Junos OS 配置和来自任何 YANG 数据模型的数据。
model
如果参数指定或ietf
openconfig
,则模块会自动使用相应的命名空间。如果指定model: "custom"
为自定义 YANG 数据模型检索数据,则还必须包含具有相应命名空间的namespace
参数。
如果包含model
值custom
为 、 ietf
或 openconfig
的参数,并且还包含filter
返回特定 XML 子树的参数,则 Junos OS 仅返回来自非原生数据模型的匹配层次结构。如果 Junos OS 配置包含同名的层次结构(例如“interfaces”),则该层次结构不会包含在回复中。使用 model: "True"
时,不支持该filter
选项。
使用 config
模块检索非本机配置数据时,仅当还包括 filter
该参数时,才能指定返回数据的格式。如果省略该 filter
参数,则必须指定 format: "xml"
。
以下操作指南从提交的配置中检索 OpenConfig interfaces
配置层次结构。如果省略该 filter
参数,RPC 将返回完整的 Junos OS 和 OpenConfig 配置。
--- - name: "Retrieve OpenConfig configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Retrieve the OpenConfig interfaces configuration" juniper.device.config: retrieve: "committed" model: "openconfig" filter: "interfaces" format: "xml" register: response - name: "Print result" ansible.builtin.debug: var: response
以下任务从具有给定命名空间的自定义 YANG 数据模型的已提交配置中检索 l2vpn
配置层次结构:
tasks: - name: "Retrieve custom configuration" juniper.device.config: retrieve: "committed" model: "custom" filter: "l2vpn" remove_ns: False namespace: "http://yang.juniper.net/customyang/l2vpn" format: "xml" register: response
以下任务将检索完整的 Junos OS 提交的配置以及已添加到设备的其他 YANG 数据模型的配置数据:
tasks: - name: "Retrieve Junos OS and all third-party configuration data" juniper.device.config: retrieve: "committed" model: "True" format: "xml" register: response
如何指定没有等效模块参数的选项
使用模块 juniper.device.config
检索配置时,该模块将调用 Junos XML 协议 <get-configuration>
操作。该模块支持许多 <get-configuration>
属性的显式参数,例如属性 format
。该模块还支持该 options
参数,该参数使您能够包含没有等效模块参数的任何其他 <get-configuration>
属性。该 options
参数采用操作支持 <get-configuration>
的任何属性的键/值对的字典。
有关 Junos XML 协议 <get-configuration>
操作支持的属性的完整列表,请参阅 <get-configuration>。
例如,该 config
模块从继承前的配置中检索数据,其中 <groups>
、 <apply-groups>
、 <apply-groups-except>
和 <interface-range>
标记是配置输出中的独立元素。要从继承后配置中检索数据,该配置将从用户定义的组和范围继承的语句显示为继承语句的子级,可以将参数包含在 options
内 inherit: "inherit"
。
以下操作指南从继承后提交的配置中检索层次结构级别的配置 [edit system services]
数据。在这种情况下,如果配置还包含在层次结构级别上配置 [edit groups global system services]
的语句,则这些语句将在继承后配置中继承 [edit system services]
,并在检索到的配置数据中返回。
--- - name: "Get Junos OS configuration hierarchies" hosts: dc1 connection: local gather_facts: no tasks: - name: "Get selected hierarchy from the post-inheritance configuration" juniper.device.config: retrieve: "committed" filter: "system/services" options: inherit: "inherit" register: response - name: "Print result" ansible.builtin.debug: var: response
如何将配置数据保存到文件
使用 juniper.device.config
模块检索配置时,可以通过添加模块的 dest_dir
或 dest
参数,将返回的配置数据保存在本地 Ansible 控制节点上的文件中。该 dest_dir
选项仅指定目录,该 dest
选项可以指定路径和文件名。如果已存在具有目标名称的输出文件,则模块将覆盖该文件。
要在本地 Ansible 控制节点上指定保存检索到的配置的目录,请包含 dest_dir
该参数,并定义目标目录的路径。每个设备的配置都存储在名为 hostname.config 的单独文件中。
以下操作指南从清单组中的所有设备中检索提交的配置。playbook 将每个设备配置保存到 Ansible 控制节点上 playbook 目录下的 configs 目录中的单独文件中。
--- - name: "Get Junos OS configuration" hosts: junos-all connection: local gather_facts: no tasks: - name: "Save configuration to a file" juniper.device.config: retrieve: "committed" dest_dir: "{{ playbook_dir }}/configs"
若要指定输出文件的路径和文件名,请包含 dest
参数,并定义文件的绝对路径或相对路径。如果包含 dest
参数,但省略目录,则文件将保存在 playbook 目录中。如果检索多个设备的配置,则参数 dest
必须包含一个变量,例如 {{ inventory_hostname }}
用于区分每个设备的文件名。如果不区分文件名,则每个设备的配置文件将覆盖其他设备的配置文件。
以下操作指南从 [edit system services]
清单组中所有设备上的已提交配置数据库中检索层次结构。playbook 会将每个设备配置保存到 Ansible 控制节点上 playbook 目录中的单独文件中。每个文件都由设备主机名进行唯一标识。
--- - name: "Get Junos OS configuration" hosts: junos-all connection: local gather_facts: no tasks: - name: "Get selected configuration hierarchies and save to file" juniper.device.config: retrieve: "committed" filter: "system/services" dest: "{{ inventory_hostname }}-system-services-config"
如果要将配置数据保存到文件中,并且不希望在模块的响应中复制配置数据,则可以选择将其包含在return_output: false
模块的参数列表中。设置为return_output
false
会导致模块在其响应中省略 config
、 config_lines
和 config_parsed
键。如果设备返回大量配置数据,则可能需要执行此操作。
如何将活动配置与之前的配置进行比较
您可以通过该 juniper.device.config
模块将活动配置与之前提交的配置或回滚配置进行比较。要将活动配置与之前的配置进行比较,请包含以下模块参数:
juniper.device.config: diff: true rollback: id check: false commit: false
默认情况下,当您包含 rollback: id
参数时,模块将回滚配置,执行提交检查,并提交更改。您必须包含参数 commit: false
才能仅比较配置并阻止模块加载和提交回滚配置。包含参数 check: false
可防止不必要的提交检查操作。
该模块返回和 diff
diff_lines
键。密钥以 差异 或修补程序格式包含活动配置与先前配置之间的配置差异。
-
diff
— 包含名为prepared
的单个键及其值的字典,该键是包含差异的单个多行字符串。 -
diff_lines
- 包含差异的单行字符串列表。
要将差异保存到本地 Ansible 控制节点上的文件中,请包含参数 diffs_file
,并定义输出文件的绝对路径或相对路径。如果包含 diffs_file
参数但省略目录,则文件将保存在 playbook 目录中。如果比较多个设备上的配置,则参数 diffs_file
必须包含一个变量,例如 {{ inventory_hostname }}
用于区分每个设备的文件名。如果不区分文件名,则每个设备的输出文件将覆盖其他设备的输出文件。
以下操作指南会提示您输入以前提交的配置的回滚 ID。然后,操作指南会将提交的配置与指定的回滚配置进行比较,将比较保存到唯一命名的文件中,并打印对标准输出的响应。
--- - name: "Compare configurations" hosts: dc1 connection: local gather_facts: no vars_prompt: - name: "ROLLBACK" prompt: "Rollback ID to compare with active configuration" private: no tasks: - name: "Compare active to previous configuration" juniper.device.config: diff: true rollback: "{{ ROLLBACK }}" check: false commit: false diffs_file: "{{ inventory_hostname }}-diff-rollback-{{ ROLLBACK }}" register: response - name: "Print diff" ansible.builtin.debug: var: response
user@ansible-cn:~$ ansible-playbook configuration-compare-to-rollback.yaml Rollback ID to compare with active configuration: 2 PLAY [Compare configurations] ************************************************* TASK [Compare active to previous configuration] ****************************** changed: [dc1a.example.net] TASK [Print diff] ************************************************************ ok: [dc1a.example.net] => { "response": { "changed": true, "diff": { "prepared": "\n[edit system services]\n- netconf {\n- ssh;\n- }\n" }, "diff_lines": [ "", "[edit system services]", "- netconf {", "- ssh;", "- }" ], "failed": false, "msg": "Configuration has been: opened, rolled back, diffed, closed." } } PLAY RECAP ******************************************************************** dc1a.example.net : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
user@ansible-cn:~$ cat dc1a.example.net-diff-rollback-2 [edit system services] - netconf { - ssh; - }