Ansible:ios_config - 仅当配置行存在时才想删除它

Ansible: ios_config - want to remove a configuration line only if it exists

我需要删除路由器上的 IP SLA 配置,如果它当前是 运行 通过 "no ip sla 46",但是,如果路由器上当前不存在,则 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

最终使用 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

我有同样的问题 - 我想更改 NTP 服务器,但为了幂等性,当我添加我想要的服务器时,我想删除任何 old/erroroneous。在 ansible 中,应该有一种方法可以让我只替换这些行——我不想使用 before 子句删除所有 ntp 配置,因为它具有破坏性的副作用。

当前配置

ntp server 10.10.10.10

所需配置

ntp server 10.10.10.20

如果我 运行 一个带有新服务器 ios_config 命令的剧本,我最终会得到 2 个 NTP 服务器 - 这不是我想要的!