# Range function in Ansible?

**URL:** <https://forum.ansible.com/t/range-function-in-ansible/32887>\
**Category:** Ansible Project\
**Created:** [May 8, 2020, 3:49pm UTC](https://forum.ansible.com/t/range-function-in-ansible/32887 "2020-05-08T15:49:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Short](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albert\_Short](https://forum.ansible.com/u/Albert_Short)\
**Post date:** [May 8, 2020, 3:49pm UTC](https://forum.ansible.com/t/range-function-in-ansible/32887/1 "2020-05-08T15:49:14Z")

</div>

Can anyone tell me there is a solution I can use to do the same thing a Python range function in Ansible?

Scenario:  
Variables I am using

`

access\_hostname:

- name: dariusz\_access1  
nodeid\_access: 1001  
from\_port: ‘04’  
vlanid:

- 201-204,500-510  
podid: 1  
int\_mode: regular  
depl\_immediacy: lazy

- name: dariusz\_access2  
nodeid\_access: 1001  
from\_port: ‘05’  
vlanid:

- 10,1200  
podid: 1  
int\_mode: regular  
depl\_immediacy: lazy

`

What I want to achieve is to expand my ‘vlanid’ variable into a list of individual items:  
So from this

`

vlanid:

- 201-204,500-510

`

to something like this, hopefully to the use that info in loops, etc.

`

dariusz\_access1:  
vlanid:

- ‘0201’
- ‘0202’
- ‘0203’
- ‘0204’
- ‘0500’
- ‘0501’
- ‘0502’
- ‘0503’
- ‘0504’
- ‘0505’
- ‘0506’
- ‘0507’
- ‘0508’
- ‘0509’
- ‘0510’
- |-  
dariusz\_access2:  
vlanid:
- ‘0010’
- ‘1200’

`

I also need to get a leading zero ‘0’ added to each vlanid that is not 4 characters in length.

The idea is to avoid having to ask the end user to input each vlanid individually, which could take some time.

I have a python code that can split the ranges for me, with no problem, but if there was an option in Ansible, i would prefer that.

Any guidance would be great.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 8, 2020, 5:54pm UTC](https://forum.ansible.com/t/range-function-in-ansible/32887/2 "2020-05-08T17:54:36Z")

</div>

This works for me:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 8, 2020, 7:06pm UTC](https://forum.ansible.com/t/range-function-in-ansible/32887/3 "2020-05-08T19:06:17Z")

</div>

> Scenario:  
> Variables I am using  
> &nbsp;&nbsp;&nbsp;&nbsp;access\_hostname:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: dariusz\_access1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodeid\_access: 1001  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from\_port: '04'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vlanid:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 201-204,500-510  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;podid: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int\_mode: regular  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;depl\_immediacy: lazy
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: dariusz\_access2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nodeid\_access: 1001  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from\_port: '05'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vlanid:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 10,1200  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;podid: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int\_mode: regular  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;depl\_immediacy: lazy
> 
> What I want to achieve is to expand my 'vlanid' variable into a list of  
> individual items:

It's possible to create "filter\_plugins"  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_plugins.html#filter-plugins](https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#filter-plugins)

For example

&nbsp;&nbsp;\> cat filter\_plugins/my\_filters.py  
&nbsp;&nbsp;def my\_range(s):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interval = str(s).split("-")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start = int(interval[0])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(interval) \> 1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stop = int(interval[1]) + 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stop = start + 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return [i for i in range(start,stop)]

&nbsp;&nbsp;def my\_split(s):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return str(s).split(",")

&nbsp;&nbsp;class FilterModule(object):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def filters(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'my\_range': my\_range,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'my\_split': my\_split  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Then this task does the job

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.vlanid|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('my\_split')|flatten|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('my\_range')|flatten }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ access\_hostname }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Albert\_Short](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Albert\_Short](https://forum.ansible.com/u/Albert_Short)\
**Post date:** [May 11, 2020, 7:41am UTC](https://forum.ansible.com/t/range-function-in-ansible/32887/4 "2020-05-11T07:41:02Z")

</div>

Dick,

thanks for that, other than the strange behavior for anything with the 8 or 9 in it not formatting correctly.

My next issue I can’t resolve is to make this loop through a ‘multi-layer’ dictionary

`

top:  
device1:  
name: test\_name1  
vlanid: 2-10,800-810  
device2:

name: test\_name2  
vlanid: 100-120

`

My brain is a bit frazzled right now but I may be looking at this from the wrong angle.

My idea, if possible is to be able to:

1. User to input ‘name:’ for each device/endpoint (‘device1:’ , ‘device2:’)
2. User input of ‘vlanid:’ for each specific device/endpoint (‘device1:’ , ‘device2:’)
3. Ansible to loop through ‘top:’
4. Use ‘name:’ input
5. Expand ‘vlanid:’
6. Build required objects in ACI, on per device/endpoint deployment  
If I can get this work once, I hope to be able to use it on other ACI object creations that are endpoint specific.

I have tried using sub\_elements in my past playbook, which works fine, but I have to input each ‘vlanid’ individually into a list

`

top:  
device1:  
name: test\_name1  
vlanid:

- ‘0002’
- ‘0003’
- ‘0004’  
…
- ‘0919’  
device2:  
name: test\_name2  
vlanid:
- ‘0100’
- ‘0101’
- ‘0102’
- ‘0103’  
…
- ‘0120’

`

Again, any assistance would be great.

Regards
