# populating dictionary from lookup

**URL:** <https://forum.ansible.com/t/populating-dictionary-from-lookup/37157>\
**Category:** Ansible Project\
**Created:** [September 29, 2022, 3:02pm UTC](https://forum.ansible.com/t/populating-dictionary-from-lookup/37157 "2022-09-29T15:02:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Edigarov](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gregory\_Edigarov](https://forum.ansible.com/u/Gregory_Edigarov)\
**Post date:** [September 29, 2022, 3:02pm UTC](https://forum.ansible.com/t/populating-dictionary-from-lookup/37157/1 "2022-09-29T15:02:16Z")

</div>

Hello  
As a part of another project, I have written a playbook:

---

<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:** [September 29, 2022, 9:04pm UTC](https://forum.ansible.com/t/populating-dictionary-from-lookup/37157/2 "2022-09-29T21:04:03Z")

</div>

Create it in Jinja. For example,

&nbsp;&nbsp;&nbsp;&nbsp;soarecords: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for zone in zones %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ zone }}:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for field in lookup('dig', zone, 'qtype=SOA')|split %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ soafields[loop.index0] }}: {{ field }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;soarecords\_dict: "{{ soarecords|from\_yaml }}"

---

<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:** [September 29, 2022, 9:17pm UTC](https://forum.ansible.com/t/populating-dictionary-from-lookup/37157/3 "2022-09-29T21:17:57Z")

</div>

The next option is putting the iteration into one task. For example,

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;soarecords\_dict: "{{ soarecords\_dict|d({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({item: \_dict}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ zones }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_dict: "{{ dict(soafields|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;zip(lookup('dig', item, 'qtype=SOA')|split)) }}"

---

<div class="post-metadata">

**Author:** ![Gregory\_Edigarov](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gregory\_Edigarov](https://forum.ansible.com/u/Gregory_Edigarov)\
**Post date:** [September 30, 2022, 9:17am UTC](https://forum.ansible.com/t/populating-dictionary-from-lookup/37157/4 "2022-09-30T09:17:44Z")

</div>

Thank you, Vladimir. I’ve completely forgotten that ansible allows vars to be defined for a single task.
