Skip to content

KeyError when setting port state to disabled with community.sonic.sonic_interface_port #17

@cablesquirrel

Description

@cablesquirrel
SUMMARY

When running the community.sonic.sonic_interface_port task on a SONiC device with a defaulted configuration, a Python error is produced if a port's enabled state is set to false.

The same occurs if a port was previously up/enabled and the enabled state is changed to false. If a subsequent run of the task is performed with the port already disabled, an error will be thrown.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

community.sonic.sonic_interface_port

ANSIBLE VERSION
ansible [core 2.19.0]
  config file = None
  configured module search path = ['/home/pi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/pi/.local/pipx/venvs/ansible-navigator/lib/python3.13/site-packages/ansible
  ansible collection location = /home/pi/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/pi/.local/bin/ansible
  python version = 3.13.7 (main, Aug 17 2025, 23:03:57) [GCC 9.4.0] (/home/pi/.local/pipx/venvs/ansible-navigator/bin/python)
  jinja version = 3.1.6
  pyyaml version = 6.0.2 (with libyaml v0.2.5)
COLLECTION VERSION
Collection      Version
--------------- -------
community.sonic 0.3.1  
CONFIGURATION
CONFIG_FILE() = None

GALAXY_SERVERS:
OS / ENVIRONMENT

Ansible running on Ubuntu 20.04

Network device: DellEMC-N3248TE

SONiC Software Version: SONiC.202505.0-2a4abe0b9
SONiC OS Version: 12
Distribution: Debian 12.11
Kernel: 6.1.0-29-2-amd64
Build commit: 2a4abe0b9
STEPS TO REPRODUCE

The following setup fails on a defaulted/out-of-box config.

If all ports are currently enabled, the setup will fail on a second run when it attempts to disable an already disabled port.

    - name: Configure physical ports
      community.sonic.sonic_interface_port:
        interface: "{{ item.name }}"
        description: "{{ item.description | default(omit) }}"
        enabled: "{{ item.enabled | default(true) }}"
        speed: "{{ item.speed | default(omit) }}"
      loop: "{{ ports }}"

Host vars:

---
ports:
  - name: Ethernet0
    enabled: true
  - name: Ethernet1
    enabled: true
  ...
  - name: Ethernet48
    description: Uplink to core
    enabled: true
    speed: "1000"
  - name: Ethernet49
    enabled: false
  - name: Ethernet50
    enabled: false
  - name: Ethernet51
    enabled: false
EXPECTED RESULTS

Expected successful run of play

ACTUAL RESULTS

When the task attempts to set a state of enabled: false, the below error is thrown.

Traceback (most recent call last):
  File \"/home/admin/.ansible/tmp/ansible-tmp-1755728748.751267-39-150064190548930/AnsiballZ_sonic_interface_port.py\", line 107, in <module>
    _ansiballz_main()
  File \"/home/admin/.ansible/tmp/ansible-tmp-1755728748.751267-39-150064190548930/AnsiballZ_sonic_interface_port.py\", line 99, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File \"/home/admin/.ansible/tmp/ansible-tmp-1755728748.751267-39-150064190548930/AnsiballZ_sonic_interface_port.py\", line 47, in invoke_module
    runpy.run_module(mod_name='ansible_collections.community.sonic.plugins.modules.sonic_interface_port', init_globals=dict(_module_fqn='ansible_collections.community.sonic.plugins.modules.sonic_interface_port', _modlib_path=modlib_path),
  File \"<frozen runpy>\", line 226, in run_module
  File \"<frozen runpy>\", line 98, in _run_module_code
  File \"<frozen runpy>\", line 88, in _run_code
  File \"/tmp/ansible_community.sonic.sonic_interface_port_payload_rzoy7wuu/ansible_community.sonic.sonic_interface_port_payload.zip/ansible_collections/community/sonic/plugins/modules/sonic_interface_port.py\", line 277, in <module>
  File \"/tmp/ansible_community.sonic.sonic_interface_port_payload_rzoy7wuu/ansible_community.sonic.sonic_interface_port_payload.zip/ansible_collections/community/sonic/plugins/modules/sonic_interface_port.py\", line 273, in main
  File \"/tmp/ansible_community.sonic.sonic_interface_port_payload_rzoy7wuu/ansible_community.sonic.sonic_interface_port_payload.zip/ansible_collections/community/sonic/plugins/modules/sonic_interface_port.py\", line 247, in run_module
  File \"/tmp/ansible_community.sonic.sonic_interface_port_payload_rzoy7wuu/ansible_community.sonic.sonic_interface_port_payload.zip/ansible_collections/community/sonic/plugins/modules/sonic_interface_port.py\", line 190, in mutate_state
KeyError: 'admin_status'

The problem appears to stem from the below line:

del new_state['admin_status']

The admin_status key is not checked to ensure it exists before attempting to delete. This is resulting in a KeyError under the above mentioned conditions (defaulted config or subsequent runs with disabled ports)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions