Reset for Terraform
Configuration Session

Now that you have explored the Configuration Session workflow — from full config replace to deploying and rolling back individual changes — you will reset the staging fabric in preparation for the next section: Terraform. Terraform takes a fully declarative approach to managing switch configuration, so it needs to start from a clean baseline. This step uses Ansible to remove the overlay configuration objects (VLANs, VRFs, SVIs, BGP, OSPF, etc.) from the staging fabric, leaving only the base connectivity in place.


Step 1 - Create Ansible Playbook to Reset the Staging Fabric

Return to your VSCode window.

This playbook removes the overlay configuration from the staging fabric so Terraform can start with a clean slate in the next section.

Like prior sections of the lab, we're making use of inline Jinja2 templating to generate the configuration for the Ansible playbook to send in one configuration transaction to the switches to optimize the execution time.


touch /home/pod19/workspace/nxapilab/ansible-nxos/reset.yml  
cat << EOF > /home/pod19/workspace/nxapilab/ansible-nxos/reset.yml 
---

- name: Reset Staging Fabric
  hosts: nxos
  gather_facts: false

  tasks:
    - name: Remove VRFs
      cisco.nxos.nxos_vrf:
        aggregate: >-
          {%- set vrf_list = [] -%}
          {%- for vrf in vrfs | default([]) if vrf.name != 'management' -%}
          {%- set _ = vrf_list.append(dict(name=vrf.name )) -%}
          {%- endfor -%}
          {{ vrf_list }}
        state: absent

    - name: Remove SVIs
      cisco.nxos.nxos_interfaces:
        config: >-
          {%- set svi_list = [] -%}
          {%- for svi in vrfs | default([]) + networks  | default([]) if svi.name != 'management' -%}
          {%- set _ = svi_list.append(dict(name="Vlan" + svi.vlan_id | string )) -%}
          {%- endfor -%}
          {%- set loopback_list = [] -%}
          {%- for loopback in loopback_interfaces | default([]) -%}
          {%- set _ = loopback_list.append(dict(name=loopback.name)) -%}
          {%- endfor -%}
          {{ svi_list + loopback_list }}
        state: purged

    - name: Remove NVE Interface
      cisco.nxos.nxos_interfaces:
        config: [name: nve1]
        state: purged

    - name: Remove VLANs
      cisco.nxos.nxos_vlans:
        config: >-
          {%- set vlan_list = [] -%}
          {%- for vlan in vrfs | default([]) + networks  | default([]) if vlan.name != 'management' -%}
          {%- set _ = vlan_list.append(dict(vlan_id=vlan.vlan_id)) -%}
          {%- endfor -%}
          {{ vlan_list }}
        state: deleted
      when:
        - vrfs is defined and vrfs is iterable
        - networks is defined and networks is iterable

    - name: Default Interfaces
      cisco.nxos.nxos_interfaces:
        config: >-
          {%- set physical_interface_list = [] -%}
          {%- for interface in layer3_physical_interfaces | default([]) -%}
          {%- set _ = physical_interface_list.append(dict(name=interface.name)) -%}
          {%- endfor -%}
          {{ physical_interface_list }}
        state: deleted

    - name: Remove All BGP
      cisco.nxos.nxos_bgp_global:
        state: purged

    - name: Remove OSPF Process
      cisco.nxos.nxos_ospfv2:
        config:
          processes:
            - process_id: UNDERLAY
        state: deleted

    - name: Remove Features
      cisco.nxos.nxos_feature:
        feature: "{{ item }}"
        state: disabled
      loop: "{{ features | reject('search', 'netconf') | reject('search', 'restconf') | reject('search', 'nxapi') | list }}"

EOF


Step 2 - Execute Ansible Playbook to Reset the Staging Fabric

From the root ansible project directory execute the following command.


cd /home/pod19/workspace/nxapilab/ansible-nxos


ansible-playbook -i staging.yml reset.yml

Upon a successful run of the playbook your output should look as follows:

  PLAY [Reset Staging Fabric] ************************************************************

  TASK [Remove VRFs] *********************************************************************
  ok: [staging-spine1]
  ok: [staging-spine2]
  changed: [staging-leaf1]
  changed: [staging-leaf2]
  changed: [staging-leaf3]

  TASK [Remove SVIs] *********************************************************************
  changed: [staging-spine1]
  changed: [staging-spine2]
  changed: [staging-leaf1]
  changed: [staging-leaf2]
  changed: [staging-leaf3]

  TASK [Remove NVE Interface] ************************************************************
  ok: [staging-spine1]
  ok: [staging-spine2]
  changed: [staging-leaf3]
  changed: [staging-leaf2]
  changed: [staging-leaf1]

  TASK [Remove VLANs] ********************************************************************
  skipping: [staging-spine1]
  skipping: [staging-spine2]
  changed: [staging-leaf1]
  changed: [staging-leaf2]
  changed: [staging-leaf3]

  TASK [Default Interfaces] **************************************************************
  changed: [staging-leaf2]
  changed: [staging-leaf3]
  changed: [staging-leaf1]
  changed: [staging-spine1]
  changed: [staging-spine2]

  TASK [Remove All BGP] ******************************************************************
  changed: [staging-leaf1]
  changed: [staging-spine2]
  changed: [staging-leaf3]
  changed: [staging-spine1]
  changed: [staging-leaf2]

  TASK [Remove OSPF Process] *************************************************************
  changed: [staging-leaf1]
  changed: [staging-leaf2]
  changed: [staging-spine1]
  changed: [staging-leaf3]
  changed: [staging-spine2]

  TASK [Remove Features] *****************************************************************
  changed: [staging-spine1] => (item=ospf)
  changed: [staging-leaf1] => (item=ospf)
  changed: [staging-leaf2] => (item=ospf)
  changed: [staging-spine2] => (item=ospf)
  changed: [staging-leaf3] => (item=ospf)
  changed: [staging-spine1] => (item=pim)
  changed: [staging-leaf1] => (item=pim)
  changed: [staging-leaf2] => (item=pim)
  changed: [staging-leaf3] => (item=pim)
  changed: [staging-spine2] => (item=pim)
  changed: [staging-spine1] => (item=bgp)
  changed: [staging-leaf1] => (item=bgp)
  changed: [staging-leaf2] => (item=bgp)
  changed: [staging-leaf3] => (item=bgp)
  changed: [staging-spine2] => (item=bgp)
  changed: [staging-spine1] => (item=nv overlay)
  changed: [staging-leaf1] => (item=nv overlay)
  changed: [staging-leaf2] => (item=nv overlay)
  changed: [staging-leaf3] => (item=nv overlay)
  changed: [staging-spine2] => (item=nv overlay)
  changed: [staging-leaf1] => (item=vn-segment-vlan-based)
  changed: [staging-leaf3] => (item=vn-segment-vlan-based)
  changed: [staging-leaf2] => (item=vn-segment-vlan-based)
  changed: [staging-leaf1] => (item=interface-vlan)
  changed: [staging-leaf3] => (item=interface-vlan)
  changed: [staging-leaf2] => (item=interface-vlan)

  PLAY RECAP *****************************************************************************
  staging-leaf1              : ok=8    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  staging-leaf2              : ok=8    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  staging-leaf3              : ok=8    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
  staging-spine1             : ok=7    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  staging-spine2             : ok=7    changed=5    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
  

Examine the output above. The configuration for the VLANs, VRFs, SVIs, NVE interfaces, BGP, OSPF, and features have all been removed.