Prevent "changed" flag when registering a variable in template task
06:31 27 Jan 2026

I have a templating task with backup and the next removing old backups which is dependent on "changed" status of the first one.

- name: Deploy config.xml
  template:
    src: "config.xml.j2"
    dest: "{{ config_dir }}/config.xml"
    mode: 0660
    backup: true
  register: template_status

- name: Cleanup old backups
  import_role:
    name: "cleanup_backups"
  vars:
    cb_config: "config.xml"
    cb_keep: 3
  when: template_status.changed

The problem is that register: makes templating task always have "changed" status. I saw another topic here "How to prevent 'changed' flag when 'register'-ing a variable?", but there's a suggestion to completely ignore any changed status by adding changed_when: false which is not suitable for me.

I'd like to keep changed status but only if the template changes, and register a variable without changing status. How to achieve this?

ansible