# Newlines are escaped when using jinja2 filters in playbook

**URL:** <https://forum.ansible.com/t/newlines-are-escaped-when-using-jinja2-filters-in-playbook/23488>\
**Category:** Ansible Project\
**Created:** [January 18, 2017, 10:54am UTC](https://forum.ansible.com/t/newlines-are-escaped-when-using-jinja2-filters-in-playbook/23488 "2017-01-18T10:54:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik\_Rambris](https://avatars.discourse-cdn.com/v4/letter/f/4af34b/32.png) [@Fredrik\_Rambris](https://forum.ansible.com/u/Fredrik_Rambris)\
**Post date:** [January 18, 2017, 10:54am UTC](https://forum.ansible.com/t/newlines-are-escaped-when-using-jinja2-filters-in-playbook/23488/1 "2017-01-18T10:54:13Z")

</div>

I’m trying to get around the issue with authorized\_keys not having support for exclusive and with\_items. I have a list of keys I want to populate an authorized\_keys file and have ansible remove all others that may be in the file.

authorized\_keys accepts a list of keys if they are supplied as a string as it splits them by newline. My setup is as following:

- name: Assemble keys  
set\_fact:  
key\_item: “{{ lookup(‘file’, role\_path + ‘/files/’ + item) }}”  
with\_items:
- dude1.pub
- dude2.pub
- dudette1.pub
- dudarino1.pub
- automated\_delivery.pub  
tags: ssh-keys

when: item != ‘automated.delivery.pub’ or (ssh\_key\_automated\_delivery is defined and ssh\_key\_automated\_delivery == True)  
register: keys

- name: Distribute operations ssh-keys to root  
authorized\_key:  
key : ‘{{ keys.results|selectattr(“ansible\_facts”,“defined”)|map(attribute=“ansible\_facts.key\_item”)|join(“\n”) }}’  
manage\_dir : yes  
state : present  
user : root  
exclusive : yes  
tags: ssh-keys

It does not complain but I get one key composing of all keys joined by backslash+n, not newline. If some of the key files have multiple keys on separate lines their newline does not get escaped.

Is there a way to tell ansible not to escape the newlines in key ?

I’ve tried to use the list filter but authorized\_key formats that as [item1], [item2] and is not happy with that.

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 20, 2017, 2:25pm UTC](https://forum.ansible.com/t/newlines-are-escaped-when-using-jinja2-filters-in-playbook/23488/2 "2017-01-20T14:25:59Z")

</div>

I don't know, I would suggest trying the template module to work  
around that.

Some jinja2-magic with looping through your keylist should work.

I also thought there was an escape filter, but that was just for  
regexes. And the shell quote filter wont work, I guess.

Johannes
