I am new to Ansible, and am trying to understand its best practices. My team uses Ansible for system setup and product deployment.
I am reading that idempotance is the foundational virtue, where playbook idempotence is defined as not doing any state changes on consecutive runs.
I am not sure I understand why.
The scenarios I am looking at:
- Initial deployment of a system: before the playbook starts nothing is installed nor running. After playbook completes, everything is installed and is running.
- Update of a system: before the playbook starts everything is installed and running. After playbook completes, everything is installed and is running. Some changes may have been applied.
A simple way to implement both scenarios in a single playbook:
-
State of the system
- not running
- uninstalled
-
State of the system
-
installed
-
running
The above implementation would require state changes on consecutive invocations.
For initial deployment, the first part is a no-change.
For system update, both parts play.
In order to implement these scenarios in a playbook that doesn’t do state changes on consecutive installations, the playbook would need to be aware of internal dependencies between things that can be in states {installed, uninstalled}, and things that can be in states { running, not running}
- Install A
- Notify Runnable-depends-on-A
- Install B
- Notify Runnable-depends-on-B
This approach makes Ansible playbook tightly coupled to system dependencies. Should these dependencies change, the playbook becomes yet another place that needs to be updated. Depending on the system implementation specifics, having dependencies specified incorrectly in Ansible playbook may cause a hard-to-reproduce bug.
Appreciate any thoughts on why avoiding state changes on consecutive playbook runs is preferred over simplicity and robustness in implementation.