# loop variable on task file does not work

**URL:** <https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337>\
**Category:** Ansible Project\
**Created:** [May 15, 2019, 9:38am UTC](https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337 "2019-05-15T09:38:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [May 15, 2019, 9:38am UTC](https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337/1 "2019-05-15T09:38:51Z")

</div>

Hi everyone,

I have a variable where i want to loop on this in a .yml task file :

My variable :  
`  
lst\_apps:

- {name: APP01, version: 1234, bdd\_sid: BDDSID, bdd\_host: SERVEURBDD, bdd\_port: 1525}

`

Content of my main.xml :  
`

- name: Test display variable  
debug:  
msg: “{{ lst\_apps }}”  
tags: test

- name: Installation des differentes applications  
include: 2-install.yml  
with\_items:

- “{{ lst\_apps }}”  
loop\_control:  
loop\_var: tmp\_deploy\_app  
tags: install,test  
`

Content of my yml task file where the loop is executed :  
`

- name: Test display variable in task file  
debug:  
msg: “{{ item.bdd\_sid }}”  
with\_items: “{{ tmp\_deploy\_app }}”  
`

When i execute the task, the content of variable is correctly display but the loop in the task file does not work :

`  
[root@ANSIBLESRV projet]# ansible-playbook -l TEST\_SRV /projet/playbooks/DEPLOY\_test.yml --extra-vars @extra-vars/structure.yml --tags “test”  
[WARNING]: While constructing a mapping from /projet/roles/TEST\_ROLE/tasks/main.yml, line 12, column 3, found a duplicate dict key  
(tags). Using last defined value only.

PLAY [MCSLAB] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [TEST\_SRV]

TASK [TEST\_ROLE : Test display variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [TEST\_SRV] =\> {  
“msg”: [  
{  
“bdd\_host”: “SERVEURBDD”,  
“bdd\_port”: 1525,  
“bdd\_sid”: “BDDSID”,  
“name”: “APP01”,  
“version”: “1234”  
}  
]  
}

TASK [TEST\_ROLE : Installation des differentes applications] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
included: /projet/roles/TEST\_ROLE/tasks/2-install.yml for TEST\_SRV

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
TEST\_SRV : ok=3 changed=0 unreachable=0 failed=0  
`

Ansible version :  
`[root@ANSIBLESRV projet]# ansible --version ansible 2.7.10 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]`

Thanks for your help,

Matt

---

<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:** [May 15, 2019, 1:09pm UTC](https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337/2 "2019-05-15T13:09:27Z")

</div>

tmp\_deploy\_app doesn't contain a list, it is a dict.  
You can check that with putting this first in your 2-install.yml file

- debug: var=tmp\_deploy\_app

So if you are going to loop you need to use with\_dict.  
But for you example above you only need this

- name: Test display variable in task file  
&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ tmp\_deploy\_app.bdd\_sid }}"

---

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [May 15, 2019, 2:14pm UTC](https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337/3 "2019-05-15T14:14:37Z")

</div>

Hi,

Thanks for your reply,

I have this in my main.yml :  
`

- name: Installation des differentes applications  
include: 2-install.yml  
with\_items:
- “{{ mcs\_lst\_apps }}”  
loop\_control:  
loop\_var: tmp\_deploy\_app  
tags: install,test  
`

And this in my 2-install.yml

`

- debug: var=tmp\_deploy\_app  
`

But when i execute it, it’s the same thing :  
`TASK [MCS_ROLE : Installation des differentes applications] ******************************************************************************************** included: /projet/roles/MCS_ROLE/tasks/2-install.yml for SERVER`

Matt

---

<div class="post-metadata">

**Author:** ![mcoussi](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mcoussi/32/8056_2.png) [@mcoussi](https://forum.ansible.com/u/mcoussi)\
**Post date:** [May 25, 2019, 7:06am UTC](https://forum.ansible.com/t/loop-variable-on-task-file-does-not-work/30337/4 "2019-05-25T07:06:58Z")

</div>

Hi,

I can’t slove my problem.

Is it someone help me?

Thanks for your help,

Matt
