# how to read variable from j2 and insert multiple values in final file

**URL:** <https://forum.ansible.com/t/how-to-read-variable-from-j2-and-insert-multiple-values-in-final-file/33118>\
**Category:** Ansible Project\
**Created:** [June 8, 2020, 3:21pm UTC](https://forum.ansible.com/t/how-to-read-variable-from-j2-and-insert-multiple-values-in-final-file/33118 "2020-06-08T15:21:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Aditya](https://forum.ansible.com/u/Aditya)\
**Post date:** [June 8, 2020, 3:21pm UTC](https://forum.ansible.com/t/how-to-read-variable-from-j2-and-insert-multiple-values-in-final-file/33118/1 "2020-06-08T15:21:21Z")

</div>

Hi Guys,

I have to write a task to read a j2 file and values from inventory and update the final file with multiple values of same variable defined in j2. I have tried to explain it with an example below.

Please note i cannot create multiple variables in j2 file as i want this to be handled through ansible/inventory.

Also i cannot define the values in my task as no. of variables will differ for different environments. For example site1 will have 1 tenantID and site2 might have 3.

test.j2

timeout.config=1000  
retry.count=3

#Multiple values  
{{Tenant\_ID}}.host={{ Tenant\_ID\_host }}

Inventory file  
Tenants:

- {“Tenant\_ID”:“1234”,“Tenant\_ID\_host”:“test”}
- {“Tenant\_ID”:“5657”,“Tenant\_ID\_host”:“test1”}

Final test file should look like

timeout.config=1000  
retry.count=3

1234.host=test

5657.host=test1

Thank you!!  
Aditya
