loop variable on task file does not work

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

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
   debug:
     msg: "{{ tmp_deploy_app.bdd_sid }}"

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

Hi,

I can’t slove my problem.

Is it someone help me?

Thanks for your help,

Matt