When should you write a custom module instead of using `shell`, `uri` or a role?
AdvancedProductionArchitecture Decisions and Tool Comparisons
Answer
Write a module when:
- The same multi-step
uriorshelllogic appears in several roles, and it needs real idempotence (read current state, compare, change only the difference). - You need check mode and diff support for something with no existing module.
- The logic is too complex to express cleanly in YAML and Jinja2.
Do not write one when an existing collection already covers the API, or when a single command with creates or changed_when is enough.
Costs to accept: Python code to maintain, argument_spec and documentation, unit and ansible-test sanity tests, and support across ansible-core versions. Package it in an internal collection rather than a role's library/ folder once more than one team uses it.
More on Architecture Decisions and Tool Comparisons
Ansible or Terraform: where do you draw the line?BeginnerHow does Ansible compare with Puppet, Chef and Salt?IntermediateWhy use Ansible instead of shell scripts?BeginnerWhere does Ansible fit once you run Kubernetes?Intermediatecloud-init or Ansible for first boot configuration?IntermediateWhen would you use AWS Systems Manager instead of, or together with, Ansible?Intermediate