4

How is it possible to make template task not produce changed: result if the before and after only differ in line endings? I really don't care the line endings in config files on the target hosts, the applications ignores them, but those files can happen to change the line endings w/o changing the other content for various reasons. Thus, when I check those with --check option, I would like them to be considered as ok:

I am aware of #jinja2: newline_sequence: inline jinja2 instruction that can be set to either '\n' or '\r\n', but that only converts it one way - you have to stick to what you set while target hosts can have any of them.

My guess is that it can be solved by plugin, which does not seem to be trivial for my python knowledge. I have found this plugin which is close to what I want

https://github.com/berlic/ansibledaily/blob/master/callback_plugins/prediff.py

(It's from the author know here in SO as an experienced ansibler)

But it only can hide the diff log, if I apply I pre-porcess the content for the diff log by eliminating line endings difference, The result still stays changed:

What is the right way to affect the result as well?

Tag Wint
  • 407
  • 3
  • 9

1 Answers1

0

This totally depends on which module you are using in the first place, consider following example of ansible files when templating:

--- # variables
description_var: |
  hello this is a description
  this is line 2
--- # test_file.j2
my important config file
server_ip is 9.9.9.9
template file description:
  {{ description_var }}
--- # tasks.yml
- name: template file
  ansible.builtin.template:
    src: test_file.j2
    dest: /tmp/test_file

Changing and adding extra new lines to description_var will result in changed true in the template file task, we can add differ mode to the task and trim spaces and new lines to check before after state of the file:

- name: template file
  ansible.builtin.template:
    src: test_file.j2
    dest: /tmp/test_file
  diff: true
  register: template_result
  changed_when: 
    - template_result.diff | type_debug == 'list'
    - template_result.diff.0.after | trim != template_result.diff.0.before | trim

Adding any number of new lines in the beginning and the end of the file will not result in changed true after wards...

YosSaL
  • 667
  • 6
  • 15