Interview Prep/Ansible/Architecture Decisions and Tool Comparisons

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 uri or shell logic 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.

All Ansible questions Roadmap: Ansible

More on Architecture Decisions and Tool Comparisons