Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops ansible创建多个模板,每个模板都有递增的var_Loops_Counter_Ansible Template - Fatal编程技术网

Loops ansible创建多个模板,每个模板都有递增的var

Loops ansible创建多个模板,每个模板都有递增的var,loops,counter,ansible-template,Loops,Counter,Ansible Template,ansible版本:2.4.2 我想用一个变量创建多个模板,每次递增。例如,我想生成多个prometheus配置文件,使每个顺序端口递增1(int) 让我们假设我想以:prometheus1.conf,prometheus2.conf,prometheus3.conf结束。这些都是由prometheus.conf.j2模板生成的。我已经计算出了文件名,但没有计算出生成后模板本身的数量 在我的小组里_vars/all/vars.yml我有 prometheus_internal_port: &qu

ansible版本:2.4.2

我想用一个变量创建多个模板,每次递增。例如,我想生成多个prometheus配置文件,使每个顺序端口递增1(int)

让我们假设我想以:prometheus1.conf,prometheus2.conf,prometheus3.conf结束。这些都是由prometheus.conf.j2模板生成的。我已经计算出了文件名,但没有计算出生成后模板本身的数量

在我的小组里_vars/all/vars.yml我有

prometheus_internal_port: "9090"
我的任务是:

- 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"
我的模板(注意prometheus|u internal|u port | int+loop.index | int不起作用):

现在我需要的是生成的配置中的变量,以便增加:

普罗米修斯1.conf已经

。。。 --web.listen地址=':9090'

普罗米修斯2.conf已经

。。。 --web.listen地址=':9091'

普罗米修斯3.conf已经

。。。 --web.listen地址=':9092'

提前谢谢

这是有效的:

---
- 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
它产生以下输出:

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

当然,您必须使其适应您的模板,但这应该是最简单的部分。

我使用索引项解决了我的问题

如果有人有兴趣在列表中循环使用每个列表项的索引值,我会这样做:

- 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"
对此的调试将给出输出,其中index.0是索引值,index.1是列表中的项:

项目=(1,u'm19')

并在模板中使用它,例如:

[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

谢谢你的快速回复。对不起,我不太清楚。我需要遍历两个列表。1) 这涉及到命名配置文件ie prometheus.conf以及prometheus.conf文件中的2)这个增量--web.listen address=':909'。#1的输出类似于m19、m20、m21,正如您看到的#2的90919092。我试图修改你的例子,但是我不知道如何处理这两个列表。
[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