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.
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?
I’m also a still a newb at this, but I would write a playbook that:
Runs through and configures everything in your host_vars files.
Pulls all interfaces from the switch and stores them in a list.
Loop over the list
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.