# Write raw file with newlines intact

**URL:** <https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849>\
**Category:** Ansible Project\
**Created:** [June 23, 2016, 10:16pm UTC](https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849 "2016-06-23T22:16:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geoff\_Webster](https://avatars.discourse-cdn.com/v4/letter/g/f07891/32.png) [@Geoff\_Webster](https://forum.ansible.com/u/Geoff_Webster)\
**Post date:** [June 23, 2016, 10:16pm UTC](https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849/1 "2016-06-23T22:16:46Z")

</div>

Hi all,

I am attempting to slurp/cat a file from one server, store it as a variable and then write it out on a second server. This file however has newlines ‘\n’ explicitly written in it and I need to write it back out with these newlines intact. Anyone have a good way to do this? I have not had any luck.

---

<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:** [June 24, 2016, 8:05am UTC](https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849/2 "2016-06-24T08:05:38Z")

</div>

Does it have to be stored as a var? Could you use the synchronize module (rsync wrapper) delegated to one host to rsync it straight to the other?

If the source file doesn’t actually vary that much you can use template / win\_template module and add the jinja2 directive to instruct the template output to have whatever style line endings you want.

For example for windows style line endings use:  
`#jinja2: newline_sequence:'\r\n'`

See [http://docs.ansible.com/ansible/win\_template\_module.html](http://docs.ansible.com/ansible/win_template_module.html) (fairly certain the same thing will work using the template module) - search for newline\_sequence

in the jinja2 docs here.

You might be able to use fetch to get the file back to your controller, lookup to get the contents into a var, then use copy module’s content=var as local action to write to a file which you then template to destination server with the newline\_sequence

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![Geoff\_Webster](https://avatars.discourse-cdn.com/v4/letter/g/f07891/32.png) [@Geoff\_Webster](https://forum.ansible.com/u/Geoff_Webster)\
**Post date:** [June 24, 2016, 3:52pm UTC](https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849/3 "2016-06-24T15:52:25Z")

</div>

Thanks for the reply!

Main problem is that the file is generated during the playbook run (its a secrets file that contains encryption keys in a format/styling specific to GitLab which seems to throw in ‘\n’ at the end of certain sections, not explicitly at the end of a line.) The machines also don’t have SSH keys located on them. Do you happen to know if you can forward the SSH key from the control machine to the 2 machines I am working on so they can communicate?

---

<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:** [June 24, 2016, 4:49pm UTC](https://forum.ansible.com/t/write-raw-file-with-newlines-intact/21849/4 "2016-06-24T16:49:07Z")

</div>

> Main problem is that the file is generated during the playbook run (its a  
> secrets file that contains encryption keys in a format/styling specific to  
> GitLab which seems to throw in '\n' at the end of certain sections, not  
> explicitly at the end of a line.)

I don't see that as a problem.  
After the file is created do a fetch to get the file to the control machine, and the do a copy with delegate\_to to the other machine.  
With this approach the two machines don't need to know about each other.

> The machines also don't have SSH keys  
> located on them. Do you happen to know if you can forward the SSH key from  
> the control machine to the 2 machines I am working on so they can  
> communicate?

Check out ssh agent forwarding. But with the solution above you don't need this.
