Automation with Ansible: Configuring Down Ports on a Cisco Switch

Good afternoon, everyone.

I need to automate a Cisco switch using Ansible to scan all ports and apply a specific configuration to those that are down and do not have a transceiver. For example, I need to add these ports to VLAN 800 (creating it if it doesn’t exist) and set them to trunk mode.

Can anyone help me with this? My biggest challenge is not creating the VLAN but making Ansible automatically configure a down port that does not have a transceiver. This is important because some ports here are down but still have a transceiver connected with a client’s fiber link.

Could someone assist me with this request?

You could get started with the facts module for IOS, that should give you the interface list you need, along with its status.

https://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_facts_module.html#ansible-collections-cisco-ios-ios-facts-module

From there, you can filter the list of interfaces using something like selectattr so that you can then loop over the ones that are down and don’ t have a transceiver. Template Designer Documentation — Jinja Documentation (3.1.x)

Hello,

Try this:

- name: Gather IOS facts
  ios_facts:
    gather_subset: all

- name: Filter interfaces with SFP modules
  set_fact:
    sfp_interfaces: >-
      {{
        ansible_net_interfaces | dict2items 
        | selectattr('value.mediatype', 'search', 'unknown') 
        | selectattr('value.operstatus', 'equalto', 'down') 
        | map(attribute='key') | list
      }}

- name: Display SFP interfaces
  debug:
    msg: "Interfaces with SFP modules: {{ sfp_interfaces }}"

When no sfp inserted mediatype should be unknown.

Regards