win_template and line breaks

I’m using win_template to copy a text file with a couple variables to a windows client. However, it seems to be removing all lines breaks from the file. I tried using unix2dos on the template, as well as adding “#jinja2:trim_blocks: False”, but neither one seemed to work. Any other ideas?

Hmm… not sure the integration tests cover this, could be a bug.

I’d try using

od -cx your_template | more

to make sure you have got CR and LF at the end of your lines to start with. If they are there in the template then please shout, as it sounds like it might need a fix.

Jon

Oh, meant to say be aware that win_template has been unmerged due to a different issue.

Checked with od and I have \r\n at the end of every line, which I think should do it.

I think that should be enough too. Soon as I get the chance I’ll see if I can work out what is going on.

Jon

Ok, as it stands, it seems the templating only generates \n line endings.

Its easy enough to force dos line endings by making a one-line change to win_template.py
after

resultant = template.template_from_file(self.runner.basedir, source, inject, vault_password=self.runner.vault_pass)
just add
resultant = resultant.replace(‘\n’, ‘\r\n’)

However, I’m wondering whether it should be optional.

Since you have a use case, do you have an opinion on whether something like ‘force_dos_line_endings’ should be a module parameter for win_template. If so I’m guessing defaulting it to True would be the best option?

Jon

Instead of forcing the replacement, does it make sense to update template_from_file to properly read \r\n vs \n? I think it’d be better to keep the file as-is regardless of the line endings instead of having to turn replacement on/off via parameter. Or is template_from_file something from jinja2 that we can’t really modify? I haven’t looked too far into that yet.

We backed win_template out of the devel branch for the time being, as we believe it does need to extend/inherit from the existing module, and that change needs to hold onto the ‘v2’ tree before it happens.

So, not a bug so much, just yet.

I do very much want to see this in the core, so that will come.

I know this has been backed out of devel, but I need windows template support and it was so close to working for me that I decided to fix the few issues I found and keep using the branch I was on. In case this is useful for the future or for anyone else:

ansible/lib/ansible/module_utils/powershell.ps1: Get-FileChecksum needs an update from this:

[System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower();
to this:

`
$hash = [System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace(“-”, “”).ToLower();

`

Otherwise it returns an array instead of a string, and win_copy will not be able to compare the two checksums correctly.

I also changed ansible/lib/ansible/modules/core/windows/win_copy.ps1 and updated:

$src_checksum.CompareTo($dest_checksum)
to:

$src_checksum.Equals($dest_checksum)
as apparently Microsoft recommends using CompareTo for sorting, not for checking equality, and I was getting some overload errors when CompareTo kept running into the array from Get-FileChecksum.

I just looked at what I was getting out from the template call in ansible. I had a bit of a look at the jinja2 docs but didn’t spot anything about line endings.
Next time I get some time I will have another look - I agree it would be nicer to use the template ‘as is’, as it would let you fetch a known working file, template it and then include into your own roles.

Thanks for this. I guess the integration tests can’t be covering this then… Another thing for me to look at while the v2 work progresses.

Hi,

Been meaning to say I discovered a few days ago that you can set the following in your j2 templates to ensure windows-style line endings

#jinja2: newline_sequence:‘\r\n’

I still want to do automatic detection of this but there is at least a workaround (which would likely be backwardly compatible once auto detection is in place).

Jon

Just to follow up, I have created new PRs which include the fixes described by Joey and added documentation about windows-style line endings to win_template module docs.

PRs are

https://github.com/ansible/ansible/pull/11086

https://github.com/ansible/ansible-modules-core/pull/1438

Jon