Our integration gateways push files to partners over SFTP through site to site VPNs, and several of those partner hosts have no public DNS. A role called partner_vpn writes their names and addresses into /etc/hosts with blockinfile. It had done that, unchanged, for two years.

In October the monitoring team added a role to the same playbook that writes the names of our metrics collectors into /etc/hosts, also with blockinfile. It ran after partner_vpn. The play was green. That night the push to our largest supplier failed with Could not resolve hostname, and so did two others.

blockinfile finds its block by the marker lines around it, and the default marker is the same for every task that does not set one: # BEGIN ANSIBLE MANAGED BLOCK and # END ANSIBLE MANAGED BLOCK. Neither role set one. When the monitoring task ran, it found the partner block, recognised the markers as its own and replaced the contents with its collector entries. On the next run partner_vpn found the collector block and replaced it back, and then the monitoring task replaced it again. Every play ended with only the second role's entries in the file.

There was a sign, and we had seen it. From the day the new role merged, both tasks reported changed on every run on every gateway. A change on every run is what non idempotent tasks look like, and we had a few of those already, so two more did not stand out.

Each role now sets its own marker, # {mark} ANSIBLE MANAGED BLOCK partner_vpn and # {mark} ANSIBLE MANAGED BLOCK monitoring. Changing a marker does not remove the block written under the old one, so the first run of the fixed playbook also deleted the default marked block with state: absent, otherwise the collector entries would have stayed in the file twice. Both roles are tested with Molecule, whose idempotence step runs the converge a second time and fails if any task reports changed, and we cleared the other tasks that had taught us to ignore that. The partner names have also moved into our internal DNS, where two teams cannot overwrite each other without noticing.

Two tasks were each correct on their own. They agreed on a name for their block, and the last one to write it won.

– Sergey Shinder