Chef template resource for Unix files UTF-8 LF on windows

Hi fellow chefs!

Re: Templating a file in UTF-8 with LF unix end of line sequence on Windows
Chef Workstation 0.13.35 used to upload to Chef 12 Server and Chef Infra Client 15.5.17 to deploy.

I upload the correct file but when templated onto Windows it converts the end of line sequence to Windows CR LF

I can use notepad++ to change encoding back to LF and our application then works straight away.
Somewhere along the chef journey the encoding changes ...

Looking at the chef template docs its doesn't mention encoding options but does have a mode option.

Any ideas?

Thanks

I see this open issue:

which says:

“When we upload cookbooks we use 'binmode' so we get no conversion of line endings. When we download cookbooks MOST of the file resources will use binmode so that users will get whatever they uploaded. However the template resource will do line ending conversion so will behave like binmode is false.”

you could try using the file resource instead of the template resource (just as a test to make sure chef installs
the file without changing the end of line sequence). If you can install the file using the file resource instead
of the template resource (it may not be as elegant), then this might be a solution.

another idea: install the file using the template resource as you are doing. but install it in an alternative location.
then use ’notifies’ to call a ruby block or command to install the updated file in the new location, but also fixing the
end of line sequence in the process.

This would allow idempotence. Chef would only update the alternative file location if the template changes, and
notifies would only call the “convert and install” process if the template changes.

something like:

install /etc/filename.windows using template resource
inside template resource, use: "notifies notifies :run, ‘execute[install_and_fix_file]’”

execute 'install_and_fix_file’ do
command ‘magically_fix_end_of_line < /etc/filename.windows > /etc/filename'
live_stream true
action :nothing
end

if there is no command you can run on windows to fix the end of line, then you could use a ruby block in a similar way.

Karl

@karlamrhein Many thanks for your responses. I really like your 'notify' approach to create an idempotent solution and will try that to resolve. I've got a PowerShell script to change the encoding and currently its running every time as a rough work around.

I see on your first link @Lamont_Granquist has already added a permanent solution coming out in Chef 16 so that's great news.

Great!

The final thing I’d recommend with that approach would be to put the following code (see below) above the other parts of the cookbook I described earlier:

# put this above the other code: 
# if the production location of the file 
# (the final location that has the correct 
# converted line ends) somehow gets 
# deleted, then have chef delete the 
# intermediate location of the file also. 
# This will trigger a clean download of the 
# intermediate location of the file, and 
# therefore trigger a conversion of that file 
# and copy it to the production location.

file ’/path/to/intermediate/file’   do
action :delete
  not_if { ::File.exist?(’/path/to/production/file’) }
end

this is a sanity check to put above the other code. It would likely never be run, unless something unexpected happened (eg, a rogue process or person removed the production version of the file). Maybe something even better would be an md5sum check. In any case, without the above code, chef is only watching the intermediate version of the file. Let me know if this makes sense.