A `template` task reported 'changed' every run and restarted the service — invisible whitespace diff. Root cause: Trailing-newline whitespace made the render differ from the target file each run. The fix: Control Jinja2 whitespace so the render byte-matches; avoid needless `notify`.
A production war story from the TheCodeForge Ansible series — the incident, why it happened, and the exact fix.
⏳ Timestamps:
0:00 - Cold open: Ansible playbook always reported 'changed' for a template task, even when no variables changed.
0:10 - Intro
0:17 - What Is a Template?
0:44 - Template vs Copy
1:24 - Full Playbook Example
1:37 - Jinja2 Syntax Basics
2:10 - Common Filters Overview
2:48 - Whitespace Control
3:19 - Using `regex_escape` Filter
3:32 - Version Caveat: `map('extract', ...)`
3:55 - Local Rendering for Debugging
4:30 - Using hostvars in Templates
5:03 - Advanced Filter Chaining
5:33 - Template Inheritance & Includes
6:05 - Testing with --diff and --check
6:34 - Performance Considerations
7:02 - Security Best Practices
7:34 - Idempotency Under the Hood
8:09 - Ansible playbook always reported 'changed' for a template task, even when no variables changed.
8:30 - Template vs Copy module
8:42 - The Fix
8:58 - ⚠ Gotcha: Using `copy` module for `.j2` files
9:13 - ⚠ Gotcha: Including `ansible_date_time` in template
9:25 - ⚠ Gotcha: Referencing host not in current play
9:38 - ⚠ Gotcha: Not escaping backslashes in `regex_replace`
9:55 - Debugging Guide
10:09 - Interview Questions
10:29 - FAQ
10:52 - Key Takeaways
11:04 - Next up
11:18 - Wrap-up
👉 Full article + code: https://thecodeforge.io/devops/ansibl...
⏭ Next up: Ansible Vault for Secrets Management
#ansible #devops #automation