Technology Security Analyst
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.
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:
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.
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.
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.
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
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
debug:
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
debug:
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