How access dictionary in a jinja2 template?

3,069 views
Skip to first unread message

skr...@gmail.com

unread,
Oct 6, 2014, 1:06:32 PM10/6/14
to ansible...@googlegroups.com
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.2: 56713727820156410577229101238628035242
  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

Guy Matz

unread,
Oct 6, 2014, 2:48:43 PM10/6/14
to ansible...@googlegroups.com
Perhaps I'm not understanding what you're after, but this worked for me:
initial_token: {{ tokens[inventory_hostname] }}


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ee4a86cb-cc29-4345-82f0-5592ecc6ec8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

skr...@gmail.com

unread,
Oct 6, 2014, 4:00:52 PM10/6/14
to ansible...@googlegroups.com
Works for me now. I over worked on this and ignored the basics. Thanks Guy Matz.
Reply all
Reply to author
Forward
0 new messages