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

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