Javatpoint Logo
Javatpoint Logo

Ansible Shell

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.

The main advantage of the Ansible shell is except any high complexes commands with pipes and semicolons can be a disadvantage from the security perspective as a single mistake could cost a lot and break the system integrity.

  • The Ansible shell module is designed to work only with LINUX based machines and not for the windows. For windows, you should use the win_shell
  • Ansible shell module can be used to execute shell scripts. Ansible has a dedicated module named script, which is used to copy the shell script from the control machine to the remote server.

Let see the syntax of how to use the Ansible shell module in the playbook and Adhoc:

Syntax of Ansible shell module in a playbook

The beauty of the playbook is the way it looks and written. A playbook is written in YAML so it can be easily understood.

The below image demonstrates how an Adhoc command would be transformed as a play of an Ansible playbook.

Ansible Shell

Syntax of Ansible shell module in Adhoc

The below image shows a quick syntax of the Ansible shell module in Adhoc manner.

Ansible Shell

Example

To execute a single command in a single task using a Shell or command module. Suppose you want to get the date of the remote server. And the remote server is under the hostgroup which name is testservers.

Step 1: Login to the Ansible server.

Step 2: Below is an example that executes a single command using the Shell module in a remote host.

In the above example, we are running our playbook against a hostgroup named testservers and executing a simple date command and saving the output of that command into a Register variable named datecmd.

At the last line, we retrieve the registered variable and printing only the date command output stored in the stdout property of datecmd.

Example 2: Execute multiple commands in a single shell:

The Shell can accept various commands together in a single shell play. Also, you can write your shell script with the Ansible shell module.

In the below example, we grouped some shell commands to execute a controlled and clean tomcat restart.

The playbook is designed to execute the following steps in order, such as:

  • Stop the tomcatServer
  • Clear the cache
  • Truncate the log file
  • Start the instance

Next TopicAnsible Templates





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA