# How use with\_sequence and with\_dict together

**URL:** <https://forum.ansible.com/t/how-use-with-sequence-and-with-dict-together/31848>\
**Category:** Ansible Project\
**Created:** [December 19, 2019, 3:40am UTC](https://forum.ansible.com/t/how-use-with-sequence-and-with-dict-together/31848 "2019-12-19T03:40:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_Tomelin](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Rafael\_Tomelin](https://forum.ansible.com/u/Rafael_Tomelin)\
**Post date:** [December 19, 2019, 3:40am UTC](https://forum.ansible.com/t/how-use-with-sequence-and-with-dict-together/31848/1 "2019-12-19T03:40:24Z")

</div>

Hi,

I want to do loop in the dict variable appliance and in variable have var count. This count I should like loop count.

appliances:  
fw01:  
count: 2  
vmname: name  
image: redhat  
os\_type: Linux  
data\_disks:

- lun: 0  
disk\_size\_gb: 128  
managed\_disk\_type: Standard\_LRS

I create this task debug for tests:

- name: virtual\_machine  
debug:  
msg: “{{ item.key }} - {{ item.value.count }}”  
with\_dict:
- “{{ virtual\_machine }}”  
with\_sequence: start=0 end=“{{ item.value.count }}”

but return this error:

ERROR! duplicate loop in task: sequence

How can create this loop?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 19, 2019, 7:31am UTC](https://forum.ansible.com/t/how-use-with-sequence-and-with-dict-together/31848/2 "2019-12-19T07:31:08Z")

</div>

Matt Martz gave you the answer yesterday when you asked exactly the same question.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 19, 2019, 8:33am UTC](https://forum.ansible.com/t/how-use-with-sequence-and-with-dict-together/31848/3 "2019-12-19T08:33:43Z")

</div>

Hi Rafael,

> I want to do loop in the dict variable appliance and in variable have var  
> count. This count I should like loop count.
> 
> appliances:  
> &nbsp;&nbsp;fw01:  
> &nbsp;&nbsp;&nbsp;&nbsp;count: 2  
> &nbsp;&nbsp;&nbsp;&nbsp;vmname: name  
> &nbsp;&nbsp;&nbsp;&nbsp;image: redhat  
> &nbsp;&nbsp;&nbsp;&nbsp;os\_type: Linux  
> &nbsp;&nbsp;&nbsp;&nbsp;data\_disks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- lun: 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disk\_size\_gb: 128  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;managed\_disk\_type: Standard\_LRS
> 
> - name: virtual\_machine  
> &nbsp;&nbsp;debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.key }} - {{ item.value.count }}"  
> &nbsp;&nbsp;with\_dict:  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ virtual\_machine }}"  
> &nbsp;&nbsp;with\_sequence: start=0 end="{{ item.value.count }}"
> 
> but return this error:  
> ERROR! duplicate loop in task: sequence
> 
> How can create this loop?

It's possible to \*include\_tasks\* and create nested loops. For this purpose  
it's necessary to "Define inner and outer variable names with loop\_var"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#defining-inner-and-outer-variable-names-with-loop-var](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#defining-inner-and-outer-variable-names-with-loop-var)

For example the playbook

&nbsp;&nbsp;&nbsp;&nbsp;$ cat playbook.yml  
&nbsp;&nbsp;&nbsp;&nbsp;- hosts: master  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- include\_tasks: create-vm.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ appliances|dict2items }}"

with this included file

&nbsp;&nbsp;&nbsp;&nbsp;$ cat create-vm.yml  
&nbsp;&nbsp;&nbsp;&nbsp;- name: virtual\_machine  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.key }} - {{ inner\_item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_sequence: start=0 end="{{ item.value.count - 1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: inner\_item

should give

&nbsp;&nbsp;&nbsp;&nbsp;ok: [master] =\> (item=0) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "fw01 - 0"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;ok: [master] =\> (item=1) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "fw01 - 1"  
&nbsp;&nbsp;&nbsp;&nbsp;}

Cheers,

&nbsp;&nbsp;-vlado
