Hi,
I am creating a playbook that creates a valid cassandra.yaml file for each node in a new cluster using a jinja2 template. What this file needs is a different token for each node, but I do not know how to do this with Ansible.
So the part of the template in question looks like this:
initial_token: {{ token }}
In this example, I have 6 nodes. I can generate the tokens for each using this script:
$ ./tokenbuilder.py 6
{
“0”: {
“0”: 0,
“1”: 28356863910078205288614550619314017621,
“2”: 56713727820156410577229101238628035242,
“3”: 85070591730234615865843651857942052864,
“4”: 113427455640312821154458202477256070485,
“5”: 141784319550391026443072753096570088106
}
}
I believe there is a way to get the output of that script into a variable in Ansible so that I can access it by {{ tokens[0] }}. (Any tips appreciated).
In any case, I can use a playbook that has
vars:
tokens:
0: 0
1: 28356863910078205288614550619314017621
2: 56713727820156410577229101238628035242
…
tasks:
- name: Copy over the templated cassandra.yaml config file
template: src=templates/cassandra.yaml.j2 dest=/etc/cassandra/cassandra.yaml
to do the same thing.
The question is, how can I then get each of the 6 hosts to use one of these array values in the template? So that when the template is evaluated:
host1 has token = tokens[0] = 0
host2 has token = tokens[1] = 28356863910078205288614550619314017621
host3 has token = tokens[2] = 56713727820156410577229101238628035242
host4 has token = tokens[3] = 85070591730234615865843651857942052864
host5 has token = tokens[4] = 113427455640312821154458202477256070485host6 has token = tokens[5] = 141784319550391026443072753096570088106
All hosts are part of the same group in the inventory. Is there a trick to get the index of a host in a group?
I would really appreciate anyone pointing me in the right direction.
Thanks,
Peter