Ansible Shell Module

Ansible Shell Module
Written by Nilima PaulJanuary 27, 2022
10 min read
Nilima Paul

Technology Security Analyst

In this blog post, We will discuss Ansible Shell Module.

Introduction to Ansible Shell Module

Ansible has a ton of such modules which are an immediate substitution of your orders or set of orders on track frameworks. Even though we take care of practically every one of the things in Ansible, we once in a while need orders to be straightforwardly executed on remote objective hosts as we use in Bash or Shell prearranging. This empowers us to keep the innovation of execution mode in the accessible shell. For this sort of prerequisite, we utilize the Ansible shell module for Linux-based working frameworks and win_shell for Microsoft Windows-based working frameworks. In this article, we will find out with regards to it and how to utilize it.

What is Ansible Shell Module?

In Ansible, we have a shell module that is used to run commands on /bin/shell on target remote machines. This module takes the commands as input along with a set of arguments. There are a few notes that one must remember while working with the Ansible shell module:

  • Freestyle order or order isolated by a semicolon are acknowledged, they can be given under cmd boundary, which we can find in the model segment.
  • This module is like the order module with the distinction that orders will be run on a shell
  • (/receptacle/sh) on remote objective machines.
  • To run an order safely and typically, then, at that point, rather than this module, use
  • order module.
  • To utilize a variable and acknowledged appropriately, use {{ var|quote }} rather than just {{ var }}. This will ensure no unique characters like semicolons will be figured out.
  • At the point when you want to run a contest, then, at that point, utilize the Ansible content module with a layout module, whenever required.
  • For reboot prerequisites, utilize the reboot or win_reboot module in playbooks.

How does the Ansible Shell Module Work?

For the Ansible shell module, similar to some other modules, we have a bunch of OK boundaries and as needs are satisfactory qualities. For a portion of the boundaries, there is default esteem. Underneath we will go through the majority of those on the off chance that not all.

  • – cmd: Below this, we give the command to be executed.
  • – chdir: Under this, we give the path of the directory to change into, before running the command.
  • – creates: To mention the filename that exists, then do not run the command.
  • – executables: To give the absolute path of the executable which is a shell under which the command will run.
  • – removes: To give the filename which if does not exist then do not run the command.
  • – stdin: To set the stdin of command.
  • – stdin_add_newline: Default is yes. This is to set for adding a new line to stdin data.
  • – warn: Default is yes. This is to enable task warnings.

Presently, we should comprehend the return esteems which can be given by the Ansible shell module, in light of the boundaries and contentions given to it. This is a significant angle, as our next undertakings in a playbook might be reliant upon the return esteem given by the shell module. The following are a rundown and a little portrayal of the return esteems for this module.

  • cmd: The command executed by the task.
  • delta: The time taken to run the command.
  • msg: if a change was made or not, True and False will be provided.
  • nd: The command execution end time.
  • RC: Return code for command, zero (0) means success, anything other than zero means there is some issue.
  • start: Start time of command.
  • stderr: Error in the command execution output, if any.
  • stdout: standard output of the command.
  • stdout_lines: Standard output of the command, split into lines.

Example of Ansible Shell Module

In this part, we will advance by doing taking a gander at certain models where we attempted to test the usefulness of the Ansible shell module. Yet, we will be aware of our lab climate before pushing forward in this part.

Here we have one Ansible regulator hub named as ansible-regulator. As target hubs, we have two remote machines. The primary machine is a Red Hat Enterprise Linux machine named as the host-one and the subsequent machine is a Ubuntu machine named as host-two. We will run our playbooks on the Ansible regulator machine and make changes on remote objective machines.

Example #1

In this example, we have a playbook like below, using which we will run some simple commands on remote target machines.

- hosts: all tasks:
- name: Here we will run a simple command on remote target machines shell: cat /var/tmp/sample.txt

After running this order like beneath we get underneath yield. Where we can see the stdout or stdout_lines segment for the standard result of the order execution. Additionally, as we have utilized the verbose mode while running the ansible-playbook order, we can see another return esteems too like order execution start, delta, end time and RC, and so on

ansible-playbook ansible_shell.YAML -v

Example #2

In this model, we have a playbook wherein we have utilized Ansible realities, conditionals, and one more method for providing order utilizing the cmd boundary.

hosts: all tasks:
name: Here we are checking & showing the /etc/Redhat-release on the machine based on condition block:
name: Here we are showing the /etc/Redhat-release on the shell: cat /etc/Redhat-release
register: var_redhat
var: var_redhat.stdout_lines
when: ansible_distribution == "RedHat"
name: Here we are checking & showing the /etc/lsb-release on Ubuntu machine based on condition block:
name: Here we are showing the /etc/lsb-release on Ubuntu shell:
cmd: cat /etc/lsb-release register: var_ubuntu
var: var_ubuntu.stdout_lines
when: ansible_distribution == "Ubuntu"

Presently running this playbook like underneath and having the beneath yield, we can see that when the Linux appropriation is RedHat, then, at that point, it will print the/and so on/Redhat-discharge record utilizing Linux feline order. Likewise, on Ubuntu framework/and so on/LSB-discharge document's substance is printed. We checked the OS on remote machines utilizing conditionals set over various Ansible squares.

ansible-playbook ansible_shell_arguments.yaml

Ansible Shell Module
Was this blog helpful?
You must be Logged in to comment
Code Block
Nilima Paul
Technology Security Analyst
201 Blog Posts
0 Discussion Threads
Trending Technologies
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Penetration Testing16

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance