# Is it possible to use variables of "list" type from inventory within Jinja templates (typecast)?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-use-variables-of-list-type-from-inventory-within-jinja-templates-typecast/28093>\
**Category:** Ansible Project\
**Created:** [July 19, 2018, 1:16pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-variables-of-list-type-from-inventory-within-jinja-templates-typecast/28093 "2018-07-19T13:16:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Thommen](https://avatars.discourse-cdn.com/v4/letter/f/7feea3/32.png) [@Frank\_Thommen](https://forum.ansible.com/u/Frank_Thommen)\
**Post date:** [July 19, 2018, 1:16pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-variables-of-list-type-from-inventory-within-jinja-templates-typecast/28093/1 "2018-07-19T13:16:30Z")

</div>

Hi,

in our inventory we use variables of type "list" like

&nbsp;&nbsp;&nbsp;userlist = "['alice', 'bob', 'mallory']"

which - e.g. - we use in loops within tasks to roll out public SSH keys:

&nbsp;&nbsp;&nbsp;- name: Roll out SSH keys for someotheruser  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;authorized\_key:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: someotheruser  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;manage\_dir: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exclusive: no  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key: "{{ lookup('file', 'vars/ssh-keys/'+item+'.pub') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ userlist }}"

This has some unwanted side-effects. Therefore we'd like to construct the full authorized\_keys from a template and copy the resulting file:

&nbsp;&nbsp;&nbsp;- name: Roll out SSH keys for someotheruser  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: authorized\_keys.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/someotheruser/.ssh/authorized\_keys  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: someotheruser  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: someothergroup  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0600  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: yes

authorized\_keys.j2:

&nbsp;&nbsp;&nbsp;# Managed by ansible, do not modify

&nbsp;&nbsp;&nbsp;{% for user in userlist %}  
&nbsp;&nbsp;&nbsp;{{ lookup('file', 'vars/ssh-keys/'+user+'.pub') }}  
&nbsp;&nbsp;&nbsp;{% endfor %}

This results in jinja seeing each single character of the variable "userlist" as a key and hence looks for the files "vars/ssh-keys/[.pub", "vars/ssh-keys/'.pub", "vars/ssh-keys/a.pub" ecc. ecc.

Is there a way to tell Jinja, that these variables are in fact lists? Like some kind of typecasting?

I am aware, that "Values [...] are [...] interpreted [...] as a string" ([https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)). However we'd like to avoid separate host and group variable files, as they make the setup confusing.

Cheers  
frank

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 19, 2018, 1:47pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-variables-of-list-type-from-inventory-within-jinja-templates-typecast/28093/2 "2018-07-19T13:47:50Z")

</div>

This must be a bug because it work if you have the variable on the host like so

host1 userlist="['alice', 'bob', 'mallory']"

But if you add it to a group like so it fails

host1  
[all:vars]  
userlist="['alice', 'bob', 'mallory']"

A workaround you could do before you use the variable is

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userlist: '{{ userlist }}'
