# Preserving list objects with Jinja2-style variables

**URL:** <https://forum.ansible.com/t/preserving-list-objects-with-jinja2-style-variables/13012>\
**Category:** Ansible Project\
**Created:** [June 23, 2013, 2:26pm UTC](https://forum.ansible.com/t/preserving-list-objects-with-jinja2-style-variables/13012 "2013-06-23T14:26:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_f\_krafft](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@martin\_f\_krafft](https://forum.ansible.com/u/martin_f_krafft)\
**Post date:** [June 23, 2013, 2:26pm UTC](https://forum.ansible.com/t/preserving-list-objects-with-jinja2-style-variables/13012/1 "2013-06-23T14:26:55Z")

</div>

Dear list,

please consider this playbook:

&nbsp;&nbsp;- name: test playbook  
&nbsp;&nbsp;&nbsp;&nbsp;hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mylist:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;othervar: '{{mylist}}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escaped: \{{mylist}}  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg='{{mylist.\_\_class\_\_}} {{othervar.\_\_class\_\_}} {{escaped.\_\_class\_\_}}'

The output is (list, unicode, unicode), which is rather unfortunate.

How do I preserve the type of a variable when interpolating like  
above?

---

<div class="post-metadata">

**Author:** ![martin\_f\_krafft](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@martin\_f\_krafft](https://forum.ansible.com/u/martin_f_krafft)\
**Post date:** [June 23, 2013, 3:22pm UTC](https://forum.ansible.com/t/preserving-list-objects-with-jinja2-style-variables/13012/2 "2013-06-23T15:22:21Z")

</div>

The same problem exists with booleans, and probably all other types:  
Jinja2 templating returns strings, no matter what type you pass in.  
This means that with legacy-style variables deprecated, and Jinja2  
interpolating the inventory, there is no way in Ansible to preserve  
the type of variables in the inventory.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 23, 2013, 4:33pm UTC](https://forum.ansible.com/t/preserving-list-objects-with-jinja2-style-variables/13012/3 "2013-06-23T16:33:11Z")

</div>

Types in the playbooks don’t matter because all the necessary casting happens in the modules.
