# set\_fact doesn't work in loop

**URL:** <https://forum.ansible.com/t/set-fact-doesnt-work-in-loop/31465>\
**Category:** Ansible Project\
**Created:** [October 11, 2019, 8:23pm UTC](https://forum.ansible.com/t/set-fact-doesnt-work-in-loop/31465 "2019-10-11T20:23:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Crabtree](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Jason\_Crabtree](https://forum.ansible.com/u/Jason_Crabtree)\
**Post date:** [October 11, 2019, 8:23pm UTC](https://forum.ansible.com/t/set-fact-doesnt-work-in-loop/31465/1 "2019-10-11T20:23:54Z")

</div>

Hello,

I’m trying to build a list from data in other lists by looping over lists and using set\_fact to build a new list.

---

<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:** [October 11, 2019, 9:36pm UTC](https://forum.ansible.com/t/set-fact-doesnt-work-in-loop/31465/2 "2019-10-11T21:36:13Z")

</div>

You can't use {{ }} inside {{ }} since you are already in template mode.  
So use this instead

&nbsp;&nbsp;&nbsp;item\_list: '{{ item\_list + [item] }}'

But you can solve this a lot faster without a loop with the union filter

&nbsp;&nbsp;&nbsp;- name: "create list"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item\_list: '{{ item\_list | union(location\_list.site) }}'

---

<div class="post-metadata">

**Author:** ![Jason\_Crabtree](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Jason\_Crabtree](https://forum.ansible.com/u/Jason_Crabtree)\
**Post date:** [October 11, 2019, 9:48pm UTC](https://forum.ansible.com/t/set-fact-doesnt-work-in-loop/31465/3 "2019-10-11T21:48:20Z")

</div>

Thank you. I’ll try the update in my actual script and see how it goes.

I can’t do a join since it’s not a simple list I’m really trying to turn into a simple list. This code seemed to demonstrate the crux of my issue though and I’m hoping you provided the solution I needed.

I’m still trying to understand when quotes and brackets should be used.

-Jason
