Redis Inventory Variable Template issue

So I’m in a rut… I have this line in my template. Basically I have set a variable in my inventory file and im trying to access it in my Jinja2 template but as you can guess it’s not working.

I need to have the template add the slaveof line on hosts that don’t have the slave string set.

[root@p-mgmt01 redis]# cat hostscert

[haproxyhosts]

10.193.45.147 keepalived_role=master

10.193.45.148 keepalived_role=slave

[redishosts]

10.193.45.145 redis_role=master

10.193.45.146 redis_role=slave

[redishosts:vars]

sentinelMaster=10.193.45.145

authpass=‘cert-0c924c2361df3473823ab64ab36c33t38’

[haproxyhosts:vars]

virtual_redis_vip=3.0.0.55

sentinelMaster=10.193.45.145

authpass=‘cert-0c9e4cdc61gf3472803ab64eb56363c12’

keepalived_router_id=‘33’

Template Code:

{% for host in redishosts %}

{% if redis_role.lower() == “slave” %}

slaveof {{ hostvars[groups[‘redishosts’][0]][‘ansible_eth0’][‘ipv4’][‘address’] }} 6379

{% endif %}

{% endfor %}

In your playbook, did you talk to both groups of hosts before templating?

hosts: redis:haproxy (not your exact group names, but you get the idea you can specify multiple gost groups separated by colons here)
gather_facts: True
tasks:
  template...