ansible version: 2.4.2
I would like to create multiple templates with a variable the increments each time. For example I want to generate multiple prometheus config files, having every sequential port incremented by 1 (int).
So lets say I want to end up with: prometheus1.conf, prometheus2.conf, prometheus3.conf. These are all generated by prometheus.conf.j2 template. I have the file name figured out but not the count within the template itself once generated.
In my group_vars/all/vars.yml I have
prometheus_internal_port: "9090"
And my task is:
- name: "Install supervisord template for {{ role }} and notify supervisor of the change"
template:
src: "supervisord.conf.j2"
dest: "{{ supervisor_conf_dir }}/{{ role }}_{{ item }}.conf"
owner: "{{ deploy_user }}"
group: "{{ deploy_user }}"
with_items:
- "{{ the_endpoints }}"
notify:
- "add_{{ role }}"
- "update_{{ role }}"
tags:
- "additional_templates"
- "supervisor_configs"
My template (note the prometheus_internal_port|int + loop.index|int which doesnt work):
[program:{{ role }}_{{ item }}]
autorestart = true
autostart = true
command = {{ opskit_dir }}/{{ role }}_{{ item }}/bin/prometheus --web.external-url='https://{{ inventory_hostname }}:4434/{{ deploy_env }}-{{ role }}_{{ item }}' --config.file='{{ opskit_dir }}/{{ role }}_{{ item }}/conf/{{ role }}_{{ item }}.yml' --storage.tsdb.path='{{ deploy_dir }}/data/{{ role }}_{{ item }}/data' --storage.tsdb.retention='365d' --log.level='debug' --web.listen-address=':{{ prometheus_internal_port|int + loop.index|int }}'
directory = {{ opskit_dir }}//{{ role }}_{{ item }}
redirect_stderr = true
stdout_logfile = {{ opskit_dir }}/log/{{ role }}_{{ item }}.log
stdout_logfile_backups = 5
stdout_logfile_maxbytes = 10MB
stopwaitsecs = 300
Now what I need is the variable inside the generated configs to increment so:
prometheus1.conf has
... --web.listen-address=':9090' ...
prometheus2.conf has
... --web.listen-address=':9091' ...
prometheus3.conf has
... --web.listen-address=':9092' ...
Thanks in advance!