Create a variable of interfaces from an IOS switch based on the way they are configured.

Hello, I am looking for way to pull interfaces from an cisco switch and make them a variable based on they way they are currently configured so I can make configuration changes. For example if it is a trunk interface make those the variable or list so I can add another allowed vlan. Or if it is an access port and this access vlan select those ports so I can make changes.

I can pull a list of interfaces from a switch using

  • name: show interfaces

hosts: all

connection: network_cli

gather_facts: no

vars:

ansible_network_os: ios

tasks:

  • name: test

ios_facts:

gather_subset:

  • ‘min’

gather_network_resources:

  • l2_interfaces

And get this result

{

“ansible_facts”: {

“ansible_network_resources”: {

“l2_interfaces”: [

{

“name”: “GigabitEthernet0/0”

},

{

“name”: “GigabitEthernet1/0/1”,

“access”: {

“vlan”: 3456

}

},

{

“name”: “GigabitEthernet1/0/2”,

“access”: {

“vlan”: 3456

}

},

{

“name”: “GigabitEthernet1/0/3”,

“access”: {

“vlan”: 3456

}

},

{

“name”: “GigabitEthernet1/0/4”,

“access”: {

“vlan”: 3456

{

“name”: “GigabitEthernet1/0/20”,

“trunk”: {

“allowed_vlans”: [

“32”,

“1978”,

“1980”,

“3456”

]

}

},

But I am having issues find a way to select and use the interfaces that are either a trunk or access and what access vlan to select.

Any help with this would be greatly appreciated.

Thank you