在Jinja中如何输出loop.index。

huangapple go评论76阅读模式
英文:

How to output loop.index in jinja

问题

我想要能够将loop.index的迭代输出到我的模板中,我正在使用Ansible,我有一个模板文件.j2和一个包含以下组的清单文件

[all_servers]
app01 ansible_host=10.10.1.100
app02 ansible_host=10.10.1.102
app03 ansible_host=10.10.1.103
app04 ansible_host=10.10.1.104
app05 ansible_host=10.10.1.105
app06 ansible_host=10.10.1.106

在这个文件中有一个jinja值

/kafka_certification/kafka.server01.keystore.jk

我想将这个jinja文件复制到所有节点,并且我想在 [ server0{{ loop.index }} ] 的部分使用loop.index,因为每个节点都有不同的jinja文件,例如,对于app02,是kafka.server02.keystore,对于app03,是kafka.server03.keystore,对于app04,是kafka.server04.keystore,对于app05,是kafka.server05.keystore,对于app06,是kafka.server06.keystore。循环依赖于我的清单主机组[all_servers]。

我该如何实现这个?

Ansible模板中的循环索引怎么用?

英文:

I want to be able to output the loop.index iteration to my template , i am using ansible and i have template file.j2 and inventory file have the below group

[all_servers]
app01 ansible_host=10.10.1.100
app02 ansible_host=10.10.1.102
app03 ansible_host=10.10.1.103
app04 ansible_host=10.10.1.104
app05 ansible_host=10.10.1.105
app06 ansible_host=10.10.1.106

I have jinja file inside this file value

/kafka_certification/kafka.server01.keystore.jk

> So i want copy this jinja file to all nodes and i want use loop.index
> in part of [ server0{{ loop.index }} ] because each node have
> different jinja file for example kafka.server02.keystore for
> app02 kafka.server03.keystore for app03
> kafka.server04.keystore for app04 kafka.server05.keystore for
> app05 kafka.server06.keystore for app06 .. loop depend on my
> inventory host group [all_servers]

how i can do this

ansible loop index for ansible template

答案1

得分: 1

以下是代码片段的翻译:

- hosts: all

  tasks:

    - debug:
        msg: "将 {{ file }} 复制到 {{ item }}"
      loop: "{{ ansible_play_hosts_all }}"
      loop_control:
        extended: true
      vars:
        index: "{{ '%02d' % ansible_loop.index }}"
        file: "/kafka_certification/kafka.server{{ index }}.keystore.jk"
      run_once: true
      delegate_to: localhost

结果(缩减):

  msg: 将 /kafka_certification/kafka.server01.keystore.jk 复制到 app01
  msg: 将 /kafka_certification/kafka.server02.keystore.jk 复制到 app02
  msg: 将 /kafka_certification/kafka.server03.keystore.jk 复制到 app03
  msg: 将 /kafka_certification/kafka.server04.keystore.jk 复制到 app04
  msg: 将 /kafka_certification/kafka.server05.keystore.jk 复制到 app05
  msg: 将 /kafka_certification/kafka.server06.keystore.jk 复制到 app06

下面是另一种更简单的方法,不需要循环:

- hosts: all

  tasks:

    - debug:
        msg: "将 {{ file }} 复制到 {{ inventory_hostname }}"
      vars:
        idx: "{{ ansible_play_hosts_all.index(inventory_hostname) + 1 }}"
        index: "{{ '%02d' % idx|int }}"
        file: "/kafka_certification/kafka.server{{ index }}.keystore.jk"

这个选项与Ansible的上下文相符。参见 Plays

最后,以下是问题和答案的翻译:

Q: "将文件复制到所有节点。在清单主机组 [all_servers] 上使用 loop.index"

A: 在这种特定情况下,以下Play给出了相同的结果:

- hosts: all_servers

  tasks:

    - debug:
        msg: "将 {{ file }} 复制到 {{ inventory_hostname }}"
      vars:
        idx: "{{ groups.all_servers.index(inventory_hostname) + 1 }}"
        index: "{{ '%02d' % idx|int }}"
        file: "/kafka_certification/kafka.server{{ index }}.keystore.jk"

希望这些翻译有帮助!

英文:

The play below shows how you can use the index

- hosts: all

  tasks:

    - debug:
        msg: "Copy {{ file }} to {{ item }}"
      loop: "{{ ansible_play_hosts_all }}"
      loop_control:
        extended: true
      vars:
        index: "{{ '%02d' % ansible_loop.index }}"
        file: "/kafka_certification/kafka.server{{ index }}.keystore.jk"
      run_once: true
      delegate_to: localhost

gives (abridged)

  msg: Copy /kafka_certification/kafka.server01.keystore.jk to app01
  msg: Copy /kafka_certification/kafka.server02.keystore.jk to app02
  msg: Copy /kafka_certification/kafka.server03.keystore.jk to app03
  msg: Copy /kafka_certification/kafka.server04.keystore.jk to app04
  msg: Copy /kafka_certification/kafka.server05.keystore.jk to app05
  msg: Copy /kafka_certification/kafka.server06.keystore.jk to app06

<hr>

There is a simpler approach. The play below gives the same result without iteration

- hosts: all

  tasks:

    - debug:
        msg: &quot;Copy {{ file }} to {{ inventory_hostname }}&quot;
      vars:
        idx: &quot;{{ ansible_play_hosts_all.index(inventory_hostname) + 1 }}&quot;
        index: &quot;{{ &#39;%02d&#39; % idx|int }}&quot;
        file: &quot;/kafka_certification/kafka.server{{ index }}.keystore.jk&quot;

This option complies with the Ansible context. See Plays:

> ... playbook object maps managed nodes (hosts) to tasks.

<hr>

Q: "Copy file to all nodes. Use loop.index on inventory host group [all_servers]"

A: In this particular case the play below gives the same result

- hosts: all_servers

  tasks:

    - debug:
        msg: &quot;Copy {{ file }} to {{ inventory_hostname }}&quot;
      vars:
        idx: &quot;{{ groups.all_servers.index(inventory_hostname) + 1 }}&quot;
        index: &quot;{{ &#39;%02d&#39; % idx|int }}&quot;
        file: &quot;/kafka_certification/kafka.server{{ index }}.keystore.jk&quot;

<sup>

PLAY [all_servers] ****************************************************************************

TASK [debug] **********************************************************************************
ok: [app02] =&gt; 
  msg: Copy /kafka_certification/kafka.server02.keystore.jk to app02
ok: [app01] =&gt; 
  msg: Copy /kafka_certification/kafka.server01.keystore.jk to app01
ok: [app04] =&gt; 
  msg: Copy /kafka_certification/kafka.server04.keystore.jk to app04
ok: [app03] =&gt; 
  msg: Copy /kafka_certification/kafka.server03.keystore.jk to app03
ok: [app05] =&gt; 
  msg: Copy /kafka_certification/kafka.server05.keystore.jk to app05
ok: [app06] =&gt; 
  msg: Copy /kafka_certification/kafka.server06.keystore.jk to app06

PLAY RECAP ************************************************************************************
app01: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app02: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app03: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app04: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app05: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app06: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

</sup>

huangapple
  • 本文由 发表于 2023年5月17日 15:24:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定