Interview Prep/Ansible/Modules, Idempotency and Handlers

`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.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers