如果它已经在使用 Ansible 的网络交换机的 运行 配置(在变量中声明)中,如何跳过或忽略 j2 模板中的配置?
How to skip or ignore a config within j2 template if it's already in the running config (declared in a variable) of a Network Switch using Ansible?
运行-交换机上的配置:
switch-1#
interface Ethernet1/1
ip dhcp relay address 1.1.1.2
j2 模板:
{% for ip in dhcp_servers %}
interface Ethernet1/1
ip dhcp relay address {{ ip }}
{% endfor %}
定义的变量:
dhcp_servers:
- 1.1.1.2
- 1.1.1.3
- 1.1.1.4
使用 j2 创建的预期配置,稍后将添加到交换机上:
interface Ethernet1/1
ip dhcp relay address 1.1.1.3
ip dhcp relay address 1.1.1.4
仅创建 运行 配置中不存在的配置,无需从变量列表中删除 ip (dhcp_servers)
从“运行-交换机上的配置”解析当前 DHCP 服务器的列表。例如
dhcp_servers_present: [1.1.1.2]
然后是下面的任务
- debug:
msg: |
interface Ethernet1/1
{% for ip in dhcp_servers|difference(dhcp_servers_present) %}
ip dhcp relay address {{ ip }}
{% endfor %}
给出预期的配置
msg: |-
interface Ethernet1/1
ip dhcp relay address 1.1.1.3
ip dhcp relay address 1.1.1.4
运行-交换机上的配置:
switch-1#
interface Ethernet1/1
ip dhcp relay address 1.1.1.2
j2 模板:
{% for ip in dhcp_servers %}
interface Ethernet1/1
ip dhcp relay address {{ ip }}
{% endfor %}
定义的变量:
dhcp_servers:
- 1.1.1.2
- 1.1.1.3
- 1.1.1.4
使用 j2 创建的预期配置,稍后将添加到交换机上:
interface Ethernet1/1
ip dhcp relay address 1.1.1.3
ip dhcp relay address 1.1.1.4
仅创建 运行 配置中不存在的配置,无需从变量列表中删除 ip (dhcp_servers)
从“运行-交换机上的配置”解析当前 DHCP 服务器的列表。例如
dhcp_servers_present: [1.1.1.2]
然后是下面的任务
- debug:
msg: |
interface Ethernet1/1
{% for ip in dhcp_servers|difference(dhcp_servers_present) %}
ip dhcp relay address {{ ip }}
{% endfor %}
给出预期的配置
msg: |-
interface Ethernet1/1
ip dhcp relay address 1.1.1.3
ip dhcp relay address 1.1.1.4