# set facts with loops

**URL:** <https://forum.ansible.com/t/set-facts-with-loops/16238>\
**Category:** Ansible Project\
**Created:** [September 16, 2014, 7:41am UTC](https://forum.ansible.com/t/set-facts-with-loops/16238 "2014-09-16T07:41:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adham\_H](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Adham\_H](https://forum.ansible.com/u/Adham_H)\
**Post date:** [September 16, 2014, 7:41am UTC](https://forum.ansible.com/t/set-facts-with-loops/16238/1 "2014-09-16T07:41:35Z")

</div>

Hey,

I am looking to concatenate variables in an array from a with\_fileglob but i am not sure that set\_fact works with loops

`

- hosts : all  
vars:  
myfiles : [“old”]  
tasks:
- name: “facts”  
set\_fact:  
myfiles: “{{ [item] | union(myfiles) }}”  
with\_items:
- “new1”
- “new2”
- “new3”
- name: “Debug”  
debug:  
var=myfiles  
`

The output

`"myfiles": ["new3", "old"]`

Only the last item is added. any ideas ?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 16, 2014, 2:48pm UTC](https://forum.ansible.com/t/set-facts-with-loops/16238/2 "2014-09-16T14:48:46Z")

</div>

Just for starters, with version of ansible are you using?

I’d probably be tempted to just declare “my\_files” as a list, in this particular case, it would make the playbook more readable.

I’m interested in the non-abstract use case though.

---

<div class="post-metadata">

**Author:** ![Adham\_H](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Adham\_H](https://forum.ansible.com/u/Adham_H)\
**Post date:** [September 16, 2014, 3:30pm UTC](https://forum.ansible.com/t/set-facts-with-loops/16238/3 "2014-09-16T15:30:10Z")

</div>

I use the dev branch. Unfortunately we use chef also. i hope will be replaced soon. Till then i have to use some data bags. So i have a bunch of json file i need to put in a list and start using some of variables.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 16, 2014, 4:52pm UTC](https://forum.ansible.com/t/set-facts-with-loops/16238/4 "2014-09-16T16:52:16Z")

</div>

Ah ok, I’m wondering if maybe you might want to write a custom lookup plugin that is something like “with\_databag” ?

That’s a little abstract, but maybe the cleanest way to model it and show you are looping over a chef databag still.

That’s just a thought and may not entirely be appropriate, but would probably read cleaner as well.

---

<div class="post-metadata">

**Author:** ![Adham\_H](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@Adham\_H](https://forum.ansible.com/u/Adham_H)\
**Post date:** [September 17, 2014, 7:10am UTC](https://forum.ansible.com/t/set-facts-with-loops/16238/5 "2014-09-17T07:10:48Z")

</div>

Michael,

I might go this way. Should be simple enough

Thanks
