loop over host group to create string

292 views
Skip to first unread message

Roy

unread,
Nov 5, 2015, 5:23:38 PM11/5/15
to Ansible Project
I have following host group 

[zookeeper]
zk001.abc.cc zookeeper_myid=1
zk002.abc.cc zookeeper_myid=2
zk003.abc.cc zookeeper_myid=3 


I want following to be created when playbook run

zookeeper.connect=zk001.abc.cc: 2181,zk002.abc.cc: 2181,zk003.abc.cc:2181


I tried following in a template which worked 

zookeeper.connect={{ groups['zookeeper']|join(':2181,') }}:2181

but now I want to use zookeeper_port var instead of 2181, so I tried following but it didn't worked.

zookeeper.connect={{ groups['zookeeper' ]|join(':{{ zookeeper_port }},') }}:{{ zookeeper_port }}


Any idea how to do this good way

Thanks

Tony Kinsley

unread,
Nov 8, 2015, 4:30:42 PM11/8/15
to Ansible Project
I don't think you need the nested {{}}. Jinja takes something similar to python code when it renders the templates. 

so 

zookeeper.connect={{ groups['zookeeper'] | join( ':' + zookeeper_port + ',' ) }}:{{ zookeeper_port }}

should work. I didn't test this out myself but I think thats correct.

Tony

John Favorite

unread,
Nov 8, 2015, 4:48:01 PM11/8/15
to ansible...@googlegroups.com
Hi Roy,

Here is what I use for a mapr drill-override template:


drill.exec: {
cluster-id: "cloud-drillbits",
zk.connect: "{% for host in groups['zookeeper'] %}{{
hostvars[host]['inventory_hostname'] }}:5181{% if not loop.last %},{%
endif %}{% endfor %}"
}

Hope this helps.
> --
> 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/d06101b5-0b26-4ab4-a30b-a2abbe76890f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Roy

unread,
Nov 11, 2015, 1:27:35 PM11/11/15
to Ansible Project
Thanks following works for me.

zookeeper.connect={% for node in groups['eventstore-zookeeper-' ] %}{{ node }}:2181{% if not loop.last %},{% endif %}{% endfor %} 

John Favorite

unread,
Nov 12, 2015, 6:20:29 AM11/12/15
to Ansible Project

Great, glad it worked for you!


On Wed, Nov 11, 2015, 1:27 PM Roy <rp...@njit.edu> wrote:
Thanks following works for me.

zookeeper.connect={% for node in groups['eventstore-zookeeper-' ] %}{{ node }}:2181{% if not loop.last %},{% endif %}{% endfor %} 

--
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.
Reply all
Reply to author
Forward
0 new messages