为每个循环增加一个值

Increment a value for each loop

我想在循环中为神社模板中变量中的每个项目增加一个整数值

这是我的变量:

variable: {"config1", "config2"}

这是我用来复制模板的任务

    - name: Templating 
      vars:
        offset: 1000
      template:
        src: "/var/opt/file.j2"
        dest: "/var/opt/file"
      with_list: "{{ variable }}"
      loop_control:
        index_var: index

这是我的 file.j2 模板

{% for item in variable %}
  configuration_{{ item }}:
    value:
    - {{ index + offset }}
    path:
    - /var/opt/{{ item }}
{% endfor %}

我想为列表变量中的每一项获取如下内容:

 configuration_config1:
    value:
    - 100
    path:
    - /var/opt/config1
 configuration_config2:
    value:
    - 101
    path:
    - /var/opt/config2

但是我目前的实现并没有给出预期的结果。

如果你想在同一个模板文件中有几行,你不应该在你的任务中循环,而应该只在你的模板中循环。

在这种情况下,您应该参考Jinja2 for structure documentation并使用循环内可用的相应特殊变量。

给定以下 file.j2 模板

{% for item in my_var %}
configuration_{{ item }}:
  value:
    - {{ loop.index + offset | default(0) | int }}
  path:
    - /var/opt/{{ item }}
{% endfor %}

以及以下 play.yml 剧本

---
- hosts: localhost
  gather_facts: false

  vars:
    my_var:
      - aconfig
      - otherconfig
      - lastconfig

    offset: 100

  tasks:

    - name: show the template result as a debug output
      debug:
        msg: "{{ lookup('template', 'file.j2').split('\n') }}"

    - name: actually write the output file on disk
      template:
        src: file.j2
        dest: /tmp/trash_it.yaml

我们得到:

# Run the playbook with configured offset in vars
$ ansible-playbook play.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [show the template result as a debug output] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "configuration_aconfig:",
        "  value:",
        "    - 101",
        "  path:",
        "    - /var/opt/aconfig",
        "configuration_otherconfig:",
        "  value:", 
        "    - 102",
        "  path:",
        "    - /var/opt/otherconfig",
        "configuration_lastconfig:",
        "  value:",
        "    - 103",
        "  path:",
        "    - /var/opt/lastconfig",
        ""
    ]
}

TASK [actually write the output file on disk] *********************************************************************************************************************************************************************
changed: [localhost]

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

# Demo it works for any value overriding the offset in an extra var
$ ansible-playbook play.yml -e offset=583

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [show the template result as a debug output] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "configuration_aconfig:",
        "  value:",
        "    - 584",
        "  path:",
        "    - /var/opt/aconfig",
        "configuration_otherconfig:",
        "  value:", 
        "    - 584",
        "  path:",
        "    - /var/opt/otherconfig",
        "configuration_lastconfig:",
        "  value:",
        "    - 585",
        "  path:",
        "    - /var/opt/lastconfig",
        ""
    ]
}

TASK [actually write the output file on disk] *********************************************************************************************************************************************************************
changed: [localhost]

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