ansible嵌套循环,外部循环作为dict,内部循环作为dict项的值

ansible嵌套循环,外部循环作为dict,内部循环作为dict项的值,ansible,ansible-playbook,ansible-2.x,Ansible,Ansible Playbook,Ansible 2.x,我有一个简单的dict,其中key是名称,value是整数值。我希望我的外循环在dict上迭代,然后内循环在该值的次数上迭代 - hosts: localhost tasks: - debug: msg="region {{ item.key }} value {{ item.value }}" with_subelements: - "{{ objs }}" - "{{ item.value }}" vars: objs: amr

我有一个简单的dict,其中key是名称,value是整数值。我希望我的外循环在dict上迭代,然后内循环在该值的次数上迭代

- hosts: localhost
  tasks:
  - debug: msg="region {{ item.key }} value {{ item.value }}"
    with_subelements:
      - "{{ objs }}"
      - "{{ item.value }}"
  vars:
    objs:
      amrs: 3
      apac: 1
      emea: 2
所以输出应该是

region amrs value 1
region amrs value 2
region amrs value 3
region apac value 1
region emea value 1
region emea value 2

我想知道是否可以通过ansible实现上述目标。我也尝试过使用嵌套的进行
,但没有成功

您可以使用助手任务来生成序列:

---
- hosts: localhost
  gather_facts: yes
  vars:
    objs:
      amrs: 3
      apac: 1
      emea: 2
  tasks:
    - set_fact:
        tmp_list: "{{ tmp_list | default([]) + [dict(name=item.key,seq=lookup('sequence','count='+item.value|string,wantlist=true))] }}"
      with_dict: "{{ objs }}"
    - debug: msg="region {{ item.0.name }} value {{ item.1 }}"
      with_subelements:
        - "{{ tmp_list }}"
        - seq

您可以使用助手任务来生成序列:

---
- hosts: localhost
  gather_facts: yes
  vars:
    objs:
      amrs: 3
      apac: 1
      emea: 2
  tasks:
    - set_fact:
        tmp_list: "{{ tmp_list | default([]) + [dict(name=item.key,seq=lookup('sequence','count='+item.value|string,wantlist=true))] }}"
      with_dict: "{{ objs }}"
    - debug: msg="region {{ item.0.name }} value {{ item.1 }}"
      with_subelements:
        - "{{ tmp_list }}"
        - seq

你能详细解释一下tmp_列表行吗。它工作了,我们在原始的
objs
dict上循环,并从空列表开始增长
tmp_列表
,添加dict元素,将
name
设置为原始dict的键和
seq
-生成的序列(列表),数字从1到原始dict的值。你真是个好朋友!你能详细解释一下tmp_列表行吗。它工作了,我们在原始的
objs
dict上循环,并从空列表开始增长
tmp_列表
,添加dict元素,将
name
设置为原始dict的键和
seq
-生成的序列(列表),数字从1到原始dict的值。你真是个好朋友!