Two `blockinfile` tasks on the same file keep overwriting each other
IntermediateTroubleshootingModules, Idempotency and Handlers
Answer
Likely causes → Both tasks use the default marker # {mark} ANSIBLE MANAGED BLOCK, so the second task replaces the block that the first one wrote, and the next run swaps them back.
Fix →
- ansible.builtin.blockinfile:
path: /etc/hosts
marker: "# {mark} ANSIBLE MANAGED: internal services"
block: |
10.0.1.10 db.internal
Give each block its own unique marker.
Prevent → Always set marker explicitly. For more than one or two blocks, template the file or use drop-in directories.
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 runIntermediate`lineinfile` with `backrefs: true` reports `ok` but the line was never addedIntermediate