Table of contents
1.
Introduction
2.
Ansible
2.1.
Ansible Shell
3.
Syntax of Ansible shell module in a playbook
4.
Syntax of Ansible shell module in Adhoc
4.1.
Example
5.
Execute multiple commands in a single shell
6.
Advantages
7.
Frequently Asked Questions
7.1.
What is the Shell in Ansible?
7.2.
How do you use shell commands in Ansible?
7.3.
What is the basic syntax of Ansible?
7.4.
Does Ansible use bash?
7.5.
How do I ping Ansible?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Ansible Shell

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Readers!!

Are you interested in learning about Ansible?

It is an open-source platform for automation and IT configuration management (CM) offered by Red Hat. Users may design repetitive tasks to happen automatically without learning a complex language because it employs YAML templates for humans.

So in this article, you'll learn about the Ansible - Shell. 

Let’s start and explore more about this.

Ansible

Ansible is a software program that automates cross-platform computer assistance in a straightforward yet effective way. It is primarily designed for IT professionals, who use it for almost all tasks performed on a weekly or daily basis by systems administrators, including application deployment, updates on workstations and servers, cloud provisioning, configuration management, and many other tasks. Ansible is simple to deploy because it doesn't require any additional security infrastructure or agent software.

Ansible Shell

The target UNIX-based hosts can be used to execute shell commands using the Ansible shell module. Except for highly sophisticated commands involving pipes and redirection, Ansible can run. Additionally, you can use the Ansible shell module to execute shell scripts.

Let's look at the Ansible shell module's syntax for use in Adhoc and playbooks:

Syntax of Ansible shell module in a playbook

The playbook's aesthetic appeal and writing quality are both excellent. A playbook is written in YAML to make it simple to read.

name: check Date of RemoteServer
shell:
     "date"
register: shellcmd
tags: checkdate

Syntax of Ansible shell module in Adhoc

Shell commands to be executed:

  • -m shell
  • -a “date”

Example

To use a Shell or command module to run a single command in a single task. Let's say you wish to obtain the remote server's date. Additionally, the remote server belongs to the hostgroup known as test servers.

Step 1:To access the Ansible server, log in.

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

---  
-name: Shell command example  
Hosts: test servers  
tasks:  
-name: check date with the shell command  
shell:  
"date"  
register: datecmd  
tags: datecmd  
-debug: msg= "{{datecmd.stdout}}"  

In the example above, we are running our script against the hostgroup test servers, running a straightforward date command, and putting the results of that command into a Register variable called datecmd.

The last line retrieves the registered variable and prints only the output of the date command that is saved in the datecmd object's stdout property.

Execute multiple commands in a single shell

In a single shell play, the Shell is capable of accepting a number of commands. Additionally, you can use the Ansible shell module to create your shell script.

The following stages are to be carried out according to the playbook in that order:

  • Put tomcatServer to sleep
  • Remove the cache
  • Shut down the log file.
  • beginning the instance
---  
  - name: Shell Examples  
    hosts: test servers  
    tasks:  
    - name: Clear Cache and Restart tomcat  
      become: yes  
      delay: 10  
      async: 10  
      poll: 50  
      shell: |  
        echo -e "\n Change directory to the Tomcat"  
        cd tomcat8/  
        echo -e "\n Present working directory is" `pwd`  
         
        echo -e "\n Stopping the tomcat instance."  
        bin/shutdown.sh  
        echo -e "\n Cleaning the tmp and work directory of tomcat"  
        rm -rfv tmp/*  
        rm -rfv work/*  
        echo -e "\nTruncate the log file"  
        > logs/Catalina.out  
        echo -e "\nDirectory listing"  
        ls -lrtd logs/Catalina.out  
        echo -e "\nStarting the instance"  
        bin/startup.sh      
      args:  
        chdir: "/apps/tomcat/"  
      register: fileout  
      tags: fileout  
    - debug: msg="{{ fileout.stdout_lines }}"  

Advantages

The key benefit of the Ansible shell is that it does not have any high complexity commands that use pipes or semicolons, which can be a security drawback since a single error could result in increased costs and compromise the integrity of the system.

  • Only LINUX-based computers and not Windows-based ones can use the Ansible shell module. You ought to utilize the winning shell for Windows.
  • Shell scripts can be run using the Ansible shell module. The shell script is transferred from the control system to the remote server using the dedicated Ansible module called script.

Frequently Asked Questions

What is the Shell in Ansible?

The target UNIX-based hosts can be used to execute shell commands using the Ansible shell module.

How do you use shell commands in Ansible?

The command name is passed to the shell module, followed by a list of arguments separated by spaces.

What is the basic syntax of Ansible?

Ansible uses YAML syntax for expressing Ansible playbooks. 

Does Ansible use bash?

By default, the shell module uses the /bin/sh Shell to execute commands, but by supplying the executable parameter, it is possible to utilize other shells, such as /bin/bash.

How do I ping Ansible?

Use the following command to see if Ansible can connect and execute commands and playbooks on your nodes:-m ping all with Ansible.

Conclusion

This blog has extensively discussed Ansible-Shell in this article.

If you want to learn more, check out the excellent content on the Coding Ninjas Website:

Ansible Interview Questions Part 1Ansible Interview Questions Part 212 Best DevOps Tools To Get Acquainted With, and DevOps Interview Questions.

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

For placement preparations, visit interview experiences and interview bundles.

Thank You Image

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass