Thanks for sharing this... just saved me quite a bit of frustration.

I was trying something along these lines 
(https://groups.google.com/d/msg/ansible-project/YTF6Up3kaKw/xHASvMROhegJ) 
to get a list of private IPs for a MySQL cluster config, and have ended up 
with:

[mysqld]
> {% set ip_list = [] %}
> {% for host in groups['db'] -%}
> {#   This is in an if block even though it is just a variable assignment 
> because
>        the only other way to append to an array is with the 'do' Jinja2 
> extension
> -#}
> {%   if ip_list.append(hostvars[host]['ansible_eth0']['ipv4']['address']) 
> -%}{% endif -%}
> {% endfor -%}
>
> datadir=/var/lib/mysql
> user=mysql
> # Path to Galera library
> wsrep_provider=/usr/lib/libgalera_smm.so
> # Cluster connection URL contains the IPs of all nodes
> wsrep_cluster_address=gcomm://{{ ip_list|join(',') }}
>


On Thursday, March 7, 2013 3:23:53 PM UTC-6, Peter Sankauskas wrote:
>
> Thanks for the suggestions. I believe both of those will work.
>
> However, both suggestions push variables for one template in one playbook 
> far up the stack and into the inventory. I am looking for a way to 
> encapsulate all of this inside just the playbook itself without extra 
> dependencies.
>
> The other issue is that I don't have named hosts (I am using the AWS EC2 
> inventory plugin). The hostnames are not unique and unpredictable.
>
> To have a solution that is both encapsulated, and dynamic, I have come up 
> with some Ansible/Jinja trickery.
>
> Using this yaml file to store the pre-computed tokens for clusters of size 
> 1 to 10 nodes: https://gist.github.com/pas256/5111146
>
> I can use this in the my cassandra.yaml.j2 template:
>
> # Cassandra storage config YAML 
> {% set seeds = [] %}
> {% for host in groups['tag_Name_cassandra'] -%}
> {#   This is in an if block even though it is just a variable assignment 
> because
>        the only other way to append to an array is with the 'do' Jinja2 
> extension
> -#}
> {%   if seeds.append(hostvars[host]['ansible_eth0']['ipv4']['address']) 
> -%}{% endif -%}
> {% endfor -%}
> {% set num_hosts = seeds|length -%}
>
> ...
>
> {% for ip in seeds|sort -%}
> {%   if ansible_eth0["ipv4"]["address"] == ip -%}
> initial_token: {{ initial_tokens[num_hosts][loop.index0] }}
> {%   endif -%}
> {% endfor -%}
>
>
>
> What I am doing is building an array of all nodes in the group, where the 
> array entry is the ip address. Then further down the template, I can loop 
> though that array looking for the IP address of the hosts that is running 
> the template, and use the loop index as the index into the initial_tokens 
> variable. 
> To ensure consistency, the array is always sorted.
>
> Not sure if there is a simpler way of doing this, but is does solve both 
> my concerns, and has all the logic in the 1 place.
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to