# How to remove unused/old configuration automatically using Ansible?

**URL:** <https://forum.ansible.com/t/how-to-remove-unused-old-configuration-automatically-using-ansible/27588>\
**Category:** Ansible Project\
**Created:** [May 22, 2018, 5:38am UTC](https://forum.ansible.com/t/how-to-remove-unused-old-configuration-automatically-using-ansible/27588 "2018-05-22T05:38:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SK1](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@SK1](https://forum.ansible.com/u/SK1)\
**Post date:** [May 22, 2018, 5:38am UTC](https://forum.ansible.com/t/how-to-remove-unused-old-configuration-automatically-using-ansible/27588/1 "2018-05-22T05:38:52Z")

</div>

I am currently using Ansible to generate running configuration for the network devices. Here is the simple scenario I have.

I have defined the interface type (routed, access, trunk) in the variables file and then created a jinja file that will generate the necessary configuration based on variables definition.

Below is the sample variable definition and jinja template configuration:

CONFIG:

- { id: Ethernet1/1, type: routed }
- { id: Ethernet1/2, type: access }
- { id: Ethernet1/3, type: trunk }

Jinja:

{% if int.type == ‘routed’ %}

no switchport  
{% elif int.type == ‘access’ %}  
switchport mode access  
switchport access vlan 10  
spanning-tree portfast  
{% elif int.type == ‘trunk’ %}  
switchport mode trunk  
{% endif %}

This works perfectly fine when I define the variables, generate the configuration and push it to the device. So, below is how the interface config will look like on the device after pushing the config:

interface Ethernet1/1  
no switchport  
interface Ethernet1/2  
switchport mode access  
switchport access vlan 10  
spanning-tree portfast  
interface Ethernet1/3  
switchport mode trunk

Now, I want to change Ethernet1/2 to trunk and Ethernet1/3 to access. What happens is that Ansible pushes “switchport mode trunk” to Ethernet1/2, but doesn’t remove “switchport access vlan 10” and “spanning-tree portfast” which are not needed anymore. The behavior is consistent for both ios\_config and eos\_config. I already tried the diff\_against running option, but it still doesn’t seem to work. Any suggestions on how to fix this would be truly appreciated? If you need more details around this, please let me know.
