【问题标题】:Ansible: ios_config - want to remove a configuration line only if it existsAnsible:ios_config - 仅当配置行存在时才想删除它
【发布时间】:2019-02-26 15:59:00
【问题描述】:
如果路由器当前通过“no ip sla 46”运行,我需要删除路由器上的 IP SLA 配置,但如果路由器上当前不存在,则 playbook 失败。想法?
- name: Add IP SLA test
ios_config:
lines:
- udp-jitter 10.x.x.x source-ip {{ loopback }} codec g711ulaw
- tos 184
- tag Network Operation Center - G711ulaw EF VoIP
- frequency 180
parents: ip sla 46
before: no ip sla 46
【问题讨论】:
标签:
networking
routing
ansible
yaml
cisco
【解决方案1】:
最终使用 ios_command 检查当前的 IP SLA 配置,如果存在则删除。
- name: Find current SLA 46 config
ios_command:
commands: 'show run | inc sla 46'
register: raw_sla_46
- set_fact:
sla_46: "{{ raw_sla_46.stdout[0] }}"
- name: Delete IP SLA 46 if present
ios_config:
lines:
- no ip sla 46
when: sla_46 == 'ip sla 46'
- name: Add IP SLA from Lo0 to DC
ios_config:
lines:
- udp-jitter 10.20.0.25 17000 source-ip {{ loopback }} codec g711ulaw
- tos 184
- tag Network Operation Center - CHA - G711ulaw EF VoIP
- frequency 180
parents: ip sla 46
【解决方案2】:
我也有同样的问题 - 我想更改 NTP 服务器,但在添加我想要的服务器时为了幂等性,我想删除任何旧的/错误的服务器。在 ansible 中应该有一种方法可以替换这些行 - 我不想使用 before 子句来删除所有 ntp 配置,因为它具有破坏性的副作用。
当前配置
ntp server 10.10.10.10
想要的配置
ntp server 10.10.10.20
如果我使用 ios_config 命令为新服务器运行剧本,我最终会得到 2 个 NTP 服务器 - 这不是我想要的!