LOGBOOK

HELP

Quiz Entry - updated: 2026.12.01

When should you edit a file with lineinfile or blockinfile, and when is a template the better choice?

Edit in place when Ansible owns only a small part of a file that something else also manages; use a template when Ansible should own the whole file, which is easier to control and less error-prone.

lineinfile blockinfile template
Ansible controls one line one marked block the whole file
Rest of the file left as it is left as it is replaced
Good for one setting in a distro config a snippet in a shared file a service's main config
Risk regexp matches too much or too little duplicate or clashing markers overwrites local edits

Line edits look simple but get fragile: every setting needs a regexp that matches both the old and the new form, the result depends on what was in the file before, and two hosts can end up different because they started different. With a template, the file on every host is fully determined by the template plus that host's variables and facts, so you can read the template and know what is deployed.

A common rule of thumb: more than a couple of lineinfile tasks against one file means it is time for a template.

Go deeper:

From Quiz: ITIA / Ansible: Managing Files and Jinja2 Templates | Updated: Dec 01, 2026