loop variable on task file does not work

20 views
Skip to first unread message

COUSSI Matthieu

unread,
May 15, 2019, 5:38:51 AM5/15/19
to Ansible Project
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

Kai Stian Olstad

unread,
May 15, 2019, 9:09:34 AM5/15/19
to ansible...@googlegroups.com
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 }}"


--
Kai Stian Olstad
Message has been deleted

COUSSI Matthieu

unread,
May 15, 2019, 10:14:37 AM5/15/19
to Ansible Project
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

COUSSI Matthieu

unread,
May 25, 2019, 3:06:58 AM5/25/19
to Ansible Project
Hi,

I can't slove my problem.

Is it someone help me?
Reply all
Reply to author
Forward
0 new messages