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.
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.IntermediateCompare `raw`, `command`, `shell` and `script`.IntermediateWhy prefer a dedicated module over `shell`/`command`?BeginnerWhat is idempotency and why does it matter?Beginner