Difference between shell and command in ansible

Ansible

Ansible Problem Overview


I am new to ansible world can anyone help me in understanding the difference between shell and command in ansible. When to use shell and when to use command. I know one use case

command module is more safe, as it is not affected by the user’s environment.

Ansible Solutions


Solution 1 - Ansible

> The Ansible Shell Module allows you to run arbitrary commands on a remote host, just like you were logged into the shell. The Shell and Command modules are very similar, the major difference being that the shell module does not escape commands, allowing you to use shell operators like redirects ("greater than", "less than"), pipe ("|") and boolean operators ("&&", "||"). This does mean that the Shell module is susceptible to command injection/shell injection, but this is easy enough to overcome by using the "quote" filter when using variables with the Shell module.

Reference: Ansible Shell Module Tutorial - Complete Beginner's Guide

Solution 2 - Ansible

according to documentation :

shell – Execute shell commands on targets

> It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.

and:

command – Execute commands on targets

> The command(s) will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", ";" and "&" will not work. Use the shell module if you need these features.

Categories

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprashantView Question on Stackoverflow
Solution 1 - AnsibleRAJAT RAWATView Answer on Stackoverflow
Solution 2 - AnsibletassinpView Answer on Stackoverflow