Delete interfaces with "cisco.ios.ios_interfaces" collection

Hello!

Newbie question - I’m using “cisco.ios.ios_interfaces” collection to set parameters to L2 and L3 interfaces of a switch. I have the variables stored in a separate file in the host_vars folders of every device.

- name: Configuring port's descriptions and state
  cisco.ios.ios_interfaces:
    config:
      - name: "{{ item.name }}"
        description: "{{ item.description }}"
        enabled: "{{ item.enabled }}"
    state: replaced
  loop: "{{ interfaces }}"

It’s working fine, but in some cases I need to delete VLAN interfaces. I want to delete the interface from the YAML file with the details, and I want that to be reflected with a playbook. With the code above, the deleted interfaces are omitted since there is no more information about them, and they stay configured on the device.

Basically, I want only the interfaces present in the YAML file to be configured on the devices. How can I achieve that?

Thank you!

I’m also a still a newb at this, but I would write a playbook that:

  1. Runs through and configures everything in your host_vars files.
  2. Pulls all interfaces from the switch and stores them in a list.
  3. Loop over the list
  4. For each interface in the list that is not configured in your host_vars file, configure the interface with “state: absent”, or use the ios_command module to delete the interface.

In your hosts_vars file, structure it in a way that all of your configured interfaces are in a list that you can easily search from a playbook.

Sample host_vars file:

# Filename: host_vars/switch01
- interfaces:
  - GigabitEthernet0/0/1:
    name: GigabitEthernet0/0/1
    description: My Trunk Interface
    mode: trunk
  - GigabitEthernet0/0/2:
    name: GigabitEthernet0/0/2
    description: My Access Interface
    mode: access
  - Vlan100:
    name: Vlan100
    description: My L3 SVI
    ip_address: 10.10.10.1
    subnet: 255.255.255.0

Sample playbook:

# Filename: pb_switch_interfaces.yml
---
- name: CONFIGURED SWITCH INTERFACES
  connection: local
  hosts: switch01
  gather_facts: no
  # Whatever other items you need for this to run in your environment

  tasks:
    - name: CONFIGURE INTERFACES
      cisco.ios.interace:
        name: "{{ item.name }}"
        description: "{{ item.description }}"
        mode: "{{ item.trunk }}"
        state: present
      loop: "{{ interfaces }}"

  - name: GET VLAN INTERFACE LIST FROM SWITCH
    cisco.ios.ios_command:
      commands: "show interfaces | i Vlan"
    register: vlan_interfaces

  - name: DELETE CONFIGURED VLANS THAT ARE NOT DEFINED IN HOST_VARS
    cisco.ios.interface:
      name: "{{ item.name }}"
      state: absent
    when: item not in interfaces
    loop: "{{ vlan_interfaces }}"

Now, the actual modules you would use in your playbook are different. I’m not fully aware of the modules available in the cisco.ios collection. For instance, my first task uses “cisco.ios.interface”, but I have no clue if that’s a real module. I just used that as an example to demonstrate my though process.

The last task uses a loop to cycle through all of the discovered VLAN Interfaces and a “when” to only execute if the discovered vlan is not found in the host_vars’ interfaces list.

You will need to tweak the loop value in the last command because the output of “ios_command” doesn’t magically make a list. So, you’ll need to learn how to extract the vlan names from the output and store them in a list.

2 Likes

Thank you, @Dustin ! I’ve managed to complete my goal with your logic.

1 Like

@ichi I’m glad it worked out for you!

I’m still new around here. Please mark my answer as the solution. I need that social credit. :slight_smile:

1 Like