使用 Ansible 配置运行网络Junos OS
总结使用瞻博网络 Ansible 模块管理运行新设备的Junos OS。
瞻博网络提供 Ansible 模块,使您能够配置运行 Junos OS 的设备。 表 1 概括了可用模块。用于执行配置更改的用户帐户必须具有更改每台设备上配置相关部分的权限。
内容集 |
模块名称 |
---|---|
从 2.0.0 版开始,该模块将合并并替换 Juniper.junos
juniper_junos_config
、 和 junos_commit
junos_get_config
junos_install_config
junos_rollback
模块的功能。
以下部分讨论如何使用模块在运行新设备的设备上修改和提交Junos OS。
模块概述
和 config
juniper_junos_config
模块使您能够对运行新设备的设备上Junos OS:
负载配置数据
提交配置
回滚配置
加载救援配置
要修改配置,模块的参数列表必须包含加载新配置数据的参数,或者包含恢复到救援配置或先前提交配置 load
rollback
的参数。进行配置更改的基本流程是锁定配置、加载配置更改、提交配置以将其处于活动状态,然后解锁配置。
默认情况下,模块使用模式对候选配置数据库进行更改,模式将自动锁定并解锁 config
juniper_junos_config
configure exclusive
候选全局配置。您也可以对候选配置的专用副本进行更改。有关指定配置模式的信息,请参阅 如何指定配置模式。
加载新配置数据时,除了指定配置模式之外,您还可以指定更改的负载操作以及来源和格式。
负载操作 — 负载操作决定如何将配置数据加载到候选配置中。这些功能支持该系列中提供的许多相同的Junos OS CLI。有关详细信息,请参阅 如何指定负载操作。
格式 — 您可使用标准Junos OS支持的格式之一来配置运行网络的设备。您可以提供配置数据或 Jinja2 模板作为文本、Junos XML 元素、Junos OS命令或
set
JSON。有关指定配置数据格式的信息,请参阅 如何指定要加载 的配置数据的格式。配置数据源 — 您可从字符串列表、本地 Ansible 控制节点上的文件、Jinja2 模板或客户端设备中分别包含 、 或 参数来加载配置数据。
lines
src
template
url
有关指定配置数据源的信息,请参阅以下各节:
此外 config
,通过 和 juniper_junos_config
模块,您还可以加载并提交救援配置,或将配置回滚到之前提交的配置。要加载救援配置或先前提交的配置,必须包含 rollback
模块参数。有关详细信息,请参阅以下章节:
修改配置后,您必须提交配置,使其成为设备上的活动配置。默认情况下,和 config
juniper_junos_config
模块将更改提交到配置。要更改此行为或提供附加提交选项,请参阅 如何提交配置。
默认情况下,当 或 模块包含 用于更改配置的 参数时,模块将自动以差异或补丁格式返回模块响应中的 config
juniper_junos_config
load
rollback
配置更改。 差异在 和 diff
diff_lines
变量中返回。为了防止模块计算并返回差异,请将 diff
模块参数设置为 false
。
如何指定配置模式
您可以指定修改候选配置数据库时要使用的配置模式。默认情况下,和 config
juniper_junos_config
模块使用模式对候选配置数据库 configure exclusive
进行更改。配置排他模式可锁定候选全局配置(也称为共享配置数据库),只要模块需要对配置执行请求的配置更改。锁定数据库可以防止其他用户修改或提交对数据库的更改,直到锁定释放。
要指定模式,请 config_mode
使参数包括在模块的参数列表中。支持的模式包括 exclusive
private
和 。两种模式在退出时都丢弃所有未提交更改。
以下指南使用 configure private
模式修改配置:
--- - name: "Configure Device" hosts: dc1 connection: local gather_facts: no tasks: - name: "Configure op script" juniper.device.config: config_mode: "private" load: "set" lines: - "set system scripts op file bgp.slax" register: response - name: "Print the config changes" debug: var: response.diff_lines
user@ansible-cn:~/ansible$ ansible-playbook configure-script.yaml PLAY [Configure Device] ******************************************************* TASK [Configure op script] **************************************************** changed: [dc1a.example.net] TASK [Print the config changes] *********************************************** ok: [dc1a.example.net] => { "response.diff_lines": [ "", "[edit system scripts op]", "+ file bgp.slax;" ] } PLAY RECAP ******************************************************************** dc1a.example.net : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如何指定负载操作
和 config
juniper_junos_config
模块使用应用程序支持的许多相同负载操作支持加载配置Junos OS CLI。通过将参数包括在模块参数列表中,并设置为相应负载操作的值,可指定 load
负载操作。 表 2 汇总了每种负载操作类型所需的参数设置。
负载操作 |
load 参数 |
描述 |
---|---|---|
|
|
将加载的配置与现有配置合并。 |
|
|
将整个配置更换为已加载的配置。 |
|
|
将已加载的配置与现有配置合并,但将现有配置中的语句替换为在已加载配置中指定 |
|
|
从补丁文件加载配置数据。 |
|
|
加载格式的负载 |
|
|
将完整加载的配置与现有配置进行比较。已加载配置中不同的每个配置元素将替换其现有配置中的相应元素。在提交操作期间,只有受更改的配置元素影响的系统进程才能解析新配置。 |
如何指定要加载的配置数据的格式
和 config
juniper_junos_config
模块使您能够使用标准Junos OS支持的格式之一配置运行新网络的设备。您可提供以字符串或文件作为字符串或文件的配置数据。文件可包含配置数据或 Jinja2 模板。在字符串、文件或 Jinja2 模板内提供配置数据时,支持的数据格式包括文本、Junos XML 元素、Junos OS命令和 set
JSON。
从Junos OS版本16.1R1,运行 Junos OS设备支持以 JSON 格式加载配置数据。
config
和 juniper_junos_config
模块尝试自动检测使用参数以字符串形式提供的配置 lines
数据格式。但是,可以通过包含参数来显式指定字符串 format
的格式。在文件或 Jinja2 模板中提供配置数据时,必须通过向文件添加相应的扩展名或包括参数来指定 format
数据格式。
表 3 汇总了配置数据支持的格式以及文件扩展名和参数的相应 format
值。如果包括 format
该参数,它将替代字符串的自动检测格式以及文件扩展名指示的格式。
配置数据格式 |
文件扩展名 |
格式参数 |
---|---|---|
CLI配置语句(文本) |
.conf |
" |
JavaScript 对象表示法 (JSON) |
.json |
" |
Junos OS |
.set |
" |
Junos XML 元素 |
.xml |
" |
将模块的 参数设置为 load
或 'override'
'update'
时,不能使用 Junos OS set
格式。
如何以字符串加载配置数据
和 config
juniper_junos_config
模块使您能够从字符串列表加载配置数据。要以字符串方式加载配置数据,请包含相应的 load
参数和 lines
参数。该 lines
参数接受要加载的配置数据字符串列表。
这些模块尝试自动检测配置 lines
数据的格式。但是,可以通过包含该参数来明确指定 format
格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。如果在模块的参数列表中包含参数,它将 format
替代自动检测格式。
以下指南配置和提交两个操作脚本。在这种情况下,参数具有值,因为 中的配置数据使用 Junos OS load
'set'
lines
set
语句格式。
--- - name: "Load and commit configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load configuration data using strings and commit" juniper.device.config: load: "set" lines: - "set system scripts op file bgp.slax" - "set system scripts op file bgp-neighbor.slax" register: response - name: "Print the response" debug: var: response
以下指南使用以文本格式配置 lines
数据配置相同的语句。在这种情况下, load: "merge"
则使用 。
--- - name: "Load and commit configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load configuration data using strings and commit" juniper.device.config: load: "merge" lines: - | system { scripts { op { file bgp.slax; file bgp-neighbor.slax; } } } register: response - name: "Print the response" debug: var: response
如何从本地或远程文件加载配置数据
和 config
juniper_junos_config
模块使您能够从文件加载配置数据。该文件可位于以下位置之一:
Ansible 控制节点
客户端设备
从客户端设备可到达的 URL
从文件加载配置文件时,您必须指示文件中配置数据的格式和文件的位置。支持的配置数据格式包括文本、Junos XML 元素、Junos OS set
命令和 JSON。有关加载包含 Jinja2 模板的文件的信息,请参阅 如何使用 Jinja2 模板加载配置数据。
可以通过在模块的参数列表中显式包含参数或向配置文件添加相应的扩展来指定 format
配置数据的格式。如果指定 format
参数,它将替代文件扩展名指示的格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。当配置数据使用 Junos XML 格式时,您必须将数据括在顶级标记 <configuration>
中。
直接在 set
<configuration-text>
<configuration-set>
NETCONF 会话内配置设备时,不需要根据需要在 中封装格式化为 ASCII 文本、Junos OS 命令或 JSON 或 标记的配置数据 <configuration-json>
。
表 4 概括了您可以包括的模块参数,以指定文件的位置。
模块参数 |
描述 |
---|---|
|
到 Ansible 控制节点上的文件的绝对或相对路径。默认目录是指南目录。 |
|
到客户端设备上、FTP 位置或超文本传输协议 (HTTP) URL 上的文件的绝对或相对路径。 客户端设备上的默认目录是当前工作目录,默认为用户主目录。 |
要从 Ansible 控制节点上的本地文件加载配置数据,请将参数设置为包含配置数据的文件的绝对或 src
相对路径。例如:
--- - name: "Load and commit configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load configuration from a local file and commit" juniper.device.config: load: "merge" src: "build_conf/{{ inventory_hostname }}/junos.conf" register: response - name: "Print the response" debug: var: response
要从运行 Junos OS 的受管理设备上,或者从 FTP 或 HTTP URL 加载配置文件中的配置数据,请使用 参数并指定要加载的配置数据 url
的文件路径。例如:
--- - name: "Load and commit configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load configuration from a remote file and commit" juniper.device.config: load: "merge" url: "/var/tmp/junos.conf" register: response - name: "Print the response" debug: var: response
的值可以是 url
绝对或相对本地文件路径、FTP 位置或超文本传输协议 (HTTP) URL。
本地文件名可以具有以下形式之一:
path / filename /—安装文件系统上的文件,在本地闪存盘上或硬盘上。
a: filename 或a: path / filename —本地驱动器上的文件。默认路径为/ (根级别目录)。可移动介质可以是 MS-DOS 或 UNIX (UFS) 格式。
FTP 服务器上文件的文件名格式如下:
ftp://username:password@hostname/path/filename
HTTP 服务器上文件的文件名格式如下:
http://username:password@hostname/path/filename
在每种情况下,该变量的默认值 path 都是用户的主目录。要指定绝对路径,应用程序使用字符%2F 开始该路径;例如, ftp:// username : password @ hostname /%2F path / filename 。
如何使用 Jinja2 模板加载配置数据
和模块使您能够从 Ansible 控制节点上的 Jinja2 模板文件呈现配置数据,在运行 Junos OS 的设备上加载并提交配置。 Jinja 是 Python 的模板引擎,可让您从预定义的模板生成文档。 config
juniper_junos_config
模板是所需语言中的文本文件,通过使用表达式和变量提供了灵活性。您可以使用支持Junos OS格式的 Jinja2 模板创建多个配置数据,这些配置格式包括 ASCII 文本、Junos XML 元素、Junos OS 命令和 set
JSON。Ansible 模块使用 Jinja2 模板和提供的变量库来呈现配置数据。
要使用 Jinja2 模板加载和提交配置数据,请包括 模块参数列表 template
vars
中的 和 参数。
template
—Jinja2 模板文件的路径vars
—呈现 Jinja2 模板所需的密钥和值库
如果模板的文件扩展名未指示数据的格式, format
则还必须包括参数。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。
例如 ,interfaces-mpls.j2 文件包含以下 Jinja2 模板:
interfaces { {% for item in interfaces %} {{ item }} { description "{{ description }}"; unit 0 { family {{ family }}; } } {% endfor %} } protocols { mpls { {% for item in interfaces %} interface {{ item }}; {% endfor %} } rsvp { {% for item in interfaces %} interface {{ item }}; {% endfor %} } }
要使用 config
或 juniper_junos_config
模块加载 Jinja2 模板,请将参数设置为模板文件的路径,并定义该模板在字典中所需的 template
vars
变量。以下指南使用 Jinja2 模板以及 中定义的变量呈现配置数据和负载,然后提交 vars
到目标主机上。 format
参数指示模板文件中配置数据的格式。
--- - name: "Load and commit configuration" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load a configuration from a Jinja2 template and commit" juniper.device.config: load: "merge" template: "build_conf/templates/interfaces-mpls.j2" format: "text" vars: interfaces: ["ge-1/0/1", "ge-1/0/2", "ge-1/0/3"] description: "MPLS interface" family: "mpls" register: response - name: "Print the response" debug: var: response
该模块会生成以下配置数据,此数据将加载到设备的候选配置中并提交:
interfaces { ge-1/0/1 { description "MPLS interface"; unit 0 { family mpls; } } ge-1/0/2 { description "MPLS interface"; unit 0 { family mpls; } } ge-1/0/3 { description "MPLS interface"; unit 0 { family mpls; } } } protocols { mpls { interface ge-1/0/1; interface ge-1/0/2; interface ge-1/0/3; } rsvp { interface ge-1/0/1; interface ge-1/0/2; interface ge-1/0/3; } }
如何加载救援配置
救援配置允许您定义已知工作配置或具有随时可恢复的已知状态的配置。如果您需要恢复到已知配置,或在设备配置和备份配置文件损坏超出维修范围时作为最后手段,您可以使用救援配置。创建救援配置时,设备会将最近提交的配置保存为救援配置。
通过 config
和 juniper_junos_config
模块,可恢复为运行新设备的现有Junos OS。要加载设备并提交救援配置,请包括该模块 rollback: "rescue"
的论断。例如:
--- - name: "Revert to rescue configuration" hosts: dc1a connection: local gather_facts: no tasks: - name: "Load and commit rescue configuration" juniper.device.config: rollback: "rescue" register: response - name: "Print response" debug: var: response
如何回滚配置
运行 Junos OS的设备将存储最近提交的配置和最多 49 个先前配置的副本,具体取决于平台。您可以将回滚到任何存储的配置。如果配置更改导致不必要的结果,并且您想要恢复到已知工作配置,则这会非常有用。回滚配置的过程类似于在设备上执行配置更改的过程,但是您执行回滚(而不是加载配置数据)将整个候选配置替换为之前提交的配置。
通过 config
和 juniper_junos_config
模块,可回滚到之前在运行新设备的Junos OS。要回滚配置并提交,请包括模块 rollback
的参数,并指定回滚配置的 ID。有效 ID 值是 0(对于最近提交的配置,为零)至比之前存储的配置数少一个(最大为 49)。
以下指南提示配置回滚 ID 以恢复、回滚配置并提交,然后打印到标准输出的配置更改。
--- - name: "Roll back the configuration" hosts: dc1a connection: local gather_facts: no vars_prompt: - name: "ROLLBACK" prompt: "Rollback ID of the configuration to restore" private: no tasks: - name: "Roll back the configuration and commit" juniper.device.config: rollback: "{{ ROLLBACK }}" register: response - name: "Print the configuration changes" debug: var: response.diff_lines
user@ansible-cn:~/ansible$ ansible-playbook configuration-rollback.yaml Rollback ID of the configuration to restore: 1 PLAY [Roll back the configuration] ******************************************** TASK [Roll back the configuration and commit] ********************************* changed: [dc1a.example.net] TASK [Print the configuration changes] *************************************** ok: [dc1a.example.net] => { "response.diff_lines": [ "", "[edit interfaces]", "- ge-0/0/0 {", "- unit 0 {", "- family mpls;", "- }", "- }" ] } PLAY RECAP ******************************************************************** dc1a.example.net : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
如何提交配置
默认情况下,当您使用 或 参数使用 或 参数修改配置时,模块将自动 config
juniper_junos_config
执行提交检查并 load
rollback
提交更改。要阻止模块执行提交检查或提交更改,请分别将 check
或 commit
参数 false
设置为 。
您还可以使用该配置中提供的许多相同选项自定义提交Junos OS CLI。 表 5 概括了可用于指定不同提交选项的模块参数。
模块参数 |
描述 |
默认值和 |
---|---|---|
|
执行提交检查或确认之前确认的提交操作。 |
|
|
在提交检查和提交操作之间等待指定的秒数。 |
– |
|
在系统日志文件以及设备的提交历史记录中记录该提交操作的评论。 |
– |
|
提交配置更改或确认之前确认的提交操作。 |
|
|
提交配置更改,即使候选配置和承诺的配置之间没有差异。 |
|
|
要求在初始提交后指定的时间内确认提交操作。否则,请回滚到之前提交的配置。 选项 |
– |
提交配置时,您可以添加一个简短的注释,用来描述提交更改的目的。要记录描述更改的注释,请 comment: "comment string"
用消息字符串包括该参数。
默认情况下,和 config
juniper_junos_config
模块同时执行提交检查和提交操作。该 check_commit_wait
参数定义了在提交检查和提交操作之间等待的秒数。需要为设备提供足够的时间来完成提交检查操作,并启动提交操作之前释放配置锁时,请包括此参数。如果您省略了此参数,则在某些情况下,设备会在提交检查操作释放其配置锁定之前启动提交操作,从而导致提交操作 CommitError
失败。
默认情况下,如果候选配置和已提交配置之间没有差异,模块不会提交更改。要强制提交操作(即使不存在差异),请包括 commit_empty_changes: true
参数。
要要求在初始提交后指定的时间内确认提交操作,请包括 confirmed: minutes
该参数。如果提交未在给定时间限制内确认,配置将自动回滚至先前提交的配置。允许的范围是 1 到 65,535 分钟。确认的提交操作有助于验证配置更改是否正常工作,并且不会阻止对设备的管理访问。如果更改阻止访问或导致其他错误,自动回滚到上一配置将允许在回滚最后期限后访问设备。要确认提交操作,请通过 config
或 juniper_junos_config
参数调用 check: true
或 commit: true
模块。
在下面的指南中,第一项任务修改配置,在提交检查和提交操作之间等待 10 秒,并要求在 5 分钟内确认提交操作。还会记录有关提交的评论。第二项任务会 commit check
执行操作以确认提交。在真实情景中,您可能会在初始提交后执行验证任务,仅在任务通过某些验证标准时执行提交确认。
--- - name: "Load configuration and confirm within 5 minutes" hosts: dc1 connection: local gather_facts: no tasks: - name: "Load configuration. Wait 10 seconds between check and commit. Confirm within 5 min." juniper.device.config: load: "merge" format: "text" src: "build_conf/{{ inventory_hostname }}/junos.conf" check_commit_wait: 10 confirmed: 5 comment: "updated using Ansible" register: response - name: "Print the response" debug: var: response - name: "Confirm the commit with a commit check" juniper.device.config: check: true diff: false commit: false register: response - name: "Print the response" debug: var: response
配置设备时如何忽略警告
和 config
juniper_junos_config
模块使您能够在运行 Junos OS 的设备上修改和Junos OS。在某些情况下,RPC 回复可能包含严重性级别或更高的元素,这些元素会导致模块引发异常,从而导致负载或 <rpc-error>
RpcError
提交操作失败。
在某些情况下,可能需要抑制为响应有关负载和提交操作警告而提出的 RpcError
例外情况。您可指示 和 模块抑制为警告而提出的例外情况,方法为将参数包括在 config
juniper_junos_config
RpcError
ignore_warning
模块参数列表中。该 ignore_warning
参数接受布尔值、字符串或字符串列表。
要指示模块忽略有关模块执行的负载和提交操作的所有警告,请包括 ignore_warning: true
该参数。以下示例将忽略有关负载和提交操作的所有警告。
--- - name: Configure Device hosts: dc1 connection: local gather_facts: no tasks: - name: Configure op script juniper.device.config: config_mode: "private" load: "set" lines: - "set system scripts op file bgp.slax" ignore_warning: true register: response - name: Print the response debug: var: response
如果包括 且所有元素都有严重性级别的警告,应用程序将忽略所有警告 ignore_warning: true
<rpc-error>
,并且不会引发 RpcError
例外。但是, <rpc-error>
严重性级别更高的任何元素仍将引发例外情况。
要指示模块忽略特定警告,请将其参数设置为要忽略的字符串 ignore_warning
或字符串列表。以下示例将忽略两个特定警告:
--- - name: Configure Device hosts: dc1 connection: local gather_facts: no tasks: - name: Configure device running Junos OS and ignore warnings juniper.device.config: config_mode: "private" load: "merge" src: "build_conf/{{ inventory_hostname }}/junos.conf" ignore_warning: - "Advertisement-interval is less than four times" - "Chassis configuration for network services has been changed." register: response - name: Print the response debug: var: response
如果所有元素都有严重性级别的警告,并且响应中的每个警告与一个或多个指定的字符串匹配,则模块 RpcError
<rpc-error>
抑制例外。
示例:使用 Ansible 配置运行网络Junos OS
config
您可通过该模块管理运行新设备的Junos OS。此示例使用此模块对通过 SSH 上的 NETCONF 运行 Junos OS config
的配置更改。
要求
此示例具有以下硬件和软件组件:
运行 Ansible 2.10 或更高版本(已安装
juniper.device
集合)的配置管理服务器在Junos OS启用 NETCONF 且配置了具有相应权限的用户帐户的设备上
为 Ansible 控制器和运行新设备的合适用户配置的 SSH 公共/私钥Junos OS
定义所需主机的现有 Ansible 清单文件
概述
此示例介绍了 Ansible 指南,该指南使用该模块在运行新代码的目标设备的配置中启用 config
新的操作Junos OS。配置文件 junos-config.conf包含格式化为文本的相关配置数据。
指南中包含该任务,该任务利用 Ansible 模块尝试使用 NETCONF 默认端口 Checking NETCONF connectivity
wait_for
(830) 与目标设备建立 NETCONF 会话。如果控制节点在执行指南过程中无法与目标设备建立 NETCONF 会话,则跳过该设备的其余任务。
配置设备的任务在 NETCONF 检查成功后执行 config
该模块。模块 load: "merge"
参数使用操作将新配置数据加载至候选 load merge
配置。默认情况下, config
该模块在用于 和 的设备上提交 load
配置 rollback
数据。模块参数包括参数,该参数记录设备系统日志文件中的提交注释 comment
和提交历史记录。
配置
创建配置文件
逐步过程
要创建模块使用的配置文件:
根据配置数据的格式创建具有相应扩展名的新文件,此示例为文本。
在文件中包括所需的配置更改。
user@ansible-cn:~/ansible$ cat build_conf/dc1a.example.net/junos-config.conf system { scripts { op { file bgp.slax; } } }
创建 Ansible 指南
逐步过程
要创建使用模块对运行以下设备的配置更改 config
的指南Junos OS:
包括指南样板,该面板在本地执行模块。
--- - name: Load and commit configuration data on a device running Junos OS hosts: dc1 connection: local gather_facts: no
(可选)创建验证 NETCONF 连接的任务。
tasks: - name: Checking NETCONF connectivity wait_for: host: "{{ inventory_hostname }}" port: 830 timeout: 5
创建将配置加载到设备上并提交它的任务。
- name: Merge configuration data from a file and commit juniper.device.config: load: "merge" src: "build_conf/{{ inventory_hostname }}/junos-config.conf" comment: "Configuring op script with Ansible" register: response
(可选)创建一个打印响应的任务,其中包含 差异格式的配置 更改。
- name: Print the response debug: var: response
结果
在 Ansible 控制节点上,查看完成的指南。如果指南未显示预期的代码,请重复此示例中的说明,以更正指南。
--- - name: Load and commit configuration data on a device running Junos OS hosts: dc1 connection: local gather_facts: no tasks: - name: Checking NETCONF connectivity wait_for: host: "{{ inventory_hostname }}" port: 830 timeout: 5 - name: Merge configuration data from a file and commit juniper.device.config: load: "merge" src: "build_conf/{{ inventory_hostname }}/junos-config.conf" comment: "Configuring op script with Ansible" register: response - name: Print the response debug: var: response
执行指南
要执行指南:
-
在
ansible-playbook
控制节点上发出 命令,并提供操作指南路径和任何所需选项。user@ansible-cn:~/ansible$ ansible-playbook ansible-pb-junos-config.yaml PLAY [Load and commit configuration data on a device running Junos OS] **** TASK [Checking NETCONF connectivity] ************************************** ok: [dc1a.example.net] TASK [Merge configuration data from a file and commit] ******************** changed: [dc1a.example.net] TASK [Print the response] ************************************************* ok: [dc1a.example.net] => { "response": { "changed": true, "diff": { "prepared": "\n[edit system scripts op]\n+ file bgp.slax;\n" }, "diff_lines": [ "", "[edit system scripts op]", "+ file bgp.slax;" ], "failed": false, "file": "build_conf/dc1a.example.net/junos-config.conf", "msg": "Configuration has been: opened, loaded, checked, diffed, committed, closed." } } PLAY RECAP **************************************************************** dc1a.example.net : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
验证配置
目的
验证正在运行的设备上配置是否正确更新Junos OS。
行动
查看 Ansible 指南输出,了解配置任务是否成功。您还可以登录运行 Junos OS 的设备,查看配置、提交历史记录和日志文件以验证配置和提交,例如:
user@dc1a> show configuration system scripts op { file bgp.slax; }
user@dc1a> show system commit 0 2020-12-17 15:33:50 PST by user via netconf Configuring op script with Ansible
user@dc1a> show log messages Dec 17 15:33:39 dc1a mgd[33444]: UI_COMMIT: User 'user' requested 'commit' operation (comment: Configuring op script with Ansible) Dec 17 15:33:57 dc1a mgd[33444]: UI_COMMIT_COMPLETED: commit complete
故障排除指南错误
故障排除超时错误
问题
指南会生成 TimeoutExpiredError
错误消息,但是无法更新设备配置。
ncclient.operations.errors.TimeoutExpiredError: ncclient timed out while waiting for an rpc reply
NETCONF RPC 的外发默认时间为 30 秒。较大的配置更改可能超过此值,从而导致操作在上传和提交配置之前的时间超过。
解决 方案
要容纳可能需要超过默认 RPC 超时间隔的提交时间的配置更改,请为相应的值设置模块参数, timeout
然后重新运行指南。
配置锁错误故障排除
问题
指南会生成 LockError
一条错误消息,表示配置不能锁定。例如:
FAILED! => {"changed": false, "msg": "Unable to open the configuration in exclusive mode: LockError(severity: error, bad_element: None, message: configuration database modified)"}
或
FAILED! => {"changed": false, "msg": "Unable to open the configuration in exclusive mode: LockError(severity: error, bad_element: lock-configuration, message: permission denied)"}
出现配置锁错误的原因如下:
另一位用户对配置有一个独占锁。
另一位用户对配置数据库进行了更改,但尚未提交更改。
执行 Ansible 模块的用户没有配置设备的权限。
解决 方案
LockError
消息字符串通常表示问题的根本原因。如果另一位用户对配置具有排他锁或已修改配置,请等待锁发布或提交更改,然后再次执行操作手册。如果问题的原因出在用户没有配置设备的权限,则使用拥有必要权限的用户执行操作指南,或者在适当时配置运行 Junos OS 的设备,为当前用户提供执行更改所需的权限。
配置更改错误故障排除
问题
指南会生成 ConfigLoadError
一条错误消息,表示配置无法修改,因为权限被拒绝。
FAILED! => {"changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: scripts, message: error: permission denied)"}
执行 Ansible 模块的用户具有更改配置的权限,但无权修改配置中请求的部分时,将生成此错误消息。
解决 方案
由拥有必要权限的用户执行操作指南,或者配置运行 Junos OS 以向当前用户授予更改所需的权限。