X

Command and Shell Module in Ansible: Differences and practical usage

Ansible consists of different types of modules to perform different types of required tasks for the ansible to work. Such modules help to do almost all the tasks in ansible. Those ansible modules directly involve the ansible hosts or nodes and perform the task on such nodes. Some of the most known modules are apt, yum, shell, raw, command and many more.

In this article, we are going to discuss the difference between the two modules which are frequently used on ansible. These modules are command and shell modules. In many ways, both have the same work to perform and look similar but as we go deep into actions, there are many differences between them.

Difference between two Ansible modules: Command and Shell module

  • In Ansible, the command module is often used for many cases to be executed on the selected nodes or hosts. In the command module, the execution of command is not executed through the shell which results in not working of variables like $HOME, $HOSTNAME and the operations like ‘|’, ‘*’, ‘<’, ‘>’, ‘&’ and ‘;’. But all these can be achieved and works by using the shell module. So, shell commands are used in such cases.
  • A shell module is used for the execution of the shell commands against the target like UNIX-based hosts but it affects the user’s environment which is not a good thing in the real environment. So in this case, the command module is good for the security purpose and to run the required command more securely.
  • In Ansible, the command module provides commands to execute on all the listed nodes or hosts. Such executed commands will not be processed through the shell. While the shell module is generally used for the execution of all the shell commands against the target UNIX-based hosts. Such shell modules execute commands on nodes or shell scripts.
  • In Ansible, the shell module executes the shell command on the remote hosts. It uses /bin/sh to execute the command by default. Also, commands can be executed using the /bin/bash by passing the executable arguments but in the command module, the execution of command is not executed through the shell

To get the details on the difference between ansible command module and ansible shell module, let’s see some examples to use those modules on the different cases.

Ansible “command” modules with some cases

To get the hostname and version of host servers

Here, we are going to use the “uname -a” command module to get the hostname and version of hot servers.

With Ad-Hoc command:

$ ansible servers -m command -a "uname -a"

With playbook:

$ vim uptime.yml

Playbook Result:

$ ansible-playbook uptime.yml

To get the disk usage of host servers

Here, we are going to use the “df -h” command module to get the disk usage of host servers.

With Ad-Hoc command:

$ ansible servers -m command -a "df -h"

With playbook:

$ vim diskusage.yml

Playbook Result:

$ ansible-playbook diskusage.yml

Here in both examples, you can see the use of a simple command module. But if you want to use operators or piping too then using shell command is recommended. Now, we are going to show some examples for the shell command to be more clear.

Ansible “shell” modules with some cases

To run the shell command with pipe and redirection

We are using shell commands with the use cases of piping too in this example. You can look below for further details.

With Ad-Hoc command:

$ ansible servers -m shell -a "ls -ld /tmp | tr -d tmp"

With playbook:

$ vim pipe_redirection.yml

Playbook Result:

$ ansible-playbook pipe_redirection.yml

To run the shell command with single command

We are using shell commands with the use cases of piping too in this example. You can look below for further details.

With Ad-Hoc command:

$ ansible servers -m shell -a "date"

With playbook:

$ vim date.yml

Playbook Result:

$ ansible-playbook date.yml

Conclusion

As you see the examples for both the command and shell module. Shell module has more flexibility with the use of operators and piping too but it has other limitations such as security of user’s environments. So ansible commands are recommended in those cases. Use them as per your requirements for the best outcome. Thank you!

Categories: Linux & Unix
Karim Buzdar: About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various websites.
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings