使用 Ansible 配置 Junos 设备
总结 使用瞻博网络 Ansible 模块管理 Junos 设备上的配置。
瞻博网络提供的 Ansible 模块使您能够配置 Junos 设备。 表 1 概述了可用的模块。用于进行配置更改的用户帐户必须有权更改每台设备上配置的相关部分。
内容集 |
模块名称 |
---|---|
从 Juniper.junos
2.0.0 版开始,该juniper_junos_config
模块组合并替换了 、 、 junos_get_config
junos_install_config
和junos_rollback
模块的功能junos_commit
。
以下部分讨论如何使用模块修改和提交 Junos 设备上的配置。
模块概述
通过和juniper_junos_config
模块,config
您可以在 Junos 设备上执行以下操作:
-
加载配置数据
-
提交配置
-
回滚配置
-
加载救援配置
要修改配置,模块的参数列表必须包含 load
用于加载新配置数据的参数,或用于恢复到救援配置或 rollback
以前提交的配置的参数。进行配置更改的基本过程是锁定配置,加载配置更改,提交配置以使其处于活动状态,然后解锁配置。
默认情况下,和模块使用configure exclusive
模式对候选配置数据库进行更改,config
该模式会自动锁定和juniper_junos_config
解锁候选全局配置。您还可以对候选配置的私有副本进行更改。有关指定配置模式的详细信息,请参阅如何指定配置模式。
加载新配置数据时,除了指定配置模式外,还可以指定加载操作以及更改的来源和格式。
-
加载操作 - 加载操作确定如何将配置数据加载到候选配置中。这些函数支持 Junos OS CLI 中提供的许多相同的加载操作。有关更多信息,请参见 如何指定加载操作。
-
格式 — 您可以使用受支持的标准格式之一配置 Junos 设备。您可以以文本、Junos XML 元素、Junos OS
set
命令或 JSON 形式提供配置数据或 Jinja2 模板。有关指定配置数据格式的信息,请参阅 如何指定要加载的配置数据的格式。 -
配置数据源 — 您可以通过分别包含
lines
、 、 或参数,从字符串列表、src
template
本地 Ansible 控制节点上的文件、Jinja2 模板或url
可从客户端设备访问的 URL 加载配置数据。有关指定配置数据源的详细信息,请参阅以下部分:
config
和juniper_junos_config
模块还使您能够加载并提交救援配置,或将配置回滚到先前提交的配置。要加载救援配置或以前提交的配置,必须包含rollback
模块参数。有关详细信息,请参阅以下部分:
修改配置后,必须提交配置以使其成为设备上的活动配置。默认情况下, config
和 juniper_junos_config
模块会将更改提交到配置。若要更改此行为或提供其他提交选项,请参阅 如何提交配置。
默认情况下,当 or 模块包含load
用于更改配置的 or juniper_junos_config
rollback
参数时config
,模块会自动在模块响应中以差异或修补程序格式返回配置更改。差异在和diff_lines
变量中diff
返回。要防止模块计算和返回差异,请将模块参数设置为 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 总结了每种负载操作所需的参数设置。
负载操作 |
加载参数 |
描述 |
---|---|---|
|
|
将加载的配置与现有配置合并。 |
|
|
将整个配置替换为加载的配置。 |
|
|
将加载的配置与现有配置合并,但将现有配置中的语句替换为在加载的配置中指定 |
|
|
从修补程序文件加载配置数据。 |
|
|
加载格式的 |
|
|
将完整加载的配置与现有配置进行比较。加载的配置中不同的每个配置元素都会替换其在现有配置中的相应元素。在提交操作期间,只有受已更改配置元素影响的系统进程才能解析新配置。 |
如何指定要加载的配置数据的格式
config
和juniper_junos_config
模块使您能够使用受支持的标准格式之一来配置 Junos 设备。您可以以字符串或文件的形式提供配置数据。文件可以包含配置数据或 Jinja2 模板。在字符串、文件或 Jinja2 模板中提供配置数据时,支持的数据类型格式包括文本、Junos XML 元素、Junos OS set
命令和 JSON。
从 Junos OS 16.1R1 版开始,Junos 设备支持以 JSON 格式加载配置数据。
config
和juniper_junos_config
模块尝试使用参数自动lines
检测以字符串形式提供的配置数据的格式。但是,可以通过包含format
参数来显式指定字符串的格式。在文件或 Jinja2 模板中提供配置数据时,必须通过向文件添加适当的扩展名或包含format
参数来指定数据的格式。
表 3 汇总了配置数据支持的格式以及文件扩展名和 format
参数的相应值。如果包含该 format
参数,它将覆盖字符串的自动检测格式和文件扩展名指示的格式。
配置数据格式 |
文件扩展名 |
格式参数 |
---|---|---|
CLI 配置语句(文本) |
.conf |
“ |
JavaScript Object Notation (JSON) |
。Json |
“ |
Junos OS |
。设置 |
“ |
Junos XML 元素 |
。Xml |
“ |
将 load
模块的参数设置为 'override'
或 'update'
时,不能使用 Junos OS set
命令格式。
如何将配置数据加载为字符串
config
和juniper_junos_config
模块使您能够从字符串列表中加载配置数据。若要将配置数据加载为字符串,请包含相应的load
参数和lines
参数。该lines
参数采用包含要加载的配置数据的字符串列表。
这些模块尝试自动检测配置数据的格式 lines
。但是,可以通过包含 format
参数来显式指定格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。如果将 format
参数包含在模块的参数列表中,它将覆盖自动检测格式。
以下操作指南配置并提交两个操作脚本。在这种情况下,参数具有值 'set'
,load
因为 中的lines
配置数据使用 Junos OS 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
以下 playbook 使用 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>
标记中。
直接在 NETCONF 会话中配置设备时,无需根据需要将格式化为 ASCII 文本、Junos OS set
命令或 JSON 的配置数据括在 <configuration-text>
、 <configuration-set>
或 <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 设备上的文件或者从 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 - 已装载文件系统上的文件,本地闪存盘或硬盘上。
-
答: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/%2Fpath/filename。
如何使用 Jinja2 模板加载配置数据
通过和juniper_junos_config
模块,config
您可以从 Ansible 控制节点上的 Jinja2 模板文件呈现配置数据,并在 Junos 设备上加载和提交配置。Jinja 是 Python 的模板引擎,使您能够从预定义的模板生成文档。模板是所需语言的文本文件,通过使用表达式和变量提供了灵活性。您可以使用 Jinja2 模板以受支持的配置格式之一创建 Junos OS 配置数据,其中包括 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
定义模板所需的变量。以下 playbook 使用 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; } }
如何加载救援配置
救援配置允许您定义已知工作配置或具有已知状态的配置,您可以随时还原。如果需要恢复到已知配置,或者在设备配置和备份配置文件损坏无法修复时,可以使用救援配置。创建救援配置时,设备会将最近提交的配置保存为救援配置。
通过和juniper_junos_config
模块,config
您可以恢复到 Junos 设备上的现有救援配置。若要在设备上加载并提交救援配置,请包含模块的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 设备可存储最近提交的配置的副本和最多 49 个先前配置的副本,具体取决于平台。您可以回滚到任何存储的配置。当配置更改导致不良结果,并且您希望恢复到已知的工作配置时,这很有用。回滚配置类似于在设备上进行配置更改的过程,但不是加载配置数据,而是执行回滚,这会将整个候选配置替换为之前提交的配置。
通过和juniper_junos_config
模块,config
您可以在 Junos 设备上回滚到之前提交的配置。要回滚配置并提交它,请包含模块的参数,并指定回滚配置的 rollback
ID。有效 ID 值为 0(对于最近提交的配置为零)到比存储的先前配置数(最大值为 49)少 1。
以下 playbook 提示输入要还原的配置的回滚 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
如何提交配置
默认情况下,当您使用 or config
juniper_junos_config
模块通过 or rollback
参数修改load
配置时,模块会自动执行提交检查并提交更改。要防止模块执行提交检查或提交更改,请将 or commit
参数分别设置为 。check
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 分钟。确认的提交操作可用于验证配置更改是否正常工作,并且不会阻止对设备的管理访问。如果更改阻止访问或导致其他错误,则自动回滚到以前的配置可以在回滚截止时间过后访问设备。要确认提交操作,请使用 or 参数调用 config
or juniper_junos_config
commit: true
模块check: true
。
在以下 playbook 中,第一个任务修改配置,在提交检查和提交操作之间等待 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 设备上的配置。在某些情况下,RPC 回复可能包含 <rpc-error>
严重性级别为警告或更高的元素,这些元素会导致模块引发 RpcError
异常,从而导致加载或提交操作失败。
在某些情况下,可能有必要或希望禁止为RpcError
响应加载和提交操作的警告而引发的异常。可以通过config
将参数包含在ignore_warning
模块的参数列表中来指示RpcError
和juniper_junos_config
模块抑制为警告引发的异常。该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 Junos device 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
如果所有元素的<rpc-error>
严重性级别均为警告,并且响应中的每个警告都与一个或多个指定字符串匹配,则该模块将RpcError
禁止异常。
示例:使用 Ansible 配置 Junos 设备
该 config
模块使您能够管理 Junos 设备上的配置。此示例使用该模块通过 config
NETCONF over SSH 在 Junos 设备上进行配置更改。
要求
此示例使用以下硬件和软件组件:
运行 Ansible 2.10 或更高版本且已安装集合的
juniper.device
配置管理服务器启用了 NETCONF 且用户帐户配置了适当权限的 Junos 设备
在 Ansible 控制器和 Junos 设备上为相应用户配置 SSH 公钥/私钥对
定义了所需主机的现有 Ansible 清单文件
概述
此示例显示了一个 Ansible 操作手册,该手册使用该模块在 config
目标 Junos 设备的配置中启用新的操作脚本。配置数据文件 junos-config.conf 包含格式化为文本格式的相关配置数据。
该手册包含以下任务, Checking NETCONF connectivity
该任务利用 wait_for
Ansible 模块尝试使用 NETCONF 默认端口 (830) 与目标设备建立 NETCONF 会话。如果控制节点在 playbook 执行期间无法与目标设备建立 NETCONF 会话,则会跳过该设备的剩余任务。
配置设备的任务将执行模块,config
前提是 NETCONF 检查成功。load: "merge"
模块参数使用操作将load merge
新配置数据加载到候选配置中。默认情况下,模块config
在设备上提交用于和rollback
操作的load
配置数据。模块参数包括参数comment
,该参数在设备的系统日志文件和提交历史记录中记录提交注释。
配置
创建配置数据文件
分步过程
创建模块使用的配置数据文件:
根据配置数据的格式(在此示例中为文本)创建具有相应扩展名的新文件。
在文件中包括所需的配置更改。
user@ansible-cn:~/ansible$ cat build_conf/dc1a.example.net/junos-config.conf system { scripts { op { file bgp.slax; } } }
创建 Ansible 剧本
分步过程
要创建使用该模块在 config
Junos 设备上进行配置更改的操作指南,请执行以下操作:
包括在本地执行模块的剧本样板。
--- - name: Load and commit configuration data on a Junos device 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 Junos device 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 Junos device] **** 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 设备上的配置是否已正确更新。
行动
查看 Ansible 剧本输出,了解配置任务是成功还是失败。您还可以登录 Junos 设备并查看配置、提交历史记录和日志文件,以验证配置和提交,例如:
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
参数设置为适当的值,然后重新运行 playbook。
排查配置锁定错误
问题
剧本会生成一条 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
消息字符串通常指示问题的根本原因。如果其他用户对配置具有独占锁或修改了配置,请等待锁定释放或更改提交,然后再次执行 playbook。如果问题的原因是用户没有配置设备的权限,请与具有必要权限的用户一起执行 playbook,或者在适当的情况下配置 Junos 设备以向当前用户授予进行更改所需的权限。
排查配置更改错误
问题
playbook 会生成一条 ConfigLoadError
错误消息,指示无法修改配置,因为权限被拒绝。
FAILED! => {"changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: scripts, message: error: permission denied)"}
当执行 Ansible 模块的用户有权更改配置,但无权更改所请求的配置部分时,将生成此错误消息。
解决 方案
使用具有必要权限的用户执行 playbook,或者配置 Junos 设备以授予当前用户进行更改所需的权限(如果适用)。
Juniper.junos
2.0.0 版开始,该
juniper_junos_config
模块组合并替换了 、 、
junos_get_config
junos_install_config
和
junos_rollback
模块的功能
junos_commit
。