In this video, we will be running ansible ad hoc commands.
Now, of course we will be covering quite a few different ad hoc commands.
So what I have done is I have divided this video into different parts.
Now here is the part one where you cover a few commands of ad hoc.
All right, first thing, first ad hoc commands run on as needed basis.
And usually for those tasks that do not repeat.
If you repeat some of your tasks frequently, then it's better that you create playbooks for those tasks.
All right, so let's take an example.
Syntax for ad hoc.
Ansible Command are as follows.
It's just.
Ansible So you don't run Ansible Dash Playbook Command, because with that command you have to specify
the playbook, location or the file.
So since this is an ad hoc command, we will just use a simple ansible command.
All right, then, ansible space.
The target, which means aware.
On which system do you want to run?
This ad command could be either or all so all meaning it will go to your etsi ansible host file and
all the targets, all the remote clients listed there will be targeted.
Or if you have aliases there or if you have a grouping there, you could define a right here.
All right, then comes the minus or hyphen M, which is the option for module?
Yes.
You specify the module right here, which module you will be using to execute that task.
Then come minus a, which is the module option.
What exactly you're doing with it.
All right.
If you notice here, there is no minus a so there is no option for name.
Here you can specify the name, but there is no reason to put the name.
It's better to put the name of your task inside of the playbook so you would know exactly what this
playbook is doing, or when you are running it, you will see which step it's on.
All right.
So here are the list of examples for Ansible add commands and the very first command that we will run
as ad hoc command is the ping command ping, the local host.
And if you are pinging the local host without a playbook, which I have covered previously and I'm sure
you remember, it's just that simple.
Ansible Space local host space minus M for module and space.
And what do you want?
Which module you want to run?
And the name of the module is Ping.
All right, so let's go to our Ansible control node and run this ad hoc example command onto our machine.
All right, so right here, our control node is up and running.
I'm going to open up the terminal window.
And simply we'll run this ad hoc command ansible local host minus M4 module and which module we are
using.
We are using a ping module and this is the attached command.
You do not need any playbook here you have the result local host to the success is the return code that
you have and ping and pong and everything looks green, which means has no red fonts or anything response
in red.
So nothing happened to that ad hoc command.
All right.
Let's move on.
The next ad hoc command will be creating a file on all remote clients.
All right, so ansible space all for all the clients in at CES will host file and the module will be
used with minus m option.
The module name is file and minus a four module options, which is the path where it's going to create
the file.
The location of the file and the state is to create with the touch and the change mod r permissions
will be 700.
All right.
Let's go to our.
Sorry.
Our Ansible node.
And let's run this command and symbol.
All and for all.
What do we have?
And all?
Once again, let's check CAD at sea.
Let's do more at sea.
Ansible.
And hosts.
Then we have 10 to 53 1.115.
This is the IP address.
We have our for remote client.
So let's first ping it to make sure it is payable 150 3115 because you never know the IP can change
since we are running a remote client which is right here on DHCP.
All right.
So it is being able clear the screen and symbol all minus m file minus a option double code the path.
Where do you want to create that file home.
I absol and call it ad hoc one.
All right.
That's what we did.
That's minimized this.
Let's move this right here and let's move this down so we could see it.
The complete command.
All right.
So that's the name of the file and the location.
Now, the state you wanted to create using the touch command and the mod, meaning the permission.
Let's put it 700 and let's run the command.
Let's move up.
And while it's running, we got the yellow messages back, which means it has changed what we have asked
for it.
So right here is changed, destination is created and all the other information that you need.
So let's go to our remote client.
Let's go to open terminal.
All right, right here.
We'll go to home.
I have soul, and we'll see if it created ad hoc one.
File.
Right here is the ad hoc one file created by root because we ran the command route and we gave it the
700 permission, which is read, write, execute only for its own self.
Beautiful.
All right.
Let's go to the second ad hoc command.
Deleting a file on all remote clients.
So if you want to delete the same file, you can run the same command or same module for the command,
which is file.
But instead of state, you put touch, you put it absent.
All right, let's go into our Ansible control node.
Hit up arrow key simply to bring back the last command.
And here, get rid of everything until state equal and put absent.
This will allow us to delete a file.
So many times you're going to see issues in your multiple servers which you want, where you want to
delete one file, and of course you don't want to write a playbook for it, then you just run it as
an ad hoc command.
All right.
So we want ad hoc zero one file that we created in our last step to be deleted.
All right.
So it's just that simple.
Hit enter and it should take care of it.
Let's verify it on our client.
Let's do LS minus LTR again right here.
When we ran it earlier, the file was there list run it again and the file is gone.
So it did delete it.
All right, next move.
Let's move on to the next command.
Copy a file to remote clients.
So copy from one location to another.
Right.
So we will copy a file from our control node to our remote client.
All right, so the copy will use the copy module and for the option, the source.
Where do we copy it from?
I have it in slash temp and we have it as ad hoc and destination.
So let's go to our Ansible control node.
The first thing we'll do will create the file.
So we'll go to temp and we'll create it with the touch command ad hoc to.
All right, the file is created right here is the file.
Let's put something in that file.
So, Echo.
This is so echo.
Let me put this in code.
This is.
And ad hoc.
Test file.
All right.
And then I'll put that to our ad hoc to file that we just created.
Let's confirm the content is in there.
Ad hoc.
Yes, it is in there.
All right, so now we have a file.
Now we need to copy that file over to our remote client so we will run our ad hoc command.
And that would be Ansible all for all the clients that is Ansible file.
Sorry.
Host Well, and we'll use the copy module option.
The source.
Slash temp slash ad hoc two.
And then the destination.
Where do you want it?
To copy it.
Home Ihev saw an ad hoc, of course, for you.
You'll put your username instead of my username.
All right, so let's do that and hit enter.
All right, moment of truth.
Let's cross the finger and let's hope it copies over.
We got the yellow font back.
The color of the font.
So which means it has changed.
It worked.
Nothing in a red, so nothing failed.
Let's check on our.
Remote client.
Here we are in slash home, slash of and let's see if it copied over the file it did ad hoc to.
All right.
Let's confirm if this is the same file by doing CAD ad hoc and see if it has the same content that we
added in our control node and it does have a same content.
This is an ad hoc test.
Well, that's just awesome.
How we could run ad out commands on our multiple remote clients to perform a single task that is not
needed later on.
All right.
I'll see you in the next part.