Q: In Ansible infrastructure automation, engineers are modifying configs using lineinfile and template modules. When should you use template versus lineinfile to prevent configuration drift?
Using lineinfile for multi-line configurations creates unpredictable file ordering, duplicate keys, and silent configuration drift. Jinja2 templates (template module) provide declarative, atomic, and auditable single-source-of-truth state.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Examine lineinfile Failure Modes
lineinfile modifies files in-place using regular expression pattern matching. Over time, this introduces subtle production outages:
- Duplicate Directives: If the regex does not match existing variations (whitespace, comments),
lineinfileappends a new line, leaving duplicate conflicting parameters. - Ordering Hazards: In files like
sshd_configwhereMatchblocks change scoping for all subsequent lines, appending lines to the end breaks global security settings. - Ghost Configurations: It cannot delete orphaned settings that are no longer declared in the playbook.
Adopt Jinja2 Templating (template Module)
The template module treats configuration files as declarative, complete artifacts:
- Single Source of Truth: The rendered output completely replaces the destination file.
- Atomic Overwrite: Ansible writes to a temporary file, validates syntax, and swaps the file atomically, eliminating partial file writes.
- Dynamic Logic: Supports loops, conditionals, and filters directly within the template.
Implement Pre-Flight Validation and Backups
Combine templating with built-in safety parameters:
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
backup: yes
validate: '/usr/sbin/sshd -t -f %s'- If template rendering produces invalid sshd syntax, Ansible halts, preserves the backup, and leaves active SSH connections undisturbed.
- lineinfile relies on fragile regex matching that frequently creates duplicate keys and misplaces scoped directives (like sshd Match blocks).
- lineinfile cannot remove obsolete configuration parameters that are deleted from your playbook, causing silent drift.
- The 'template' module renders Jinja2 files as complete, authoritative declarative configurations.
- Templates are written atomically using temporary swap files, preventing truncated files during sudden outages.
- Always include 'backup: yes' and 'validate: /usr/sbin/sshd -t -f %s' to guarantee invalid configs never hit disk.
- Reserve lineinfile strictly for isolated one-line insertions in external vendor-managed files.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Managing files with 5+ consecutive lineinfile tasks Massively slows execution down (5 SSH round-trips) and turns declarative config into unmaintainable procedural code.
- Using lineinfile at the bottom of sshd_config containing Match Group blocks Appended lines become scoped inside the Match block rather than global server configuration, causing major security holes.
- Omitting backup: yes when deploying system-critical daemon templates Without backups, rolling back unexpected template variables during an outage requires rebuilding instances.