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

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 ?

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

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.

it 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