I'm sure in your Linux system administration career, you probably have experienced a situation where
you have a process running in your Linux machine and you wanted to kill it.
You don't need the process.
It's not just shutting it down, so you have to forcefully kill it.
Now, if the same process is on multiple systems, then of course there's a lot of work, tedious work.
You have to log into every system and kill that process.
So now we have Ansible Automation, which could do all that work for you.
So in this video we are going to write a playbook and we'll name it Kill Process, Die YAML file.
This playbook is going to find a running process by the process name that you define inside the playbook.
Then we will.
We'll let the playbook know that you could ignore any error messages that comes back.
Hold the results in the registry variable.
What is the result?
Result will be going to be the process ID that you want.
We want to kill.
Hold that in a variable or a container, then use a shell module and run kill command to kill the registered
variable or the process id.
All right.
This is how our playbook looks like to kill the process.
Start with dash, dash, dash.
The name of find a process and kill it and which host we want to run it against the task right here.
You notice the name of the task.
Ignore errors, as I mentioned, ignore errors.
Shell.
Now here is the command which we will use to get the name or to get the process ID of the process that
you wanted to kill.
I'm using top as an example to show you how we could kill a top process.
Then we'll register the process ID as the variable and call the variable running process name.
The second is the task.
Ignore errors and shell and use the item and this is the variable and we use width items with the running
underscore process.
This is the variable dot standard out underscore lines.
This is something that you have to remember whenever you want to kill a running process and you get
the result and use that result.
And then of course at the end we will use Ansible playbook, kill process.
All right.
Let's go straight into our.
Our client, terminal, remote, terminal client.
And here I wanted to run top command.
Here the top command is running.
Once I finished writing my playbook and executed, it should kill it and I should get my prompt back.
All right.
For that, let's go to our.
Control node.
Here is our control node.
Now.
Okay, now let's go to our Etsy playbooks.
I'm logged in as root.
Now let's use Vim editor and name it kill process yaml.
Here, define the YAML name.
Find a process and kill it.
Host.
You could put all or be specific to which host you wanted to go to.
This is the IP address of my client, so I'm just putting in an IP address.
Or you could put all now the tasks and the name of the task get running process.
From remote host.
All right.
Now ignore any error message that you get and still go ahead and execute or kill it.
Yes.
Now, Shell, what do we want?
How are we going to get the process ID we will run this command PS minus F or f w doesn't really matter.
Whichever you prefer.
And the process we are getting is the top and asking it to get its column number to.
And closing it and double coding it.
And if you notice, this is exactly what we're doing right here.
And.
Yes, okay, good.
Now, register the.
Process ID that we get after running the above command running process.
All right, then we go in and put in a new task name kill running process.
Ignore error here as well.
And Shell.
What shell or what command that you wanted to run.
You wanted to run simple Linux Command, which is the kill command with the defined variable item.
Hold that in here.
Oops.
And then double click close.
All right.
And with the items we wanted to do is.
Double coat codebases curly braces running underscore process that sdd out underscore line and this
should be.
UPS.
Closing the curly braces, curly braces closed, curly braces closed and double code closed.
All right.
I hope I don't have any errors, and I hope it runs fine.
Let's save it.
All right, before we run it, let's verify one more time.
You have the top running the background.
Good.
Now let's move this to the left and run it.
And symbol.
Playbook.
Kill processed animal.
All right, again, moment of truth.
Let's cross your fingers and let's run it.
Oh, it's giving us an error message.
It says the error appears to be in Etsy's playbook.
Process line six, column 13, but maybe elsewhere.
And right here, task name, get running process from the remote.
This is where the issue is.
Line six.
Nine six.
Okay.
Name?
And that's to Colon six.
Oh, I found a problem right here.
You notice.
Ignore.
Underscore error.
I missed the S.
All right.
So the s is right here.
Now.
Save it.
Now let's run it.
See, this one little tiny mistake can cause a huge problem.
So be very careful when you write playbooks.
All right, so let's run the playbook again.
Assemble the playbook.
All right, so let's run it.
Find a process and kill it.
Gathering facts.
This is the host.
All right.
Another error message field.
The 10 to 51 off a failed message.
Direct object has no attribute.
Steady out.
Underscore line.
Let's see what we are doing.
With items.
Colon running underscore process.
Dot steady.
Out here.
I missed s again.
I'm sorry, guys.
Here is the se and save it.
All right, let's clear the screen here and let's run it again.
All right.
So now it worked.
The reason you are getting this failed error message, which you could safely ignore it is because when
this Ansible playbook ran, it found a couple of additional processes and one of the process that it
found was its own self while it was running.
And the grep command of itself.
When you do grep for top, that becomes a process of its own.
So it found those two processes and then it tried to kill those process, but it couldn't find it.
So that's fine.
You could safely ignore them and it did what it's supposed to do.
It stopped our top process.
So that's how you could kill a running process or a defined process that is running.
And you wanted to kill it on your remote client.