# Rendering a List in a Template

**URL:** <https://forum.ansible.com/t/rendering-a-list-in-a-template/18037>\
**Category:** Ansible Project\
**Created:** [April 21, 2015, 11:52am UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037 "2015-04-21T11:52:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Lannine](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nathan\_Lannine](https://forum.ansible.com/u/Nathan_Lannine)\
**Post date:** [April 21, 2015, 11:52am UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/1 "2015-04-21T11:52:51Z")

</div>

I am attempting to pass a list of lists via a registered variable to a template in which I then attempt to loop over the outer list. However, the template is looping over the entire list as a string, and each iteration of the loop outputs one character instead of a sublist as I intend. Does anyone have any thoughts on how I might tweak what I’m doing to accomplish this? At this point, I’m looking at attempting to bundle the first two parts of the below task list into a single task that creates what I want with Jinja2 directly instead of asking Ansible to do so.

Thank you,  
Nathan

/home/nathan/ANSIBLE/TEMPLATE/roles/3850/tasks/main.yml:

---

<div class="post-metadata">

**Author:** ![Yassen\_Damyanov](https://avatars.discourse-cdn.com/v4/letter/y/9fc29f/32.png) [@Yassen\_Damyanov](https://forum.ansible.com/u/Yassen_Damyanov)\
**Post date:** [April 23, 2015, 8:37pm UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/2 "2015-04-23T20:37:19Z")

</div>

Your issue is that the variable is a python string and iterating over it gives back each character.

This is probably not the best way to fix this, but should work:  
Creating a custom jinja2 filter that does eval(str\_argument) will do. I managed to get one working as follows:

cat ./filter\_plugins/utils.py  
“”"  
Simple jinja2 filters for ansible playbooks:  
See [http://grokbase.com/t/gg/ansible-project/14btv17s4n/how-to-add-custom-jinja-filters](http://grokbase.com/t/gg/ansible-project/14btv17s4n/how-to-add-custom-jinja-filters)  
“”"

class FilterModule(object):  
def filters(self):  
return {  
‘eval’: lambda s: eval(s),

# some day add more filters here …

}

The ./filter\_plugins/ folder is relative to the location where I execute the playbook from.

Now use that filter in your jinja2 template, where you reference the string var which is actually a list:

{% for iranges in range\_list.results[0].stdout |eval %}

HTH,  
Yassen

---

<div class="post-metadata">

**Author:** ![Yassen\_Damyanov](https://avatars.discourse-cdn.com/v4/letter/y/9fc29f/32.png) [@Yassen\_Damyanov](https://forum.ansible.com/u/Yassen_Damyanov)\
**Post date:** [April 23, 2015, 8:52pm UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/3 "2015-04-23T20:52:32Z")

</div>

Forgot to note that you must trust the output that you "eval()"uate. Another (safer) option would be to output valid Json and then parse it on the ansible/python side.  
Yassen

---

<div class="post-metadata">

**Author:** ![Nathan\_Lannine](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nathan\_Lannine](https://forum.ansible.com/u/Nathan_Lannine)\
**Post date:** [April 24, 2015, 2:43pm UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/4 "2015-04-24T14:43:30Z")

</div>

Thank you, Yassen,

That helped tremendously by doing exactly what I was looking for and saving me quite a bit of time doing it another way. Regarding your suggestion, if I output my list as json, would I set it as a fact in the playbook filtered with “|from\_json” or would that best be implemented in the for loop in my template?

Regards,  
Nathan

---

<div class="post-metadata">

**Author:** ![Yassen\_Damyanov](https://avatars.discourse-cdn.com/v4/letter/y/9fc29f/32.png) [@Yassen\_Damyanov](https://forum.ansible.com/u/Yassen_Damyanov)\
**Post date:** [April 24, 2015, 5:18pm UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/5 "2015-04-24T17:18:21Z")

</div>

In case you are not going to use that python structure elsewhere – I guess it is a matter of taste; I would simply filter some\_result.stdout in the template using from\_json:

{% for item in some\_result.stdout |from\_json %}  
…  
{% endfor %}

(I omit lots of details, you see.)

If your script has issues and cannot dump that json stuff (but spits some error instead) then the template rendering will fail, which seems fine – you should still be able to see the error and diagnose it.

Y.

---

<div class="post-metadata">

**Author:** ![Nathan\_Lannine](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Nathan\_Lannine](https://forum.ansible.com/u/Nathan_Lannine)\
**Post date:** [April 24, 2015, 7:41pm UTC](https://forum.ansible.com/t/rendering-a-list-in-a-template/18037/6 "2015-04-24T19:41:14Z")

</div>

Thank you, again.

N
