Output on Windows shows carriage return and new line feed

When looking at the output from a Windows host, all of the lines are appended together onto one line. You can see a “\r\n” instead of a new line. Is there an option to fix this for the output so they display as new lines correctly?

That sounds like an issue with your SSH terminal.

@Dick
Do you have this working for Windows hosts? Just to be clear, this only happens on Windows hosts. The output from Linux hosts works correctly.

There are numerous places where output is displayed- can you be more specific? The only place I’m aware of that attempts to “prettify” output like that is the “ansible” ad-hoc runner, and it works fine for me:

(ansiblev2) mdavis-mac:win-domain mdavis$ ansible winclient-basic -i hosts -m raw -a "Get-ChildItem c:\ "
winclient-basic | SUCCESS | rc=0 >>

Directory: C:\

Mode LastWriteTime Length Name


d---- 9/9/2015 4:01 PM inetpub
d---- 1/4/2016 4:04 PM my
d---- 8/30/2016 10:32 AM new
d---- 8/30/2016 10:32 AM new2
d---- 10/7/2015 1:15 PM newpath
d---- 8/22/2013 8:52 AM PerfLogs
d-r-- 9/22/2016 3:48 PM Program Files
d---- 9/2/2016 5:50 PM Program Files (x86)
d---- 7/5/2016 5:17 PM Python26
d---- 9/5/2016 9:09 PM temp
d---- 5/12/2016 3:18 PM tools
d-r-- 9/23/2016 2:04 PM Users
d---- 10/6/2016 1:14 PM vagrant
d---- 8/22/2016 4:10 PM Windows
d---- 5/16/2016 10:07 PM こんにちは
-a— 8/8/2016 10:08 AM 104857600 bigfile.txt
-a— 10/6/2016 1:44 PM 50 bla.inf
-a— 9/9/2016 4:40 PM 824 out.txt
-a— 9/2/2016 5:50 PM 233320 pslog.txt
-a— 9/2/2016 5:50 PM 1992 wrapperlog.txt

This would be the output from a Powershell script using ansible-playbook.

I think I’ve understood the above, this behaviour is to be expected as Ansible expects JSON back from modules.
Probably simplest way to fix it is to get your script to return json instead of whatever the objects at the end of the powershell pipeline output.

Sometimes doing a

ConvertTo-Json -Depth 1

is enough, although sometimes you have to use Select to pick the properties you need.

If you have json in your output you can use register and pickout whatever results you need the same as you would if you were using the return object from an any ansible module. Personally I find it more readable to look for {{ results.someattribute.thing }} rather than {{ results.stdout_lines[4].thing }} especially as often you can’t guarantee that the result you want is at pos 4 in the stdout_lines array, and you may have to deal with handling whitespace too before you’ve actually got to a string that you can use.

For extra credit of course, turn your script into an ansible module (its really not a lot of extra code), although you’d probably want to think about idempotence for a module in a way which you might not for a script. (As ever it depends what you are trying to achieve - i don’t allways try to make every playbook completely idempotent after all).

Hope this helps,

Jon