Why prefer a dedicated module over `shell`/`command`?
BeginnerConceptAd-hoc Commands, Modules and Idempotency
Answer
Modules check state before acting, report changed accurately, support check and diff mode, return structured data, handle platform differences and escape arguments safely. A shell: apt install nginx always reports changed and fails ugly on a RHEL host, whereas ansible.builtin.package works on both and does nothing the second time.
When no module exists, wrap the command with creates/removes/changed_when so it is still safe to re-run.
More on Ad-hoc Commands, Modules and Idempotency
What is an ad-hoc command and what is its syntax?BeginnerWhat does `ansible all -m ping` actually test?BeginnerGive practical use cases for ad-hoc commands.IntermediateWhat is the difference between the `command` and `shell` modules?BeginnerCompare `raw`, `command`, `shell` and `script`.IntermediateWhat is idempotency and why does it matter?Beginner