Find VLAN in a range of VLAN (930-932)

26 views
Skip to first unread message

Spiro Mitsialis

unread,
Aug 11, 2020, 3:51:35 PM8/11/20
to Ansible Project
Using various methods, I can get a list of VLAN on a switch port.  The list looks like the following:
"vlans": "1,134,170,175,540,605,819-820,911-912,930-932,945,952,955,959-960,1200-1201,1400"

I would like to check to see if VLAN 931 is in the list.  In this case its between 930-932 in the list.  Is there a filter or function that I can use to see if its in the list?
Is there some other way I can get this info?

For a single VLAN number I can use when: 'item in vlans" if in a loop but 'in" looks for exact match.

Mauricio Tavares

unread,
Aug 11, 2020, 4:14:23 PM8/11/20
to ansible...@googlegroups.com
It is probably happening because your switch does not return a
nice list. 911-912 is a single entry in your list. I would first
convert entries like 930-932 into 930,931,932.

> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7fbfa960-ecea-46a7-b3b1-660f56f912dbo%40googlegroups.com.

Dick Visser

unread,
Aug 12, 2020, 4:13:20 AM8/12/20
to ansible...@googlegroups.com
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#network-vlan-filters
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7fbfa960-ecea-46a7-b3b1-660f56f912dbo%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dick Visser

unread,
Aug 12, 2020, 5:04:03 AM8/12/20
to ansible...@googlegroups.com
You can create a "real" list of all the vlans that this range-list contains.
Once that is done it's trivial to do your check:


---
- hosts: localhost
connection: local
gather_facts: no

vars:
vlan_range:
"1,134,170,175,540,605,819-820,911-912,930-932,945,952,955,959-960,1200-1201,1400"

tasks:
- set_fact:
vlans: "{{ vlans|default([]) |
union(lookup('sequence', item.split('-')|first + '-' +
item.split('-')|last + ':%04d', wantlist=True ) |
map('int')|list) }}"
loop: "{{ vlan_range.split(',') }}"

- debug: var=vlans

- debug:
msg: "Found!"
when: "931 in vlans"
Reply all
Reply to author
Forward
0 new messages