'Python Filter Module for Ansible returning some strings all lowercase rather than keeping original string casing

I've written a simple filter plugin for Ansible that takes 2 variables ( 1 being a list of strings and the other is just 1 string), converts the number vlan into the vlan name and returns a list of json dictionaries. Everything works as expected, but I am having 1 issue and I can't figure out what could possibly be causing it.

Here is my filter (note, the list of vlans is longer than what I've posted but didn't think I needed to post them all)

class FilterModule(object):
    '''Vlan Name Filter '''

    def filters(self):
        return {
            'allowed_tagged_filter': self.allowed_tagged_filter
        }
    
    def allowed_tagged_filter(self, vlans, sites):
        allowed_vlans = []
        for vlan in vlans[:]:
            if vlan == '2':
                allowed_vlans.append({"name": "Management", "vlan_group": f"{sites}_VLANs"})
            elif vlan == '3':
                allowed_vlans.append({"name": "Native", "vlan_group": f"{sites}_VLANs"})
            elif vlan == '4':
                allowed_vlans.append({"name": "Engineering", "vlan_group": f"{sites}_VLANs"})
            else: 
                pass
        return allowed_vlans

Here is my Ansible Playbook

---
- name: Gather Cisco Device Facts and Update Netbox
  hosts: "{{ hosts }}"
  connection: network_cli
  gather_facts: False
  vars_files:
    - secret.yml

  tasks:
  - name: Gather IOS Facts
    cisco.ios.ios_facts:
      available_network_resources: yes
      gather_subset: all
    register: ios_facts

  - name: Gather L2 Interface Info
    cisco.ios.ios_l2_interfaces:
      state: gathered
    register: ios_l2_interfaces

  - name: Update Trunk Interfaces in Netbox
    netbox.netbox.netbox_device_interface:
      netbox_url: "{{ lab2netbox.netbox_url }}"
      netbox_token: "{{ lab2netbox.netbox_token }}"
      data:
        device: "{{ inventory_hostname }}"
        name: "{{ item.name }}"
        mode: "tagged"
        untagged_vlan:
          vlan_group: "{{ sites[0] }}_VLANs"
          name: "{{ item.trunk.native_vlan | vlan_name_filter }}"
        tagged_vlans: "{{ item.trunk.allowed_vlans | allowed_tagged_filter(sites[0]) }}"
      state: present
      update_vc_child: yes
      validate_certs: no
    loop: "{{ ios_l2_interfaces.gathered }}"
    when: (item.mode is defined and item.mode == 'trunk' and item.trunk.allowed_vlans is defined)

Here's what the vlans and sites variables that get passed into the filter look like. I'm looping through all the interfaces for each device returned in the ios_l2_interfaces results and pulling the necessary values required to update the interface in my Netbox instance. The sites variable is contained in the host_var file of the device.

"item": {
    "mode": "trunk",
    "name": "GigabitEthernet4/0/48",
    "trunk": {
        "allowed_vlans": [
            "2",
            "3",
            "4"
        ],
        "native_vlan": 69
    }

"sites": [
    "NewYork"
    ]

Here is what I expect to be returned

[
    {
        "name": "Management",
        "vlan_group": "NewYork_VLANs"
    },
    {
        "name": "Native",
        "vlan_group": "NewYork_VLANs"
    },
    {
        "name": "Engineering",
        "vlan_group": "NewYork_VLANs"
    }
]

Now, as I said, this works. Except for 1 small detail. For some reason, some of the vlans in the returned list, have their vlan_group returned as all lowercase, like below

[
    {
        "name": "Management",
        "vlan_group": "newyork_vlans"
    },
    {
        "name": "Native",
        "vlan_group": "NewYork_VLANs"
    },
    {
        "name": "Engineering",
        "vlan_group": "NewYork_VLANs"
    }
]

This ultimately breaks the play and causes the tasks to fail as the vlan_group needs to match exactly. From what I can tell, it is not random and always happens on the same 5 vlans out of the 20 I have in the filter. I have removed them from the filter and re-added by copying and pasting from lines that return proper format, still happens. I've deleted the filter plugin completely and re-wrote it, but still happens.

Any idea on why this would happen and what could possibly be causing it?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source