Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between shell and command in ansible

Tags:

ansible

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.

like image 289
prashant Avatar asked Jun 19 '19 08:06

prashant


People also ask

What is shell in Ansible?

Ansible shell module is designed to execute the shell commands against the target UNIX based hosts. Ansible can run except any high complexes commands with pipes, redirection. And you can also perform the shell scripts using the Ansible shell module.

What is the primary difference between shell and command module?

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 ("&&", "||").

How do you use shell commands in Ansible?

Synopsis. The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell ( /bin/sh ) on the remote node. For Windows targets, use the win_shell module instead.


2 Answers

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

like image 67
RAJAT RAWAT Avatar answered Sep 22 '22 22:09

RAJAT RAWAT


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.

like image 34
tassinp Avatar answered Sep 24 '22 22:09

tassinp