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

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

References:

Loop 1…26 → https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#id20
Loop over files in a directory - > https://docs.ansible.com/ansible/2.4/playbooks_loops.html#id17
template module → 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 :slight_smile: