Weekly Padgett

No. 0401 min read

Idempotency: can I run the damned thing again?

Your script worked once. Wonderful. Now lose the terminal and run it again. Ansible, shell scripts, and ledgers make different promises about what happens next.

A person holds a toilet seat above numerous seats stacked on a toilet, with tools on the tiled floor.
File photoDramatization. No ansible was harmed.

Your script worked once, but can you run it again after losing the terminal?

Idempotency[1]Ref. 1Idempotency en.wikipedia.org means repeating an operation has the same intended effect as performing it once. The program can execute again and emit another log line. It can talk to the network too. What it cannot do is keep changing the state it promised to control just because you asked twice.

"Append this setting" tells it to do something. Keep running that and you can collect duplicate lines. "This setting must have this value" gives the program a condition to inspect, which is considerably more useful to me than counting how many commands ran.

The Ansible glossary[2]Ref. 2Ansible glossary docs.ansible.com describes idempotency through the changes needed to reach the desired state. Putting a task in a playbook does not get it a certificate. You still have to write the thing properly.

Wrap an append command in ansible.builtin.shell[3]Ref. 3ansible.builtin.shell docs.ansible.com and it is still an append command. Set changed_when: false and you've changed the reporting while the command keeps doing exactly what it did before. The duplicate lines do not give a fuck what the report says.

A creates guard helps if the existence of that file really proves the operation finished. But if the file can exist after partial initialization, you have a problem. Write the marker too early and a failed setup becomes a permanent no-op.

I would test an empty target and a correct target, then try a partly configured target to see which assumptions fall apart there. The correct one needs that second run, too. I'd want to see what happens when the terminal goes away and nobody is sitting there keeping track of where the script got to.

References

  1. Idempotency en.wikipedia.org ↩
  2. Ansible glossary docs.ansible.com ↩
  3. ansible.builtin.shell docs.ansible.com ↩
ansibleautomationidempotencysysadmin

More on this beat