# Automation with Ansible: Configuring Down Ports on a Cisco Switch

**URL:** <https://forum.ansible.com/t/automation-with-ansible-configuring-down-ports-on-a-cisco-switch/40335>\
**Category:** Get Help\
**Tags:** cisco, network\
**Created:** [February 18, 2025, 5:34pm UTC](https://forum.ansible.com/t/automation-with-ansible-configuring-down-ports-on-a-cisco-switch/40335 "2025-02-18T17:34:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielSilva](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/danielsilva/32/7611_2.png) [@DanielSilva](https://forum.ansible.com/u/DanielSilva)\
**Post date:** [February 18, 2025, 5:34pm UTC](https://forum.ansible.com/t/automation-with-ansible-configuring-down-ports-on-a-cisco-switch/40335/1 "2025-02-18T17:34:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 19, 2025, 4:08pm UTC](https://forum.ansible.com/t/automation-with-ansible-configuring-down-ports-on-a-cisco-switch/40335/2 "2025-02-19T16:08:01Z")

</div>

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](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)](https://jinja.palletsprojects.com/en/stable/templates/#jinja-filters.selectattr)

---

<div class="post-metadata">

**Author:** ![jbroillet](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbroillet/32/7548_2.png) [@jbroillet](https://forum.ansible.com/u/jbroillet)\
**Post date:** [February 21, 2025, 1:46pm UTC](https://forum.ansible.com/t/automation-with-ansible-configuring-down-ports-on-a-cisco-switch/40335/3 "2025-02-21T13:46:15Z")

</div>

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
