使用 Ansible 配置 Junos 设备
总结 使用瞻博网络 Ansible 模块管理 Junos 设备上的配置。
瞻博网络提供了一个 Ansible 模块,可用于配置 Junos 设备。 表 1 概述了可用的模块。用于进行配置更改的用户帐户必须有权更改每台设备上配置的相关部分。
内容集 |
模块名称 |
---|---|
以下各节讨论如何使用该模块在 Junos 设备上修改和提交配置。
模块概述
该 juniper.device.config
模块允许您在 Junos 设备上执行以下操作:
-
加载配置数据
-
提交配置
-
回滚配置
-
加载救援配置
要修改配置,模块的参数列表必须包含 load
用于加载新配置数据的参数或 rollback
用于恢复到挽救配置或先前提交的配置的参数。进行配置更改的基本过程是锁定配置,加载配置更改,提交配置以使其处于活动状态,然后解锁配置。
默认情况下, config
模块使用 configure exclusive
模式对候选配置数据库进行更改,该模式会自动锁定和解锁候选全局配置。您还可以指定其他配置模式。例如,您可以对候选配置的专用副本或临时配置数据库进行更改。有关指定配置模式的详细信息,请参阅 如何指定配置模式。
加载新配置数据时,除了指定配置模式外,还可以指定加载操作以及更改的来源和格式。
-
加载操作 - 加载操作确定如何将配置数据加载到所选配置数据库中。该函数支持许多与 Junos OS CLI 中提供的相同加载操作。有关更多信息,请参见 如何指定加载操作。
-
格式 - 您可以使用其中一种受支持的标准格式来配置 Junos 设备。您可以以文本、Junos XML 元素、Junos OS 命令或 JSON 的形式提供配置数据或 Jinja2
set
模板。有关指定配置数据格式的信息,请参阅 如何指定要加载的配置数据的格式。 -
配置数据源 — 您可以分别从
lines
src
template
url
字符串列表、本地 Ansible 控制节点上的文件、Jinja2 模板或可从客户端设备访问的 URL 加载配置数据。有关指定配置数据源的详细信息,请参阅以下部分:
该 config
模块还允许您加载并提交救援配置,或将配置回滚到之前提交的配置。若要加载救援配置或以前提交的配置,必须包含 rollback
模块参数。有关详细信息,请参阅以下部分:
修改配置后,您必须提交配置,使其成为设备上的活动配置。默认情况下, config
模块会提交对配置的更改。要更改此行为或提供其他提交选项,请参阅 如何提交配置。
默认情况下,当模块包含用于更改配置的 load
or rollback
参数时config
,模块会在模块的响应中自动以 diff 或 patch 格式返回配置更改。差异在 和 diff_lines
变量中diff
返回。要防止模块计算并返回差值,请将 diff
module 参数设置为 false
。
如何指定配置模式
您可以指定修改设备配置时要使用的配置模式。要在任务中指定配置模式,请包含 config
模块的 config_mode
参数。支持的配置模式包括:
-
batch
-
dynamic
-
ephemeral
-
exclusive
-
private
默认情况下, juniper.device.config
模块使用 configure exclusive
模式对候选配置数据库进行更改。只要模块需要对配置进行请求的更改,配置独占模式就会锁定候选全局配置(也称为 共享配置数据库)。锁定数据库可防止其他用户修改或提交对数据库的更改,直到解锁为止。
以下示例说明如何配置候选配置的专用副本以及如何配置临时数据库。
示例: config_mode:“private”
以下操作指南使用 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" ansible.builtin.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
配置临时数据库
您可以使用该 juniper.device.config
模块在支持此数据库的设备上更新 临时配置数据库 。临时数据库是一种备用配置数据库,它提供了一个快速编程接口,用于在 Junos 设备上执行配置更新。
要打开并配置临时配置数据库的默认实例,请包含该 config_mode: "ephemeral"
参数。例如:
--- - name: "Configure ephemeral database" hosts: dc1a connection: local gather_facts: no tasks: - name: "Configure the default ephemeral database" juniper.device.config: config_mode: "ephemeral" load: "set" lines: - "set protocols mpls label-switched-path to-hastings to 192.0.2.1"
若要打开和配置临时配置数据库的现有用户定义实例,请包含该 config_mode: "ephemeral"
参数,并将该参数设置为 ephemeral_instance
实例的名称。
tasks: - name: "Configure a user-defined ephemeral instance" juniper.device.config: config_mode: "ephemeral" ephemeral_instance: "eph1" load: "set" lines: - "set protocols mpls label-switched-path to-hastings to 192.0.2.2"
如何指定加载操作
该 juniper.device.config
模块支持使用许多与 Junos OS CLI 中支持的相同加载操作来加载配置更改。通过将参数包含在 load
模块的参数列表中并将其设置为相应加载操作的值来指定加载操作。 表 2 总结了每种类型的加载操作所需的参数设置。
负载操作 |
load 参数 |
描述 |
---|---|---|
|
|
将加载的配置与现有配置合并。 |
|
|
将整个配置替换为加载的配置。 |
|
|
从修补程序文件加载配置数据。 |
|
|
将加载的配置与现有配置合并,但将现有配置中的语句替换为在已加载的配置中指定标记的 |
|
|
加载格式化的 |
|
|
将完整加载的配置与现有配置进行比较。在已加载的配置中不同的每个配置元素都会替换现有配置中其对应的元素。在提交操作期间,只有受更改配置元素影响的系统进程才会分析新配置。 |
如何指定要加载的配置数据的格式
借助该 juniper.device.config
模块,您可以使用其中一种标准、受支持的格式来配置 Junos 设备。您可以提供字符串或文件形式的配置数据。文件可以包含配置数据或 Jinja2 模板。在字符串、文件或 Jinja2 模板中提供配置数据时,支持的数据格式包括文本、Junos XML 元素、Junos OS set
命令和 JSON。
该 config
模块尝试自动检测在参数中 lines
作为字符串提供的配置数据的格式。但是,您可以通过包含 format
参数来显式指定字符串的格式。在文件或 Jinja2 模板中提供配置数据时,必须通过向文件添加适当的扩展名或包含 format
参数来指定数据的格式。
表 3 汇总了配置数据支持的格式以及文件扩展名和 format
参数的相应值。如果包含该 format
参数,它将覆盖字符串的自动检测格式和文件扩展名指示的格式。
配置数据格式 |
文件扩展名 |
format 参数 |
---|---|---|
CLI 配置语句(文本) |
.conf 文件 |
“ |
JavaScript 对象标记 (JSON) |
.json |
“ |
|
。设置 |
“ |
Junos XML 元素 |
.xml |
“ |
将 load
模块的参数设置为 'override'
或 'update'
时,不能使用 Junos OS set
命令格式。
如何将配置数据加载为字符串
您可以通过该 juniper.device.config
模块从字符串列表中加载配置数据。若要将配置数据加载为字符串,请包含相应的 load
参数和 lines
参数。该 lines
参数采用包含要加载的配置数据的字符串列表。
该模块会尝试自动检测配置数据的 lines
格式。但是,您可以通过包含 format
参数来显式指定格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。如果包含该 format
参数,它将覆盖自动检测的格式。
以下 playbook 配置并提交两个 op 脚本。在这种情况下, load
参数的值 'set'
为 ,因为中的 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" ansible.builtin.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" ansible.builtin.debug: var: response
如何从本地或远程文件加载配置数据
该 juniper.device.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 控制节点上文件的绝对或相对路径。默认目录是 playbook 目录。 |
|
客户端设备上文件的绝对或相对路径,或者 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" ansible.builtin.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" ansible.builtin.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.device.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 %} } }
要使用该 juniper.device.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" ansible.builtin.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.device.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" ansible.builtin.debug: var: response
如何回滚配置
Junos 设备会存储最近提交的配置和最多 49 个以前配置的副本,具体取决于平台。您可以回滚到任何存储的配置。当配置更改导致不希望的结果,并且您希望恢复为已知的工作配置时,这将非常有用。回滚配置类似于在设备上进行配置更改的过程,但执行回滚,而不是加载配置数据,这会将整个候选配置替换为之前提交的配置。
您可以通过该 juniper.device.config
模块回滚到 Junos 设备上先前提交的配置。要回滚配置并提交,请包含模块的 rollback
参数,并指定回滚配置的 ID。有效 ID 值为 0(对于最近提交的配置为零),比之前存储的配置数(最大值为 49)少 1。
以下操作指南会提示输入要还原的配置的回滚 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" ansible.builtin.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
如何提交配置
默认情况下,当您使用juniper.device.config
模块通过load
或 参数rollback
修改配置时,模块会自动执行提交检查并提交更改。若要防止模块执行提交检查或提交更改,请将 或 commit
参数分别设置为 。check
false
您还可以使用许多与 Junos OS CLI 中提供的相同选项来自定义提交操作。 表 5 概述了可用于指定不同提交选项的模块参数。
模块参数 |
描述 |
和 |
---|---|---|
|
执行提交检查或确认之前确认的提交操作。 |
|
|
在提交检查和提交操作之间等待指定的秒数。 |
– |
|
在系统日志文件和设备的提交历史记录中记录该提交操作的注释。 |
– |
|
提交配置更改或确认之前确认的提交操作。 |
|
|
即使候选配置与提交的配置之间没有差异,也要提交配置更改。 |
|
|
同步并提交所有路由引擎上的配置,即使其他路由引擎上有打开的配置会话或未提交的配置更改也是如此。 |
|
|
同步并提交所有路由引擎上的配置。 |
|
|
要求在初始提交后的指定时间内确认提交操作。如果未在指定时间内确认提交,请回滚到之前提交的配置。
|
– |
|
使用指定值作为超时,等待操作完成。 |
30 秒 |
提交评论
提交配置时,可以添加简短注释,说明已提交更改的用途。若要记录描述更改的注释,请将参数与消息字符串一起包含在 comment: "comment string"
内。
提交检查
默认情况下,该 config
模块同时执行提交检查和提交操作。该 check_commit_wait
参数定义提交检查和提交操作之间等待的秒数。当需要为设备提供足够的时间来完成提交检查操作并在启动提交操作之前释放配置锁定时,请包含此参数。如果省略该 check_commit_wait
参数,在某些情况下,设备可能会在提交检查操作释放其对配置的锁定之前启动提交操作,从而导致 CommitError
提交操作失败。
提交空更改
默认情况下,如果候选配置和提交的配置之间没有差异,则模块不会提交更改。若要在没有差异的情况下强制执行提交操作,请包含 commit_empty_changes: true
该参数。
提交同步
如果设备具有双路由引擎,则可以通过包含 commit_sync: true
参数在两个路由引擎上同步并提交配置。若要强制 commit synchronize
操作成功,即使其他路由引擎上存在打开的配置会话或未提交的配置更改,请使用参数 commit_force_sync: true
。如果包含该 commit_force_sync: true
选项,设备将终止其他路由引擎上的所有配置会话,然后再同步并提交配置。
提交、确认
若要要求在初始提交后的指定时间内确认提交操作,请包含该confirmed: minutes
参数。如果在给定时限内未确认提交,配置将自动回滚到之前提交的配置。允许的范围为 1 到 65,535 分钟。确认的提交操作可用于验证配置更改是否正常工作,并且不会阻止对设备的管理访问。如果更改阻止访问或导致其他错误,则自动回滚到之前的配置将在回滚截止时间过后允许访问设备。若要确认提交操作,请使用 check: true
or commit: true
参数调用config
模块。
在下面的操作指南中,第一个任务修改配置,在提交检查和提交操作之间等待 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" ansible.builtin.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" ansible.builtin.debug: var: response
配置设备时如何忽略警告
您可以通过该 juniper.device.config
模块在 Junos 设备上修改和提交配置。在某些情况下,RPC 回复可能包含 <rpc-error>
严重性级别为 warning 或更高的元素,这些元素会导致模块引发 RpcError
异常。 RpcError
异常可能会导致加载或提交操作失败。
在某些情况下,可能有必要或希望抑制RpcError
为响应加载和提交操作的警告而引发的异常。您可以通过在模块的参数列表中包含ignore_warning
参数来指示config
模块抑制RpcError
因警告而引发的异常。该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 ansible.builtin.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 ansible.builtin.debug: var: response
如果所有<rpc-error>
元素的严重性级别均为警告,并且响应中的每个警告都与一个或多个指定字符串匹配,则该模块将RpcError
抑制异常。
示例:使用 Ansible 配置 Junos 设备
您可以通过该 juniper.device.config
模块管理 Junos 设备上的配置。此示例使用该 config
模块通过 SSH 上的 NETCONF 对 Junos 设备进行配置更改。
要求
此示例使用以下硬件和软件组件:
-
运行 Ansible 2.10 或更高版本并安装集合的
juniper.device
配置管理服务器 -
启用了 NETCONF 且用户帐户配置了适当权限的 Junos 设备
-
在 Ansible 控制器和 Junos 设备上为相应用户配置的 SSH 公钥/私钥对
-
定义了所需主机的现有 Ansible 清单文件
概述
此示例提供了一个 Ansible 操作指南,该指南使用该 juniper.device.config
模块在目标 Junos 设备的配置中启用新的 op 脚本。配置数据文件 junos-config.conf 包含格式为文本的相关配置数据。
该操作指南包含以下 Check NETCONF connectivity
任务,该 ansible.builtin.wait_for
任务利用 Ansible 模块尝试使用 NETCONF 默认端口 (830) 与目标设备建立 NETCONF 会话。如果控制节点在执行 playbook 期间未能与目标设备建立 NETCONF 会话,则它将跳过该设备的剩余任务。
该操作指南使用该 juniper.device.file_copy
模块将新的 op 脚本从 Ansible 控制节点复制到 Junos 设备。模块参数指定本地设备上脚本的目录和文件名,以及远程设备上的目标目录。
如果 NETCONF 检查成功,则配置设备的任务将 juniper.device.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 设备上进行配置更改的操作指南,请执行以下操作:
包括在本地执行模块的 playbook 样板。
--- - name: Load and commit configuration data on a Junos device hosts: dc1 connection: local gather_facts: no
(选答)创建任务以验证 NETCONF 连接。
tasks: - name: Check NETCONF connectivity ansible.builtin.wait_for: host: "{{ inventory_hostname }}" port: 830 timeout: 5
创建一个任务以将新的 op 脚本复制到设备。
- name: Copy the op script to the device juniper.device.file_copy: action: put file: bgp.slax local_dir: scripts remote_dir: /var/db/scripts/op
创建任务以将配置加载到设备上并提交。
- 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
(选答)创建一个任务以打印响应,其中包括 diff 格式的配置更改。
- name: Print the response ansible.builtin.debug: var: response
结果
在 Ansible 控制节点上,查看已完成的操作指南。如果 playbook 未显示预期的代码,请重复此示例中的说明以更正 playbook。
--- - name: Load and commit configuration data on a Junos device hosts: dc1 connection: local gather_facts: no tasks: - name: Check NETCONF connectivity ansible.builtin.wait_for: host: "{{ inventory_hostname }}" port: 830 timeout: 5 - name: Copy the op script to the device juniper.device.file_copy: action: put file: bgp.slax local_dir: scripts remote_dir: /var/db/scripts/op - 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 ansible.builtin.debug: var: response
执行操作手册
要执行 playbook,请执行以下操作:
-
在控制节点上发出
ansible-playbook
命令,并提供操作指南路径和所需的任何选项。user@ansible-cn:~/ansible$ ansible-playbook ansible-pb-junos-config.yaml PLAY [Load and commit configuration data on a Junos device] *************** TASK [Check NETCONF connectivity] ***************************************** ok: [dc1a.example.net] TASK [Copy the op script to the device] *********************************** changed: [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=4 changed=2 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
对 Playbook 错误进行故障排除
超时错误疑难解答
问题
playbook 将生成 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
消息字符串通常指示问题的根本原因。如果其他用户对配置具有独占锁定或已修改配置,请等待释放锁定或提交更改,然后再次执行操作指南。如果问题的原因是用户没有配置设备的权限,请使用具有必要权限的用户执行操作,或者在适当的情况下,将 Junos 设备配置为为当前用户提供进行更改所需的权限。
配置变更错误疑难解答
问题
playbook 将生成一条 ConfigLoadError
错误消息,指示无法修改配置,因为权限被拒绝。
FAILED! => {"changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: scripts, message: error: permission denied)"}
当执行 Ansible 模块的用户有权更改配置,但没有更改配置请求的部分时,将生成此错误消息。
溶液
使用具有必要权限的用户执行操作,或者在适当的情况下,将 Junos 设备配置为为当前用户提供进行更改所需的权限。