# How to use a variable I created in a jinja2 template within playbook?

**URL:** <https://forum.ansible.com/t/how-to-use-a-variable-i-created-in-a-jinja2-template-within-playbook/25793>\
**Category:** Ansible Developer\
**Created:** [October 20, 2017, 2:32pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-i-created-in-a-jinja2-template-within-playbook/25793 "2017-10-20T14:32:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![K\_Dawg](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@K\_Dawg](https://forum.ansible.com/u/K_Dawg)\
**Post date:** [October 20, 2017, 2:32pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-i-created-in-a-jinja2-template-within-playbook/25793/1 "2017-10-20T14:32:58Z")

</div>

Hi,

I have something like this in a templates/file.j2 file (happens to be a kafka broker config):

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 20, 2017, 7:51pm UTC](https://forum.ansible.com/t/how-to-use-a-variable-i-created-in-a-jinja2-template-within-playbook/25793/2 "2017-10-20T19:51:07Z")

</div>

Sure, you can use set\_fact to set it as a task then it will be available for subsequent tasks and when templating.

Here’s how I build up the list of zookeeper hosts to pass to solrcloud. I’m not using private ips but should be adaptable to your situation.

`

- name: set fact zookeeper hosts  
set\_fact:  
zk\_hosts: “{{ groups[‘zookeeper’] | join(':” ~ zookeeper\_client\_port ~ ‘,’) }}:{{ zookeeper\_client\_port }}"  
`

Hope this helps,

Jon
