<p>看起来Ansible中并没有任何本机功能可以实现这一点</p>
<p>也就是说,为了实现这一点,您可以使用一点计算和一个<a href="https://jinja.palletsprojects.com/en/2.11.x/templates/#math" rel="nofollow noreferrer">modulo</a>与循环<a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#tracking-progress-through-a-loop-with-index-var" rel="nofollow noreferrer">^{<cd1>}</a>耦合</p>
<p>根据剧本:</p>
<pre class="lang-yaml prettyprint-override"><code>- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ item }} {{ wl_hostname[idx % wl_hostname | length] }}"
loop: "{{ gs_hostname }}"
loop_control:
index_var: idx
vars:
gs_hostname:
- gs01
- gs02
- gs03
- gs04
wl_hostname:
- wl01
- wl02
</code></pre>
<p>这将作为输出提供:</p>
<pre><code>PLAY [all] ********************************************************************************************************
TASK [debug] ******************************************************************************************************
ok: [localhost] => (item=gs01) => {
"msg": "gs01 wl01"
}
ok: [localhost] => (item=gs02) => {
"msg": "gs02 wl02"
}
ok: [localhost] => (item=gs03) => {
"msg": "gs03 wl01"
}
ok: [localhost] => (item=gs04) => {
"msg": "gs04 wl02"
}
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</code></pre>