ansible.netcommon.netconf_config 模块 – Netconf 设备配置
注意
此模块是 ansible.netcommon 集合 (版本 7.1.0) 的一部分。
如果您使用的是 ansible
包,则可能已安装此集合。它不包含在 ansible-core
中。要检查它是否已安装,请运行 ansible-galaxy collection list
。
要安装它,请使用: ansible-galaxy collection install ansible.netcommon
。您需要其他要求才能使用此模块,请参阅 要求 获取详细信息。
要在 playbook 中使用它,请指定: ansible.netcommon.netconf_config
。
ansible.netcommon 1.0.0 中的新增功能
概要
Netconf 是由 IETF 开发和标准化的网络管理协议。它在 RFC 6241 中有记录。
此模块允许用户将配置 XML 文件发送到 Netconf 设备,并检测配置是否发生更改。
要求
以下要求是在执行此模块的主机上所需的。
ncclient
参数
参数 |
注释 |
---|---|
此参数将导致模块在进行任何更改之前创建远程设备当前 选项
|
|
这是一个包含与备份文件路径相关的可配置选项的字典对象。只有当 |
|
此选项提供以目录名结尾的路径,将在其中存储备份配置文件。如果目录不存在,它将首先被创建,文件名要么是 |
|
用于存储备份配置的文件名。如果没有给出文件名,它将根据主机名、当前时间和日期以 <主机名>_config.<当前日期>@<当前时间> 格式生成。 |
|
此布尔标志控制在编辑候选数据存储后是否应提交配置更改。此选项仅在远程 Netconf 服务器支持:candidate 功能时才受支持。如果该值设置为 *False*,则在 edit-config 操作后不会发出 commit,用户需要显式处理 commit 或 discard-changes。 选项
|
|
此参数将为提交配置超时值,在自动回滚之前确认提交。如果 默认值: |
|
此参数将在远程设备上执行提交操作。它可用于确认之前的提交。 选项
|
|
设备数据模型定义的配置数据,该值可以是 xml 字符串格式、文本格式或 JSON 格式的 Python 字典表示形式。 如果是 json 字符串格式,则在推送到远程主机之前,将使用 xmltodict 库将其转换为相应的 xml 字符串。 如果此选项的值为 *text* 格式,则远程 Netconf 服务器应支持该格式。 如果 |
|
<edit-config> rpc 的默认操作,有效值为 *merge*、*replace* 和 *none*。如果默认值为 merge,则 选项
|
|
它指示模块从 选项
|
|
此选项控制在编辑配置期间发生错误后 netconf 服务器的操作。 如果 *error_option=stop-on-error*,则在第一个错误时中止配置编辑。 如果 *error_option=continue-on-error*,则继续处理配置数据中的错误。如果发生任何错误,则会记录错误并生成负面响应。 如果 *error_option=rollback-on-error*,则如果发生任何错误,则回滚到原始配置。这需要远程 Netconf 服务器支持 *error_option=rollback-on-error* 功能。 选项
|
|
作为 如果是 json 字符串格式,则在推送到远程主机之前,将使用 xmltodict 库将其转换为相应的 xml 字符串。 对于配置的 *text* 格式,远程 Netconf 服务器应支持。 如果没有给出 如果未识别数据格式,则默认设置为 *xml*。 选项
|
|
此参数指定充当过滤器的 XML 字符串,用于限制从远程设备检索的数据部分,在调用 edit_config 后比较设备的之前和之后状态时使用。如果未指定,则根据 |
|
指示模块显式锁定指定为 选项
|
|
选项
|
|
用作源的配置数据存储的名称,用于将配置复制到 |
|
要编辑的配置数据存储的名称。 - auto,使用 candidate 并回退到 running - candidate,编辑 <candidate/> 数据存储然后提交 - running,直接编辑 <running/> 数据存储。 选项
|
|
如果设置此布尔标志,则验证 选项
|
注释
注意
此模块要求在要管理的远程设备上启用 netconf 系统服务。
此模块支持具有和不具有 candidate 和 confirmed-commit 功能的设备。它将始终使用更安全的功能。
此模块支持使用 connection=netconf。
此模块在
ansible_network_os
网络平台上受支持。有关详细信息,请参见 :ref:`网络平台选项 <platform_options>`。
示例
- name: use lookup filter to provide xml configuration
ansible.netcommon.netconf_config:
content: "{{ lookup('file', './config.xml') }}"
- name: set ntp server in the device
ansible.netcommon.netconf_config:
content: |
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<system xmlns="urn:ietf:params:xml:ns:yang:ietf-system">
<ntp>
<enabled>true</enabled>
<server>
<name>ntp1</name>
<udp><address>127.0.0.1</address></udp>
</server>
</ntp>
</system>
</config>
- name: wipe ntp configuration
ansible.netcommon.netconf_config:
content: |
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<system xmlns="urn:ietf:params:xml:ns:yang:ietf-system">
<ntp>
<enabled>false</enabled>
<server operation="remove">
<name>ntp1</name>
</server>
</ntp>
</system>
</config>
- name: configure interface while providing different private key file path (for connection=netconf)
ansible.netcommon.netconf_config:
backup: true
register: backup_junos_location
vars:
ansible_private_key_file: /home/admin/.ssh/newprivatekeyfile
- name: configurable backup path
ansible.netcommon.netconf_config:
backup: true
backup_options:
filename: backup.cfg
dir_path: /home/user
- name: "configure using direct native format configuration (cisco iosxr)"
ansible.netcommon.netconf_config:
format: json
content:
{
"config":
{
"interface-configurations":
{
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
"interface-configuration":
{
"active": "act",
"description": "test for ansible Loopback999",
"interface-name": "Loopback999",
},
},
},
}
get_filter:
{
"interface-configurations":
{
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
"interface-configuration": null,
},
}
- name: "configure using json string format configuration (cisco iosxr)"
ansible.netcommon.netconf_config:
format: json
content: |
{
"config": {
"interface-configurations": {
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
"interface-configuration": {
"active": "act",
"description": "test for ansible Loopback999",
"interface-name": "Loopback999"
}
}
}
}
get_filter: |
{
"interface-configurations": {
"@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
"interface-configuration": null
}
}
# Make a round-trip interface description change, diff the before and after
# this demonstrates the use of the native display format and several utilities
# from the ansible.utils collection
- name: Define the openconfig interface filter
set_fact:
filter:
interfaces:
"@xmlns": "http://openconfig.net/yang/interfaces"
interface:
name: Ethernet2
- name: Get the pre-change config using the filter
ansible.netcommon.netconf_get:
source: running
filter: "{{ filter }}"
display: native
register: pre
- name: Update the description
ansible.utils.update_fact:
updates:
- path: pre.output.data.interfaces.interface.config.description
value: "Configured by ansible {{ 100 | random }}"
register: updated
- name: Apply the new configuration
ansible.netcommon.netconf_config:
content:
config:
interfaces: "{{ updated.pre.output.data.interfaces }}"
- name: Get the post-change config using the filter
ansible.netcommon.netconf_get:
source: running
filter: "{{ filter }}"
display: native
register: post
- name: Show the differences between the pre and post configurations
ansible.utils.fact_diff:
before: "{{ pre.output.data|ansible.utils.to_paths }}"
after: "{{ post.output.data|ansible.utils.to_paths }}"
# TASK [Show the differences between the pre and post configurations] ********
# --- before
# +++ after
# @@ -1,11 +1,11 @@
# {
# - "@time-modified": "2020-10-23T12:27:17.462332477Z",
# + "@time-modified": "2020-10-23T12:27:21.744541708Z",
# "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
# "interfaces.interface.aggregation.config['fallback-timeout']['#text']": "90",
# "interfaces.interface.aggregation.config['fallback-timeout']['@xmlns']": "http://arista.com/yang/openconfig/interfaces/augments",
# "interfaces.interface.aggregation.config['min-links']": "0",
# "interfaces.interface.aggregation['@xmlns']": "http://openconfig.net/yang/interfaces/aggregate",
# - "interfaces.interface.config.description": "Configured by ansible 56",
# + "interfaces.interface.config.description": "Configured by ansible 67",
# "interfaces.interface.config.enabled": "true",
# "interfaces.interface.config.mtu": "0",
# "interfaces.interface.config.name": "Ethernet2",
返回值
常见的返回值已记录在 此处,以下是此模块独有的字段
键 |
描述 |
---|---|
备份文件的完整路径 返回:当备份为 yes 时 示例: |
|
如果在运行时启用了 –diff 选项,则之前和之后配置更改将作为 before 和 after 键的一部分返回。 返回:启用 diff 时 示例: |
|
服务器的功能列表 返回:成功 示例: |