Line breaks are excluded when copying a file from ansible linux host to windows server using win_copy

914 views
Skip to first unread message

ishan jain

unread,
Mar 3, 2016, 6:33:17 AM3/3/16
to Ansible Project
I am copying some text files located on a linux file system to windows file system using the win_copy module of Ansible. The final copied file do not have line breaks in it.

So the file which is like this on linux:

line1
line2
line3


Becomes this on windows:
line1line2line3

How can i make sure win_copy just copy the file exactly as it is ?

J Hawkesworth

unread,
Mar 3, 2016, 12:59:32 PM3/3/16
to Ansible Project
My guess is you are hitting the difference between what linux and windows considers a line ending.

linux is happy with \n 

windows wants \r\n

if you run 

od -cx yourfile

you will probably only see \n

if the file was originally created on a windows machine, in say, Notepad, then it will get the \r\n line endings.

I suggest you do the following:

edit the file in Notepad on a windows machine.
use the fetch module to copy it from the windows machine to your ansible controller
use od -cx to check it has windows line endings
the file from windows should now copy across unmodified when using win_copy

Hope this helps,

Jon

Nick Mellor

unread,
Sep 4, 2017, 1:49:45 AM9/4/17
to Ansible Project
Hi Ishan,

As mentioned previously, the file has not changed, just that Windows and Linux expect different line endings.

 

This is what worked for me using Powershell, converting Linux line-endings to Windows line-endings:

 

- name: convert line endings from Linux to Windows (PowerShell)

  win_shell: '(Get-Content "filename_from") -replace "`n", "`r`n" | Set-Content "filename_to.txt"'

 

The reason I needed to do this was that I was checking out text files on Linux, then copying them to a Windows server.


But the problem is, Git automatically ensures files have the "right" line-endings during checkout (meaning "right" for the machine doing the checkout, not the machine the file's copied to.)


Cheers,

 

Nick

Reply all
Reply to author
Forward
0 new messages