# Loop files that will be used as variables

**URL:** <https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102>\
**Category:** Ansible Project\
**Created:** [June 14, 2023, 11:07am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102 "2023-06-14T11:07:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [June 14, 2023, 11:07am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/1 "2023-06-14T11:07:38Z")

</div>

Hi ,

I’m using the template module that takes a J2 template and update the relevant fields.

My problem is When I want to create a file per file input should be used as variable – I’m actually need to understand how to loop file that should be used as var files

**My J2 file**

select \* from {{ item.id}} where {{ item.color}}

**My input files**

**File-1.yml :**

Id: 1  
color: blue

**File-2** \*\*.yml\*\* **:**

Id: 2  
color: red

**My Playbook – that is not working.**

- hosts: localhost  
become: true  
gather\_facts: yes  
tasks:

- name:  
template:  
src: /opt/input.sql.j2  
dest: /opt//{{item.id}}.sql  
with\_items:

- file-1.yaml

- file-2.yaml

**The output files I wish to have**

1.sql

select \* from 1 where blue

2.sql

select \* from 2 where red

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [June 14, 2023, 7:01pm UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/2 "2023-06-14T19:01:25Z")

</div>

I think this answer might give you a pointer in the right direction: [https://stackoverflow.com/a/52237675](https://stackoverflow.com/a/52237675)

It should work, but there could be a better/simpler way to do this. Do you control the generation of the files that provide your source variables?

What’s the **_actual_** problem you need to solve? (Obviously dynamically generating and presumably running some SQL but can you step back a level or two and describe the actual goal?)

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [June 18, 2023, 5:43am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/3 "2023-06-18T05:43:33Z")

</div>

Sorry - But i didnt fully understand your question

My input files are static - The problem that i’m trying to solve is how to loop “vars\_file” for the same task

ב-יום רביעי, 14 ביוני 2023 בשעה 22:01:56 UTC+3, Will McDonald כתב/ה:

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [June 18, 2023, 6:12am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/4 "2023-06-18T06:12:44Z")

</div>

> Sorry - But i didnt fully understand your question
> 
> My input files are static - The problem that i’m trying to solve is how to loop “vars\_file” for the same task

What are you trying to achieve with the 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:** [June 18, 2023, 6:51am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/5 "2023-06-18T06:51:29Z")

</div>

> \*My J2 file\*
> 
> select \* from {{ item.id}} where {{ item.color}}
> 
> \*My input files\*
> 
> \*File-1.yml :\*  
> Id: 1  
> color: blue
> 
> \*File-2\*\*.yml\*\* :\*  
> Id: 2  
> color: red
> 
> \*My Playbook – that is not working. \*  
> - hosts: localhost  
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;gather\_facts: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /opt/input.sql.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /opt//{{item.id}}.sql  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file-1.yaml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file-2.yaml
> 
> \*The output files I wish to have \*
> 
> 1.sql  
> select \* from 1 where blue
> 
> 2.sql  
> select \* from 2 where red

Given the files

&nbsp;&nbsp;\> cat file-1.yaml  
&nbsp;&nbsp;id: 1  
&nbsp;&nbsp;color: blue

&nbsp;&nbsp;\> cat file-2.yaml  
&nbsp;&nbsp;id: 2  
&nbsp;&nbsp;color: red

Read the files in the loop. Test it

&nbsp;&nbsp;\> cat pb.yml  
&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: i  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file-1.yaml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file-2.yaml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i: "{{ lookup('file', item )|from\_yaml }}"

gives (abridged)

&nbsp;&nbsp;TASK [debug]

---

<div class="post-metadata">

**Author:** ![dudu.c](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dudu.c](https://forum.ansible.com/u/dudu.c)\
**Post date:** [June 19, 2023, 7:43am UTC](https://forum.ansible.com/t/loop-files-that-will-be-used-as-variables/38102/6 "2023-06-19T07:43:32Z")

</div>

Vladimir Botka‏@  
Thank you - it works

ב-יום ראשון, 18 ביוני 2023 בשעה 09:51:58 UTC+3, Vladimir Botka כתב/ה:
