# Configure ports based on mode trunk or access

**URL:** <https://forum.ansible.com/t/configure-ports-based-on-mode-trunk-or-access/33962>\
**Category:** Ansible Project\
**Created:** [October 5, 2020, 8:33pm UTC](https://forum.ansible.com/t/configure-ports-based-on-mode-trunk-or-access/33962 "2020-10-05T20:33:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Lucht](https://avatars.discourse-cdn.com/v4/letter/r/a8b319/32.png) [@Richard\_Lucht](https://forum.ansible.com/u/Richard_Lucht)\
**Post date:** [October 5, 2020, 8:33pm UTC](https://forum.ansible.com/t/configure-ports-based-on-mode-trunk-or-access/33962/1 "2020-10-05T20:33:13Z")

</div>

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}}”
