1 00:00:00,600 --> 00:00:07,170 I'm sure in your Linux system administration career, you probably have experienced a situation where 2 00:00:07,170 --> 00:00:11,730 you have a process running in your Linux machine and you wanted to kill it. 3 00:00:11,760 --> 00:00:13,080 You don't need the process. 4 00:00:13,080 --> 00:00:16,810 It's not just shutting it down, so you have to forcefully kill it. 5 00:00:16,830 --> 00:00:22,460 Now, if the same process is on multiple systems, then of course there's a lot of work, tedious work. 6 00:00:22,470 --> 00:00:25,640 You have to log into every system and kill that process. 7 00:00:25,680 --> 00:00:30,420 So now we have Ansible Automation, which could do all that work for you. 8 00:00:30,810 --> 00:00:36,720 So in this video we are going to write a playbook and we'll name it Kill Process, Die YAML file. 9 00:00:36,750 --> 00:00:43,950 This playbook is going to find a running process by the process name that you define inside the playbook. 10 00:00:44,250 --> 00:00:45,410 Then we will. 11 00:00:45,480 --> 00:00:50,370 We'll let the playbook know that you could ignore any error messages that comes back. 12 00:00:50,880 --> 00:00:53,670 Hold the results in the registry variable. 13 00:00:53,670 --> 00:00:54,500 What is the result? 14 00:00:54,530 --> 00:00:57,890 Result will be going to be the process ID that you want. 15 00:00:57,900 --> 00:00:59,010 We want to kill. 16 00:00:59,040 --> 00:01:08,790 Hold that in a variable or a container, then use a shell module and run kill command to kill the registered 17 00:01:08,790 --> 00:01:10,830 variable or the process id. 18 00:01:11,520 --> 00:01:11,820 All right. 19 00:01:11,820 --> 00:01:15,840 This is how our playbook looks like to kill the process. 20 00:01:16,230 --> 00:01:17,910 Start with dash, dash, dash. 21 00:01:17,910 --> 00:01:24,810 The name of find a process and kill it and which host we want to run it against the task right here. 22 00:01:24,810 --> 00:01:27,060 You notice the name of the task. 23 00:01:27,120 --> 00:01:29,450 Ignore errors, as I mentioned, ignore errors. 24 00:01:29,460 --> 00:01:30,120 Shell. 25 00:01:30,150 --> 00:01:39,240 Now here is the command which we will use to get the name or to get the process ID of the process that 26 00:01:39,240 --> 00:01:40,110 you wanted to kill. 27 00:01:40,140 --> 00:01:45,120 I'm using top as an example to show you how we could kill a top process. 28 00:01:45,390 --> 00:01:51,850 Then we'll register the process ID as the variable and call the variable running process name. 29 00:01:51,870 --> 00:01:53,640 The second is the task. 30 00:01:54,000 --> 00:02:00,930 Ignore errors and shell and use the item and this is the variable and we use width items with the running 31 00:02:00,930 --> 00:02:01,860 underscore process. 32 00:02:01,860 --> 00:02:05,070 This is the variable dot standard out underscore lines. 33 00:02:05,070 --> 00:02:10,380 This is something that you have to remember whenever you want to kill a running process and you get 34 00:02:10,380 --> 00:02:12,240 the result and use that result. 35 00:02:12,390 --> 00:02:16,380 And then of course at the end we will use Ansible playbook, kill process. 36 00:02:17,190 --> 00:02:17,490 All right. 37 00:02:17,490 --> 00:02:20,070 Let's go straight into our. 38 00:02:22,240 --> 00:02:26,050 Our client, terminal, remote, terminal client. 39 00:02:26,050 --> 00:02:29,050 And here I wanted to run top command. 40 00:02:29,560 --> 00:02:31,360 Here the top command is running. 41 00:02:31,480 --> 00:02:38,690 Once I finished writing my playbook and executed, it should kill it and I should get my prompt back. 42 00:02:38,710 --> 00:02:39,030 All right. 43 00:02:39,040 --> 00:02:41,260 For that, let's go to our. 44 00:02:42,920 --> 00:02:43,730 Control node. 45 00:02:43,760 --> 00:02:45,320 Here is our control node. 46 00:02:46,260 --> 00:02:46,360 Now. 47 00:02:46,550 --> 00:02:50,810 Okay, now let's go to our Etsy playbooks. 48 00:02:51,260 --> 00:02:53,060 I'm logged in as root. 49 00:02:53,360 --> 00:02:59,240 Now let's use Vim editor and name it kill process yaml. 50 00:02:59,840 --> 00:03:03,800 Here, define the YAML name. 51 00:03:05,030 --> 00:03:08,870 Find a process and kill it. 52 00:03:09,740 --> 00:03:10,470 Host. 53 00:03:10,730 --> 00:03:17,030 You could put all or be specific to which host you wanted to go to. 54 00:03:18,200 --> 00:03:22,910 This is the IP address of my client, so I'm just putting in an IP address. 55 00:03:23,330 --> 00:03:33,890 Or you could put all now the tasks and the name of the task get running process. 56 00:03:36,260 --> 00:03:38,000 From remote host. 57 00:03:38,810 --> 00:03:39,550 All right. 58 00:03:39,560 --> 00:03:46,040 Now ignore any error message that you get and still go ahead and execute or kill it. 59 00:03:47,300 --> 00:03:48,130 Yes. 60 00:03:48,140 --> 00:03:50,300 Now, Shell, what do we want? 61 00:03:50,330 --> 00:03:57,870 How are we going to get the process ID we will run this command PS minus F or f w doesn't really matter. 62 00:03:57,890 --> 00:03:59,060 Whichever you prefer. 63 00:03:59,810 --> 00:04:09,440 And the process we are getting is the top and asking it to get its column number to. 64 00:04:10,110 --> 00:04:14,310 And closing it and double coding it. 65 00:04:14,880 --> 00:04:18,750 And if you notice, this is exactly what we're doing right here. 66 00:04:18,750 --> 00:04:19,260 And. 67 00:04:19,260 --> 00:04:21,210 Yes, okay, good. 68 00:04:21,540 --> 00:04:24,210 Now, register the. 69 00:04:25,190 --> 00:04:32,180 Process ID that we get after running the above command running process. 70 00:04:33,330 --> 00:04:42,840 All right, then we go in and put in a new task name kill running process. 71 00:04:44,620 --> 00:04:46,990 Ignore error here as well. 72 00:04:51,780 --> 00:04:53,100 And Shell. 73 00:04:53,100 --> 00:04:56,250 What shell or what command that you wanted to run. 74 00:04:56,430 --> 00:05:04,590 You wanted to run simple Linux Command, which is the kill command with the defined variable item. 75 00:05:05,160 --> 00:05:06,570 Hold that in here. 76 00:05:07,800 --> 00:05:08,160 Oops. 77 00:05:08,160 --> 00:05:11,220 And then double click close. 78 00:05:13,510 --> 00:05:14,050 All right. 79 00:05:14,050 --> 00:05:18,730 And with the items we wanted to do is. 80 00:05:19,860 --> 00:05:33,210 Double coat codebases curly braces running underscore process that sdd out underscore line and this 81 00:05:33,210 --> 00:05:34,350 should be. 82 00:05:36,110 --> 00:05:36,620 UPS. 83 00:05:37,620 --> 00:05:44,180 Closing the curly braces, curly braces closed, curly braces closed and double code closed. 84 00:05:44,220 --> 00:05:44,610 All right. 85 00:05:44,610 --> 00:05:48,620 I hope I don't have any errors, and I hope it runs fine. 86 00:05:48,630 --> 00:05:49,680 Let's save it. 87 00:05:51,920 --> 00:05:54,490 All right, before we run it, let's verify one more time. 88 00:05:54,500 --> 00:05:56,350 You have the top running the background. 89 00:05:56,360 --> 00:05:56,820 Good. 90 00:05:56,840 --> 00:06:00,920 Now let's move this to the left and run it. 91 00:06:01,100 --> 00:06:02,210 And symbol. 92 00:06:03,000 --> 00:06:03,870 Playbook. 93 00:06:04,600 --> 00:06:06,700 Kill processed animal. 94 00:06:07,300 --> 00:06:09,020 All right, again, moment of truth. 95 00:06:09,040 --> 00:06:12,610 Let's cross your fingers and let's run it. 96 00:06:13,100 --> 00:06:14,860 Oh, it's giving us an error message. 97 00:06:14,860 --> 00:06:17,460 It says the error appears to be in Etsy's playbook. 98 00:06:17,590 --> 00:06:21,760 Process line six, column 13, but maybe elsewhere. 99 00:06:21,760 --> 00:06:24,670 And right here, task name, get running process from the remote. 100 00:06:24,670 --> 00:06:27,610 This is where the issue is. 101 00:06:27,610 --> 00:06:28,570 Line six. 102 00:06:30,000 --> 00:06:30,990 Nine six. 103 00:06:33,410 --> 00:06:34,520 Okay. 104 00:06:34,550 --> 00:06:35,630 Name? 105 00:06:36,950 --> 00:06:39,170 And that's to Colon six. 106 00:06:39,620 --> 00:06:41,990 Oh, I found a problem right here. 107 00:06:41,990 --> 00:06:42,610 You notice. 108 00:06:42,620 --> 00:06:43,670 Ignore. 109 00:06:44,810 --> 00:06:46,760 Underscore error. 110 00:06:47,510 --> 00:06:49,880 I missed the S. 111 00:06:50,030 --> 00:06:50,570 All right. 112 00:06:50,570 --> 00:06:52,130 So the s is right here. 113 00:06:53,740 --> 00:06:54,280 Now. 114 00:06:54,280 --> 00:06:55,210 Save it. 115 00:06:56,340 --> 00:06:57,960 Now let's run it. 116 00:06:57,990 --> 00:07:02,040 See, this one little tiny mistake can cause a huge problem. 117 00:07:02,040 --> 00:07:05,110 So be very careful when you write playbooks. 118 00:07:05,130 --> 00:07:07,650 All right, so let's run the playbook again. 119 00:07:08,010 --> 00:07:09,750 Assemble the playbook. 120 00:07:10,470 --> 00:07:12,320 All right, so let's run it. 121 00:07:12,750 --> 00:07:14,280 Find a process and kill it. 122 00:07:14,280 --> 00:07:15,510 Gathering facts. 123 00:07:15,540 --> 00:07:16,860 This is the host. 124 00:07:17,310 --> 00:07:17,730 All right. 125 00:07:17,730 --> 00:07:19,740 Another error message field. 126 00:07:19,740 --> 00:07:23,010 The 10 to 51 off a failed message. 127 00:07:24,270 --> 00:07:26,070 Direct object has no attribute. 128 00:07:26,100 --> 00:07:28,110 Steady out. 129 00:07:28,650 --> 00:07:30,080 Underscore line. 130 00:07:30,090 --> 00:07:31,920 Let's see what we are doing. 131 00:07:33,560 --> 00:07:35,180 With items. 132 00:07:35,360 --> 00:07:39,410 Colon running underscore process. 133 00:07:39,560 --> 00:07:41,840 Dot steady. 134 00:07:43,010 --> 00:07:44,330 Out here. 135 00:07:44,330 --> 00:07:45,990 I missed s again. 136 00:07:46,010 --> 00:07:47,150 I'm sorry, guys. 137 00:07:47,810 --> 00:07:51,350 Here is the se and save it. 138 00:07:52,640 --> 00:07:55,760 All right, let's clear the screen here and let's run it again. 139 00:08:07,570 --> 00:08:08,200 All right. 140 00:08:08,200 --> 00:08:09,970 So now it worked. 141 00:08:10,270 --> 00:08:17,710 The reason you are getting this failed error message, which you could safely ignore it is because when 142 00:08:17,740 --> 00:08:24,490 this Ansible playbook ran, it found a couple of additional processes and one of the process that it 143 00:08:24,490 --> 00:08:27,400 found was its own self while it was running. 144 00:08:27,730 --> 00:08:30,100 And the grep command of itself. 145 00:08:30,100 --> 00:08:34,360 When you do grep for top, that becomes a process of its own. 146 00:08:34,360 --> 00:08:40,930 So it found those two processes and then it tried to kill those process, but it couldn't find it. 147 00:08:40,930 --> 00:08:41,740 So that's fine. 148 00:08:41,740 --> 00:08:46,960 You could safely ignore them and it did what it's supposed to do. 149 00:08:46,990 --> 00:08:50,200 It stopped our top process. 150 00:08:50,830 --> 00:08:56,980 So that's how you could kill a running process or a defined process that is running. 151 00:08:56,980 --> 00:09:01,150 And you wanted to kill it on your remote client.