使用 juniper.device.config Ansible 模块管理 Junos OS 配置
juniper.device.config您可以使用 Ansible 模块来管理运行 Junos OS 的设备和运行 Junos OS Evolved 的设备上的配置。
瞻博网络提供了用于配置 Junos 设备的 Ansible 模块。 表 1 概述了可用的模块。如果您已经在使用集合中的 juniper.device 一组给定模块,请使用该集的模块。
|
收藏 |
模块集 |
模块名称 |
|---|---|---|
|
|
||
|
|
juniper.device.junos_config |
以下部分讨论如何使用该 juniper.device.config 模块修改和提交 Junos 设备上的配置。有关该 juniper.device.junos_config 模块的信息,请参阅 使用 juniper.device.junos_config Ansible 模块配置Junos设备。
模块概述
该 juniper.device.config 模块允许您在 Junos 设备上执行以下操作:
-
加载配置数据
-
提交配置
-
回滚配置
-
加载救援配置
进行配置更改的基本过程是锁定配置,加载配置更改,提交配置使其处于活动状态,然后解锁配置。用于进行配置更改的用户帐户必须具有更改每个设备上配置的相关部分的权限。要修改配置,模块的参数列表必须包含以下参数之一:
-
load—加载新的配置数据。 -
rollback— 恢复为挽救配置或之前提交的配置。
默认情况下,模块使用 juniper.device.config mode 对 configure exclusive 候选配置数据库进行更改,该模式会自动锁定和解锁候选全局配置。您还可以指定其他配置模式。例如,可以更改候选配置的专用副本或临时配置数据库。有关指定配置模式的详细信息,请参阅 如何指定配置模式。
加载新的配置数据时,除了指定配置模式外,还可以指定加载操作以及更改的来源和格式。
-
加载操作 (Load operation) - 加载操作确定如何将配置数据加载到选定配置数据库中。您可以从 Junos OS CLI 中提供的许多相同的加载操作中进行选择。有关详细信息,请参阅 如何指定加载操作。
-
格式 — 可以使用受支持的标准格式之一配置 Junos 设备。您可以以文本、Junos XML 元素、Junos OS
set命令或 JSON 的形式提供配置数据或 Jinja2 模板。有关指定配置数据格式的信息,请参阅 如何指定要加载的配置数据的格式。 -
配置数据源 — 您可以从字符串列表、本地 Ansible 控制节点上的文件、Jinja2 模板或可从客户端设备访问的 URL 中加载配置数据。有关指定配置数据源的更多信息,请参阅以下部分:
该 juniper.device.config 模块还允许您加载并提交挽救配置,或将配置回滚到之前提交的配置。要加载救援配置或之前提交的配置,必须包含 rollback module 参数。有关更多信息,请参阅以下部分:
修改配置后,您必须提交配置,使其成为设备上的活动配置。默认情况下,模块 juniper.device.config 会对配置提交更改。要更改此行为或提供其他提交选项,请参阅 如何提交配置。
默认情况下,当模块包含 load or rollback 参数时juniper.device.config,模块的响应会自动以 diff 或 patch 格式返回配置更改。该模块返回 和 diff diff_lines 字段中的差异。要防止模块计算和返回差异,请将模块参数false设置为 diff 。
如何指定配置模式
您可以指定修改设备配置时要使用的配置模式。要在任务中指定配置模式,请包含模块的 config_mode 参数。支持的配置模式包括:
-
batch -
dynamic -
ephemeral -
exclusive -
private
默认情况下,模块使用 juniper.device.config 模式对候选配置数据库 configure exclusive 进行更改。配置排他模式会锁定候选全局配置(也称为 共享配置数据库),只要模块需要进行请求的配置更改。锁定数据库可防止其他用户同时修改或提交对数据库的更改。
以下示例说明如何配置候选配置的专用副本以及如何配置临时数据库。
示例:config_mode:“私密”
以下操作指南使用配置模式修改 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 汇总了不同加载操作的参数值。
|
负载操作 |
|
描述 |
|---|---|---|
|
|
|
将加载的配置与现有配置合并。 |
|
|
|
将整个配置替换为加载的配置。所有系统进程都会解析配置。 |
|
|
|
从修补程序文件加载配置数据。 |
|
|
|
将加载的配置与现有配置合并,但将现有配置中的语句替换为已加载配置中指定标记的 |
|
|
|
加载格式化 |
|
|
|
加载完整配置并将其与现有配置进行比较。仅替换候选配置中已更改的部分。在提交操作期间,只有受影响的系统进程才能解析新配置。 |
如何指定要加载的配置数据的格式
该模块允许您 juniper.device.config 使用一种标准支持的格式配置 Junos 设备。您可以以字符串或文件的形式提供配置数据。文件可以包含配置数据或 Jinja2 模板。在字符串、文件或 Jinja2 模板中提供配置数据时,支持的数据格式包括文本、Junos XML 元素、Junos OS set 命令和 JSON。
该 juniper.device.config 模块会尝试自动检测您在参数中 lines 作为字符串提供的配置数据的格式。但是,可以通过包含 format 参数来显式指定字符串的格式。在文件或 Jinja2 模板中提供配置数据时,必须指定数据的格式。向文件添加适当的扩展名或包含 format 参数。
表 3 汇总了配置数据支持的格式以及文件扩展名和 format 参数的相应值。如果包含参数 format ,它会覆盖字符串的自动检测格式和文件扩展名指示的格式。
|
配置数据格式 |
文件扩展名 |
|
|---|---|---|
|
CLI 配置语句(文本) |
.conf 域名 |
|
|
JavaScript 对象标记 (JSON) |
.json |
|
|
Junos OS |
.set |
|
|
Junos XML 元素 |
.xml |
|
将模块的load参数设置为 或 update时,不能使用 Junos OS set 命令override格式。
如何将配置数据加载为字符串
该 juniper.device.config 模块允许您从字符串列表中加载配置数据。要将配置数据加载为字符串,请包含相应 load 的参数和 lines 参数。该 lines 参数采用包含要加载的配置数据的字符串列表。
该模块会尝试自动检测配置数据的 lines 格式。但是,您可以通过包含 format 参数来显式指定格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。如果包含 format 参数,它将覆盖自动检测的格式。
以下操作指南配置并提交两个 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 控制节点
-
客户端设备
-
可从客户端设备访问的 FTP 或 HTTP URL
从文件加载配置数据时,必须指明文件的位置和文件中配置数据的格式。支持的配置数据格式包括文本、Junos XML 元素、Junos OS set 命令和 JSON。有关加载包含 Jinja2 模板的文件的信息,请参阅 如何使用 Jinja2 模板加载配置数据。
要指定格式,请包含模块的 format 参数或向配置文件添加适当的扩展名。如果指定该 format 参数,它将覆盖文件扩展名指示的格式。有关指定格式的信息,请参阅 如何指定要加载的配置数据的格式。当配置数据使用 Junos XML 格式时,您必须将数据括在顶级 <configuration> 标记中。
直接在 NETCONF 会话中配置设备时,无需将格式化为文本、Junos OS set 命令或 JSON 或<configuration-text><configuration-set><configuration-json>标记的配置数据括起来。
表 4 概述了可用于指定文件位置的模块参数。
|
模块参数 |
描述 |
|---|---|
|
|
|
|
|
|
要从 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 参数。指定要加载的文件的路径或 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 and 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 定义模板所需的变量。
以下 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
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(对于最近提交的配置为 0)到比存储的先前配置数少 1(最大值为 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
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模块修改配置时,模块会自动执行提交检查并提交更改。要防止模块执行提交检查或提交更改,请分别将 or commit 参数false设置为 check 。
您还可以使用 Junos OS CLI 中提供的许多相同选项自定义提交操作。 表 5 概述了可用于指定不同提交选项的模块参数。
|
模块参数 |
描述 |
和 |
|---|---|---|
|
|
执行提交检查或确认之前确认的提交操作。 |
|
|
|
在提交检查和提交操作之间等待指定的秒数。 |
– |
|
|
在系统日志文件和设备的提交历史记录中记录该提交操作的注释。 |
– |
|
|
提交配置更改或确认之前确认的提交操作。 |
|
|
|
即使候选配置没有更改,也要提交配置。 |
|
|
|
同步并提交所有路由引擎上的配置,即使另一个路由引擎有打开的配置会话或未提交的配置更改。 |
|
|
|
同步并提交所有路由引擎上的配置。 |
|
|
|
要求在初始提交后的指定时间内确认提交操作。如果未在指定时间内确认提交,请回滚到之前提交的配置。 使用该 |
– |
|
|
使用指定的值作为超时等待操作完成。 |
30 秒 |
提交评论
提交配置时,可以添加简短的注释来描述提交更改的目的。要记录描述更改的注释,请将参数与消息字符串一起包含在 comment: "comment string" 内。
提交检查
默认情况下,该 juniper.device.config 模块同时执行提交检查和提交操作。 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 分钟。确认的提交操作可用于验证配置更改是否正常工作,并且不会阻止管理访问设备。如果更改导致访问失败或导致其他错误,则在回滚截止时间过后,自动回滚到之前的配置将允许访问设备。要确认提交操作,juniper.device.config请使用 or commit: true 参数调用check: 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
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> 严重性级别为“警告”或更高的元素,这些元素会导致模块引发 RpcError 异常。异常可能会导致 RpcError 加载或提交操作失败。
在某些情况下,可能有必要或希望抑制RpcError为响应加载和提交操作的警告而引发的异常。您可以通过juniper.device.config在模块的参数列表中包含ignore_warning参数来指示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 模块通过 NETCONF over SSH 在 Junos 设备上进行配置更改。
要求
此示例使用以下硬件和软件组件:
-
运行 Ansible 2.17 或更高版本并安装集合的
juniper.device配置管理服务器 -
启用了 NETCONF 且用户帐户配置了适当权限的 Junos 设备
-
在 Ansible 控制器和 Junos 设备上为相应用户配置的 SSH 公钥/私钥对
-
定义了所需主机的现有 Ansible 清单文件
概述
此示例提供 Ansible 操作指南,该操作指南使用该 juniper.device.config 模块在目标 Junos 设备的配置中启用新的操作脚本。配置文件 junos-config.conf 包含格式为文本的相关配置数据。
该操作指南包含任务 Check NETCONF connectivity ,该任务利用 ansible.builtin.wait_for Ansible 模块尝试使用 NETCONF 默认端口 (830) 与目标设备建立 NETCONF 会话。如果控制节点在操作指南执行期间未能与目标设备建立 NETCONF 会话,则将跳过该设备正在执行的剩余任务。
该操作指南使用该 juniper.device.file_copy 模块将新的操作脚本从 Ansible 控制节点复制到 Junos 设备。模块参数指定脚本在本地设备上的目录和文件名,以及在远程设备上的目标目录。
如果 NETCONF 检查成功,则配置设备的任务将执行模块juniper.device.config。该load: "merge"参数使用操作load merge将新的配置数据加载到候选配置中。默认情况下,该config模块会在设备上提交配置数据,以进行loadrollback操作。模块参数包括参数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: 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 -
(选答)创建一个任务以打印响应,其中包括 差异 格式的配置更改。
- 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
执行剧本
要执行该剧本,请执行以下步骤:
-
在控制节点上发出
ansible-playbook命令,并提供 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
排解剧本错误
超时错误故障排除
问题
操作指南会生成一条 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。如果问题的原因是用户无权配置设备,请使用具有必要权限的用户执行操作指南,或者在适当情况下,配置 Junos 设备以授予当前用户进行更改所需的权限。
配置更改错误故障排除
问题
操作指南将生成一条 ConfigLoadError 错误消息,指示无法修改配置,因为权限被拒绝。
FAILED! => {"changed": false, "msg": "Failure loading the configuraton: ConfigLoadError(severity: error, bad_element: scripts, message: error: permission denied)"}
当执行 Ansible 模块的用户有权更改配置,但无权更改配置的请求部分时,将生成此错误消息。
解决方案
使用具有必要权限的用户执行操作指南,或者在适当情况下配置 Junos 设备以向当前用户提供进行更改所需的权限。