When do you use `copy`, `template`, `lineinfile` and `blockinfile`?
IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer
copy— place a static file or inlinecontent:as-is.template— render a Jinja2.j2file with variables, then place it. Default for config files.lineinfile— ensure one line is present/absent, optionally replacing a regex match.blockinfile— manage a marked multi-line block inside a file you do not fully own.
If you own the whole file, use template. Editing files in place with lineinfile becomes hard to reason about once a file has more than a few managed lines.
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?Beginner