network-programmingroutesansibleyamlcisco

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


I need to remove the IP SLA configuration on the router if it is currently running via "no ip sla 46" but, if it doesn't currently exist on the router, the playbook fails. Ideas?

    - 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


Solution

  • Ended up using ios_command to check for a current IP SLA config and delete if present.

    - 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