`lineinfile` with `backrefs: true` reports `ok` but the line was never added
IntermediateTroubleshootingModules, Idempotency and Handlers
Answer
Likely causes → This is how backrefs works: if regexp matches nothing, the file is left unchanged, and no line is appended.
Fix → Use two tasks (one to replace if present, one to add if missing), or drop backrefs and write the complete line:
- ansible.builtin.lineinfile:
path: /etc/default/grub
regexp: '^GRUB_CMDLINE_LINUX='
line: 'GRUB_CMDLINE_LINUX="quiet audit=1"'
Prevent → Only use backrefs when you are editing part of a line that you know already exists. Assert that it exists first if it matters.
More on Modules, Idempotency and Handlers
`ERROR! The requested handler 'Restart nginx' was not found in either the main handlers list nor in the listening handlers list`BeginnerA handler named `restart {{ app_service }}` is never found when notified as `restart myapp`AdvancedA `command` task reports `changed` on every run, and the Molecule idempotence check failsBeginner`curl ... | tar xz` fails on the host but the `shell` task reports successIntermediate`lineinfile` appends the same line again on every runIntermediateA `template` task reports `changed` and restarts the service on every runIntermediate