# Redis Inventory Variable Template issue

**URL:** <https://forum.ansible.com/t/redis-inventory-variable-template-issue/24568>\
**Category:** Ansible Project\
**Created:** [May 24, 2017, 6:15pm UTC](https://forum.ansible.com/t/redis-inventory-variable-template-issue/24568 "2017-05-24T18:15:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Owen\_Corcoran](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Owen\_Corcoran](https://forum.ansible.com/u/Owen_Corcoran)\
**Post date:** [May 24, 2017, 6:15pm UTC](https://forum.ansible.com/t/redis-inventory-variable-template-issue/24568/1 "2017-05-24T18:15:01Z")

</div>

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 %}

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [May 24, 2017, 7:33pm UTC](https://forum.ansible.com/t/redis-inventory-variable-template-issue/24568/2 "2017-05-24T19:33:06Z")

</div>

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:  
&nbsp;&nbsp;template...
