Hi Kai Stian Olstad,

Thanks for the reply.

Yes I did try the loop_control. The problem with loop_control is when it 
iterates over the list_of_dict and takes each item of it, it considers the 
item as "String" and not the dict to iterate over, so I get the error 
saying with_dict expects a dict on the task of iterating over the dict.
 
Here are the playbooks and ansible output.

*test.yml*

---


- hosts: localhost

  tasks:


     - name: print

       include: "something.yml listDict={{listDict}}"

       with_items: "{{ list_of_dict }}"

       loop_control:

         loop_var: listDict


*something.yml*


---


- debug: var=listDict


- name: print the task

  debug: msg="{{ item.key }} is key,{{ item.value }} is value"

  with_dict: listDict


*Ansible output:*

bash-4.3# ansible-playbook test.yml 

PLAY [localhost] 
***************************************************************

TASK [setup] 
*******************************************************************
Wednesday 10 January 2018  21:01:17 +0000 (0:00:00.038)       0:00:00.038 
***** 
ok: [localhost]

TASK [print] 
*******************************************************************
Wednesday 10 January 2018  21:01:21 +0000 (0:00:03.729)       0:00:03.767 
***** 
 [WARNING]: The loop variable 'listDict' is already in use. You should set 
the `loop_var` value in the `loop_control` option for the task to something
else to avoid variable collisions and unexpected behavior.

included: /vms-deploy/ansible/something.yml for localhost
included: /vms-deploy/ansible/something.yml for localhost

TASK [debug] 
*******************************************************************
Wednesday 10 January 2018  21:01:21 +0000 (0:00:00.108)       0:00:03.876 
***** 
ok: [localhost] => {
    "listDict": "dict1_test"
}

*TASK [print the task] 
***********************************************************
*Wednesday 10 January 2018  21:01:21 +0000 (0:00:00.087)       0:00:03.964 
***** *
*fatal: [localhost]: FAILED! => {"failed": true, "msg": "with_dict expects 
a dict"}*

PLAY RECAP 
*********************************************************************
localhost                  : ok=4    changed=0    unreachable=0    
failed=1   

Wednesday 10 January 2018  21:01:21 +0000 (0:00:00.022)       0:00:03.986 
***** 
=============================================================================== 
setup ------------------------------------------------------------------- 
3.73s
print ------------------------------------------------------------------- 
0.11s
debug ------------------------------------------------------------------- 
0.09s
print the task ---------------------------------------------------------- 
0.02s


Please advice.

Thanks,
Amishi Shah

On Tuesday, January 9, 2018 at 10:49:50 PM UTC-8, Kai Stian Olstad wrote:
>
> On 09.01.2018 22:47, Amishi Shah wrote: 
> > In the code I want to iterate over all the dictionaries listed in 
> > list_of_dict. So that whenever a new dictionary is added in the 
> > variable 
> > file, the code handles it dynamically and there is no need to change 
> > the 
> > with_dict list. 
> > 
> > 
> > I tried to look into Ansible approaches but I could not find out the 
> > way to 
> > iterate over two things (list and dictionaries) at the same time. 
>
> Check out loop_control 
> http://docs.ansible.com/ansible/latest/playbooks_loops.html#loop-control 
>
>
> -- 
> Kai Stian Olstad 
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/3be8fee1-1f39-43ea-8624-51101a67b64e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to