In this video, we are going to run a Shell script on our Linux remote clients.
Now shell scripts are written so you could automate some of your tedious tasks as well.
You could write shell scripts in either bash shell script, seashell script, or corn shell script.
Now, what we will do in this video is we will write a playbook which will run your shell scripts that
are located on your remote client.
And for us, our client is the Linux client one.
You should have a script located on your remote client that you want to run by Ansible, but if you
do not have a script already, then what we will do is we will create the script and we will create
it in home.
IFC Directory.
You could create the script anywhere or in any directory of your choice, but to be consistent, let's
put it in our home directory or IFC.
I'll use a home directory.
Then once we have that file, then the script.
What it should do, it will create a file called Example one.
It is just that small sample script.
Your script can do anything that you want.
But for this video's purpose, to show you how we could run or how we could execute shell scripts on
remote clients is using Ansible modules.
All right, so once we have that, then we will do as we will create our shell script, yaml file,
define our YAML format, we'll name the playbook.
The host could be either all or any specific host.
The task, the name of the shell or the name of the task, actually.
And in this case, run shell script.
Now we need to define the module, the module that can be used to run the shell script on the remote
client.
And for that you have to use the module shell and then you define the absolute path of that shell script
where it's located.
And the last thing is we will run that YAML file.
All right, so let's go to the very first thing is let's go to our client.
Here is our Linux client one.
We'll go to slash home slash.
I have solid directory, by the way.
I'm logged in as a root.
Who am I?
And I am in slash home.
Slash.
I have solved directory.
You could create your shell script either by IFCO or as root.
It doesn't really matter.
All right, so let's first create our shell script.
So them see file.
This is the name I just picked randomly to create a shell script.
All right, let's define that.
It is going to be been a bash shell script, and we want to actually create a file called Example one.
And for that, we'll use the command touch.
All right.
So that's pretty much it.
It's just that simple script.
And let's save it now.
Let's go ahead and see if that C shell file has executable permission, which it does not.
So we will give the executable permission search mod or change mod a exe c file.
All right.
This has the executable permission.
Now, before we run this Shell script through Ansible.
Let's run it on the client on this machine just to make sure that it works.
All right.
For that, we'll just do simply dot slash C file, S-H hit enter.
Now, this should create a file example one in this current directory and there it is.
Our example of one file has been created.
Perfect.
Now let's go ahead and delete that.
So this way, when we create with Ansible, we should have a proof that Ansible created the file.
All right, let's delete it.
Let's make sure it does not have.
Example one file.
Note it does not.
Perfect.
Now let's go to our control node.
Right here is our control node.
Let's log in as route.
I am in my route directory.
Let's go to Route Ansible.
Sorry, not route.
It is an Etsy Ansible playbooks.
These are where all our playbooks are located.
And we'll use the same location to write all of our playbooks.
All right, so we'll do them and we'll call it shell script, dot yaml.
Dash, dash, dash to define it is a gimbal file.
The name of the playbook playbook for shell script.
All right then.
Host, where do you want to execute it this time?
Instead of picking all, I'm just going to define the IP address of our host, which is 10.2 53 1.115.
This is the IP address of our client.
Now let's make sure that it has not changed because it is running on dhcp.
So I did if config.
Let's move up and we'll see.
ENP zero three still have 10 to 53 1.115 IP.
All right, good.
Let's go back to our control node.
All right, so now the tasks.
What do you want it to run?
Let's name the task that it will run.
Shell script.
All right.
And what module that you will use to run that task?
We will use the module shell.
All right, put double code slash home.
Where is the location of your script?
I have saw Slash.
See that S-H double code.
Close.
And save the file.
All right, the file is saved.
Now let's run it.
Let's see if he have written the shell's the java file correctly.
Enter.
All right, play.
Play for shell script task.
Task is for our defined host right here sheet and right here is telling it that it ran, it changed
and it was successful.
Nice.
So now the only way to confirm it is by going to our client and see if it has created the example.
One file in slash home and slash EOF solved.
All right, I am in slash home.
I have solved directory.
Let's do ls minus l tr and oops it did not run.
Hmm.
Let's see why I did not run.
Maybe when we ran the file, which was slash home, slash IFC file, it created the file not in the
same directory.
It created the file in slash root directory.
Because right here the root is the one that is going into this machine and running that.
So just let's do a CD and hit enter and this is going to bring me back to the root home directory.
So let's do p.w dx and I am in slash root now let's do ls minus l tr and let's check if it created example
one in this directory.
And there you go.
That's what I have a hunch on I would say, and that's where it created the file on.
If you want to create the file in a different location through the shell script, then you go to the
shell script slash home slash I of saw and see file.
Sorry, that's vim shell script.
At sea.
Sorry.
Home.
I have saw sea fall.
And right here, when we say touch example one will define the absolute path home I saw slash.
All right.
Now it should regardless where I am running it from which location on the control node, where it's
on the client.
When we run it right here, let's run it again.
It will create that file in our slash home slash I of saw directory and do a list myself here and you'll
see right here it created.
All right.
So that was an example of running or creating a playbook that will execute your shell scripts on your
Linux remote clients.