# Setting facts from a loop

**URL:** <https://forum.ansible.com/t/setting-facts-from-a-loop/35474>\
**Category:** Ansible Project\
**Created:** [July 7, 2021, 7:53pm UTC](https://forum.ansible.com/t/setting-facts-from-a-loop/35474 "2021-07-07T19:53:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 7, 2021, 7:53pm UTC](https://forum.ansible.com/t/setting-facts-from-a-loop/35474/1 "2021-07-07T19:53:04Z")

</div>

I have a playbook where I’m trying to use the find module to find files owned by a certain user. I’m trying to do register a variable to hold the files found for each folder within the loop as follows:

vars:  
folders:

- folder: “/folder1”  
var: “temp\_folder1”
- folder: “folder2”

var: “temp\_folder2”

tasks:

- name: Find all files under {{ item.folder }}  
find:  
paths: “{{ item.folder }}”  
recurse: yes  
register: “{{ item.var }}”  
loop: “{{ folders }}”

I get a warning stating “register” is not templatable, then an error stating “Invalid variable name in ‘register’ specified: '{{ item.var }}”

Do I have to do this individually instead of via a loop?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [July 7, 2021, 7:54pm UTC](https://forum.ansible.com/t/setting-facts-from-a-loop/35474/2 "2021-07-07T19:54:29Z")

</div>

`register` must be static, you cannot template it.

See the following link for information about registering a variable with a loop:

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#registering-variables-with-a-loop](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#registering-variables-with-a-loop)

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 7, 2021, 8:01pm UTC](https://forum.ansible.com/t/setting-facts-from-a-loop/35474/3 "2021-07-07T20:01:06Z")

</div>

OK, so if I had 10 folders I was looping through, and set the register variable to “tmp\_folder” for example, how would I know then loop through that 1 variable to pull out the info I need? The next step is to set a fact based on the returned files that match a certain user name. Would the fact be something like this:

set\_fact:

temp\_folder1\_files: “{{ tmp\_folder[0].files | selectattr(‘pw\_name’, ‘equalto’, remove\_user) | list }}”  
temp\_folder2\_files: “{{ tmp\_folder[1].files | selectattr(‘pw\_name’, ‘equalto’, remove\_user) | list }}”  
temp\_folder3\_files: “{{ tmp\_folder[2].files | selectattr(‘pw\_name’, ‘equalto’, remove\_user) | list }}”

Thanks,  
Harry
