Configure ports based on mode trunk or access

I am a bit new to ansible, I am ok at making playbooks on static devices. What I would like to do is make a playbook that is sort of universal. I would like to add a vlan to a cisco switch then automatically add it to all the trunk ports. First what I am trying is to add vlans to all the trunk ports. I am not sure if I am doing this correctly. I want to loop all the l2_interfaces that are set to trunk and add the vlans on the switch to them as allowed vlans. I think this script is working off of a predetermined list of vlans and ports.

hosts: all
connection: network_cli
gather_facts: no
vars:
ansible_network_os: ios

tasks:

  • name: Get config for IOS devices
    ios_facts:
    gather_subset:

  • ‘!all’

  • ‘!min’
    gather_network_resources:

  • ‘l2_interfaces’

  • name: Configure L2 Trunks
    ios_l2_interfaces:
    name: “{{ item.name }}”
    mode: “{{ item.mode }}”
    trunk_allowed_vlans: “{{ vlans | map(attribute=‘vlan_id’) | join(‘,’) }}”
    state: present
    loop: “{{ l2_interfaces[ansible_net_hostname] | selectattr(‘mode’,‘equalto’,‘trunk’) | list}}”