Write raw file with newlines intact

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.

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 (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

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?

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.