Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

What is the difference between the `command` and `shell` modules?

BeginnerConceptAd-hoc Commands, Modules and Idempotency
Answer

command runs the program directly, without a shell. Pipes, redirects, &&, globs and variable expansion like $HOME do not work. shell passes the string to /bin/sh -c, so all of those work.

- ansible.builtin.command: /usr/bin/uptime
- ansible.builtin.shell: ps aux | grep nginx | wc -l

Prefer command because it is safer (no shell-injection surface) and more predictable. Use shell only when you truly need shell features. Both always report changed unless you add creates, removes or changed_when.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency