Interview Prep/Ansible/Modules, Idempotency and Handlers

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.

All Ansible questions Roadmap: Ansible

More on Modules, Idempotency and Handlers