While using j2 template and with_items i get the last item in o/p

Hi

my playbook is :

  • name: include vars
    include_vars:
    file: /home/rparida1/content-var.yml

  • name: Testing
    template:
    src: /home/rparida1/config.j2
    dest: /home/rparida1/test-raka1111222.properties
    with_items:

  • “{{ Tenants }}”

When i use the with_items, only the last item mentioned in my var file is displayed (my desired o/p is it should iterate through all the items mentioned in my var file):

iam.biz.tenant.auth.connetion.timeout=1000
iam.biz.tenant.auth.response.timeout=1000
iam.biz.tenant.auth.retry.count=3

iam.biz.tenant.auth.4231.host= test3
iam.biz.tenant.auth.4231.authType=ldap1
iam.biz.tenant.auth.4231.port=4433
iam.biz.tenant.auth.4231.baseDN=ou=dfsd,ou=People21,o=xxxxxx.com

I have a j2 template as follows:

iam.biz.tenant.auth.connetion.timeout={{ iam_biz_tenant_auth_connetion_timeout | default(‘1000’) }}

iam.biz.tenant.auth.response.timeout={{ iam_biz_tenant_auth_response_timeout | default(‘1000’) }}
iam.biz.tenant.auth.retry.count={{ iam_biz_tenant_auth_retry_count | default(‘3’) }}

iam.biz.tenant.auth.{{ item.Tenant_ID }}.host= {{ item.Tenant_host }}
iam.biz.tenant.auth.{{ item.Tenant_ID }}.authType={{ item.Tenant_authType }}
iam.biz.tenant.auth.{{ item.Tenant_ID }}.port={{ item.Tenant_port }}
iam.biz.tenant.auth.{{ item.Tenant_ID }}.baseDN={{ item.Tenant_baseDN }}

my var file is as follows:

Tenants:

  • {“Tenant_ID”:“8888”, “Tenant_host”:“test1”,“Tenant_port”:“3333”, “Tenant_authType”:“ldap1234”,“Tenant_baseDN”:“ou=dfhsdjfsd,ou=People,o=xxxxx.com”}
  • {“Tenant_ID”:“7777”, “Tenant_host”:“test7”,“Tenant_port”:“7733”, “Tenant_authType”:“ldap3777”,“Tenant_baseDN”:“ou=7777,ou=People7,o=xxxx.com”}
  • {“Tenant_ID”:“4231”, “Tenant_host”:“test3”,“Tenant_port”:“4433”, “Tenant_authType”:“ldap1”,“Tenant_baseDN”:“ou=dfsd,ou=People21,o=xxxxxx.com”}

You are looping over the same template so that will only keep the last item.

Don’t loop over the template, but instead use a loop inside your template.