# How access dictionary in a jinja2 template?

**URL:** <https://forum.ansible.com/t/how-access-dictionary-in-a-jinja2-template/16443>\
**Category:** Ansible Project\
**Created:** [October 6, 2014, 5:06pm UTC](https://forum.ansible.com/t/how-access-dictionary-in-a-jinja2-template/16443 "2014-10-06T17:06:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skr989](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@skr989](https://forum.ansible.com/u/skr989)\
**Post date:** [October 6, 2014, 5:06pm UTC](https://forum.ansible.com/t/how-access-dictionary-in-a-jinja2-template/16443/1 "2014-10-06T17:06:31Z")

</div>

I am trying to come up with a play book to install cassandra. Here each node has a calculated token (not a random hash) and is unique. So, I have defined a dictionary in the vars/cassandra\_variables.yml as below. The IPs 192.168.56.x happen to be the ansible\_inventory hosts which are defined in the inventory file as [cassandrahosts]

How would I access the dictionary through jinja2 template.?

vars/cassandra\_variables.yml

tokens:

[192.168.56.1](http://192.168.56.1/): 0  
[192.168.56.2](http://192.168.56.2/): 56713727820156410577229101238628035242  
[192.168.56.3](http://192.168.56.3/): 113427455640312821154458202477256070485

192.168.56.x hosts are coming in from inventory file and in the “cassandra.j2” I have the following

initial\_token: {{ tokens[‘{{ inventory\_hostname}}’] }}

tokens[“{{ inventory\_hostname}}”] is evaluated but not {{ tokens[‘{{ inventory\_hostname}}’] }}

How do I get it to work? Where am I going wrong? Any help is much appreciated.

-SR

---

<div class="post-metadata">

**Author:** ![Guy\_Matz](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@Guy\_Matz](https://forum.ansible.com/u/Guy_Matz)\
**Post date:** [October 6, 2014, 6:48pm UTC](https://forum.ansible.com/t/how-access-dictionary-in-a-jinja2-template/16443/2 "2014-10-06T18:48:39Z")

</div>

Perhaps I’m not understanding what you’re after, but this worked for me:  
initial\_token: {{ tokens[inventory\_hostname] }}

---

<div class="post-metadata">

**Author:** ![skr989](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@skr989](https://forum.ansible.com/u/skr989)\
**Post date:** [October 6, 2014, 8:00pm UTC](https://forum.ansible.com/t/how-access-dictionary-in-a-jinja2-template/16443/3 "2014-10-06T20:00:51Z")

</div>

Works for me now. I over worked on this and ignored the basics. Thanks Guy Matz.
