# Loop j2 template with numerical range and run command for each change iteration of .j2

**URL:** <https://forum.ansible.com/t/loop-j2-template-with-numerical-range-and-run-command-for-each-change-iteration-of-j2/28809>\
**Category:** Ansible Project\
**Created:** [October 17, 2018, 12:03pm UTC](https://forum.ansible.com/t/loop-j2-template-with-numerical-range-and-run-command-for-each-change-iteration-of-j2/28809 "2018-10-17T12:03:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_Schmitz](https://avatars.discourse-cdn.com/v4/letter/j/d9b06d/32.png) [@Justin\_Schmitz](https://forum.ansible.com/u/Justin_Schmitz)\
**Post date:** [October 17, 2018, 12:03pm UTC](https://forum.ansible.com/t/loop-j2-template-with-numerical-range-and-run-command-for-each-change-iteration-of-j2/28809/1 "2018-10-17T12:03:26Z")

</div>

I am installing SQL2016 with Ansible. This is easy enough to do.

I have a config file (Configurationfile.ini) that SQL uses to specify all the options for installation via the command line

I now have to install named instances of SQL server on the same server (basically multiple SQL instances on the same SQL server)

This requires that I change iterate through specific values within a j2 template and for each change of the j2 template, to run the command to install the sql instance again.

My question is:

- how do I loop through a j2 template and change a range of values 01 → 26 in specific places
- On each change of the template, run a win\_command to install the SQL named instance

---

<div class="post-metadata">

**Author:** ![bill\_paxton](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bill\_paxton](https://forum.ansible.com/u/bill_paxton)\
**Post date:** [October 17, 2018, 9:35pm UTC](https://forum.ansible.com/t/loop-j2-template-with-numerical-range-and-run-command-for-each-change-iteration-of-j2/28809/2 "2018-10-17T21:35:43Z")

</div>

References:

> Loop 1..26 → [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#id20](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#id20)  
> Loop over files in a directory - \> [Ansible Documentation — Ansible Community Documentation](https://docs.ansible.com/ansible/2.4/playbooks_loops.html#id17)  
> template module → [Ansible collections | Ansible documentation](https://docs.ansible.com/ansible/latest/modules/template_module.html)

Maybe two steps?

1. Create 26 .ini files in some\_dir, each numbered 1..26
2. Loop back over those 26 files with the win\_command.

`

- name: template files 1 to 26

# use the var {{ sql\_instance }} inside your .j2 template where ever you want the values 1..26 to appear.

template:  
src: /mytemplates/sql\_config.j2  
dest: “/some\_dir/sql\_config\_{{ sql\_instance }}.ini”  
loop: “{{ range\*\*(\*\*1, 26) }}”  
loop\_control:  
loop\_var: sql\_instance

- name: win\_command with all 26 ini files  
win\_command: “sql\_server\_install.exe {{ ini\_item }}”  
with\_fileglob:
- “/some\_dir/\*”  
loop:  
loop\_control:  
loop\_var: ini\_item

`

Full discloser:

1. I don’t know what you run to install + specifiy the .ini file name at the commandline, so modify the pattern win\_command pattern to see if it gets you close, or at least gives you some ideas

2. Code not tested, so buy beware 🙂
