Ansible – Command vs Shell Module

The Shell Module as well as command modules are used to execute Binary Commands.

The command module is the default module to execute the binary commands, if we do not specify the module name in our ad-hoc command, ansible will by default use the command module.

The command module will execute the commands without proceeding to through the shell.

The command module will not recognize certain variables like $HOME

Stream operators like <,>, &, and | will not work with command modules.

The command module is more secured, compared to the shell module

For example, both shell and command module will have the same outputs,

ansible localhost -m shell -a "uptime"
ansible localhost -m command -a "uptime"

The stream operator, when passed through the shell and command module, will show different results, as the command module doesn’t recognize the stream operators and will throw errors, whereas the same will work fine with the shell module.

Command Module, will throw an error as it will not recognize > stream operator

ansible localhost -m command -a "uptime > uptime.txt"

Shell Module

ansible localhost -m shell -a "uptime > uptime.txt"