-1

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!

New2Python
  • 325
  • 1
  • 4
  • 17

2 Answers2

0

This works:

---
- name: Iterate over numbers
  hosts: localhost
  gather_facts: no
  connection: local

  tasks:
  - debug:
      msg: Hey I am port {{ base_port + item }}
    loop: "{{ range(5)|list }}"
    vars:
      base_port: 8000

It produces the output:

PLAY [Iterate over numbers] ****************************************************

TASK [debug] *******************************************************************
ok: [localhost] => (item=0) => {
    "msg": "Hey I am port 8000"
}
ok: [localhost] => (item=1) => {
    "msg": "Hey I am port 8001"
}
ok: [localhost] => (item=2) => {
    "msg": "Hey I am port 8002"
}
ok: [localhost] => (item=3) => {
    "msg": "Hey I am port 8003"
}
ok: [localhost] => (item=4) => {
    "msg": "Hey I am port 8004"
}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Or course, you'd have to adapt it to your template, but that should be the easy part.

Alexei Z
  • 79
  • 1
  • 3
  • Thanks for the quick response. Apologies, I wasnt quite clear. I need to iterate through two lists. 1) which involves naming the config files ie prometheus.conf as well as 2) inside prometheus.conf file this increments --web.listen-address=':909'. Output for #1 is something like m19,m20,m21 and as you saw for #2 its 9090,9091,9092. Im trying to adapt your example but cant figure out how to work off of 2 lists. – New2Python Sep 16 '20 at 16:21
0

I solved my issue with with_indexed_items.

If anyone is interested in looping through a list and using the index value of each list item this what I did:

- name: "Install supervisord template for {{ role }} and notify supervisor of the change"
  template: 
    src: "supervisord.conf.j2"
    dest: "{{ supervisor_conf_dir }}/{{ role }}_{{ item.1 }}.conf"
    owner: "{{ deploy_user }}"
    group: "{{ deploy_user }}"
  with_indexed_items:
    - "{{ the_endpoints }}"
  notify:
    - "add_{{ role }}"
    - "update_{{ role }}"
  tags:
    - "additional_templates"
    - "supervisor_configs"

debug of this gives output where index.0 is the index value and index.1 is the item from the list:

item=(1, u'm19')

and use it in a template such as:

[program:{{ role }}_{{ item.1 }}]
autorestart = true
autostart = true
command = {{ opskit_dir }}/{{ role }}_{{ item.1 }}/bin/prometheus --web.external-url='https://{{ inventory_hostname }}:4434/{{ deploy_env }}-{{ role }}_{{ item.1 }}' --config.file='{{ opskit_dir }}/{{ role }}_{{ item.1 }}/conf/{{ role }}_{{ item.1 }}.yml' --storage.tsdb.path='{{ deploy_dir }}/data/{{ role }}
_{{ item.1 }}/data' --storage.tsdb.retention='365d' --log.level='debug' --web.listen-address=':{{ prometheus_internal_port|int + item.0|int }}'
directory = {{ opskit_dir }}/{{ role }}_{{ item.1 }}
redirect_stderr = true
stdout_logfile = {{ opskit_dir }}/log/{{ role }}_{{ item.1 }}.log
stdout_logfile_backups = 5
stdout_logfile_maxbytes = 10MB
stopwaitsecs = 300
New2Python
  • 325
  • 1
  • 4
  • 17