Ansible and the lookup file module removes line endings from file

The lookup file module is useful for reading in local files (not remote ones) and saving the contents into a variable. One gotcha that caught me recently was the default action to run rstrip() which according to the documentation removes whitespace from the end of the looked up file. What it doesn’t mention is that is also removes the trailing newline ‘\n’ character from the file. Normally this isn’t a problem except I was using it to read in a ssl certificate and send it to a fluentd server which then wouldn’t complete it’s startup and listen on ports – it just hung. Checking the certificate with openssl showed that at least openssl thought the certificate file was valid, and so did ruby (tested with OpenSSL::PKey::RSA.new() and File.read()). Turns out fluentd specifically checked for the newline character at the end of the ssl certificate file.

So until we upgrade to the very latest version of fluentd with this fix, we need to run the lookup module with the rstrip=False parameter like so:

lookup('file', '/etc/foo.txt', rstrip=False)

Leave a Reply