Loop files that will be used as variables

79 views
Skip to first unread message

dudu.c...@gmail.com

unread,
Jun 14, 2023, 7:07:38 AM6/14/23
to Ansible Project

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

Will McDonald

unread,
Jun 14, 2023, 3:01:56 PM6/14/23
to ansible...@googlegroups.com
I think this answer might give you a pointer in the right direction: 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?)


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/50d4e830-9330-43be-95dc-68795a311d9an%40googlegroups.com.

dudu.c...@gmail.com

unread,
Jun 18, 2023, 1:43:33 AM6/18/23
to Ansible Project
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 כתב/ה:

Dick Visser

unread,
Jun 18, 2023, 2:13:20 AM6/18/23
to ansible...@googlegroups.com
On Sun, 18 Jun 2023 at 07:43, dudu.c...@gmail.com <dudu.c...@gmail.com> wrote:
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?

 

Vladimir Botka

unread,
Jun 18, 2023, 2:51:58 AM6/18/23
to ansible...@googlegroups.com
On Wed, 14 Jun 2023 04:07:38 -0700 (PDT)
"dudu.c...@gmail.com" <dudu.c...@gmail.com> wrote:

> *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

Given the files

shell> cat file-1.yaml
id: 1
color: blue

shell> cat file-2.yaml
id: 2
color: red

Read the files in the loop. Test it

shell> cat pb.yml
- hosts: localhost
tasks:
- debug:
var: i
loop:
- file-1.yaml
- file-2.yaml
vars:
i: "{{ lookup('file', item )|from_yaml }}"

gives (abridged)

TASK [debug]
****************************************************
ok: [localhost] => (item=file-1.yaml) => ansible_loop_var: item
i:
color: blue
id: 1
item: file-1.yaml
ok: [localhost] => (item=file-2.yaml) =>
ansible_loop_var: item
i:
color: red
id: 2
item: file-2.yaml

The variable *item* keeps the name of the current file in the loop.
You have to use the variable *i* both in the template file

shell> cat input.sql.j2
select * from {{ i.id }} where {{ i.color }}

and in the *template* task. The play

shell> cat pb.yml
- hosts: localhost
tasks:
- template:
src: input.sql.j2
dest: "/tmp/{{ i.id }}.sql"
loop:
- file-1.yaml
- file-2.yaml
vars:
i: "{{ lookup('file', item )|from_yaml }}"

creates the files

shell> cat /tmp/1.sql
select * from 1 where blue

shell> cat /tmp/2.sql
select * from 2 where red


--
Vladimir Botka

dudu.c...@gmail.com

unread,
Jun 19, 2023, 3:43:32 AM6/19/23
to Ansible Project

Vladimir Botka
‏@
Thank you - it works 


ב-יום ראשון, 18 ביוני 2023 בשעה 09:51:58 UTC+3, Vladimir Botka כתב/ה:
Reply all
Reply to author
Forward
0 new messages