1 00:00:00,690 --> 00:00:06,150 In this video, we are going to run a Shell script on our Linux remote clients. 2 00:00:06,630 --> 00:00:13,260 Now shell scripts are written so you could automate some of your tedious tasks as well. 3 00:00:13,290 --> 00:00:19,350 You could write shell scripts in either bash shell script, seashell script, or corn shell script. 4 00:00:19,920 --> 00:00:26,490 Now, what we will do in this video is we will write a playbook which will run your shell scripts that 5 00:00:26,490 --> 00:00:28,500 are located on your remote client. 6 00:00:28,530 --> 00:00:31,680 And for us, our client is the Linux client one. 7 00:00:32,070 --> 00:00:38,730 You should have a script located on your remote client that you want to run by Ansible, but if you 8 00:00:38,730 --> 00:00:46,950 do not have a script already, then what we will do is we will create the script and we will create 9 00:00:46,950 --> 00:00:47,850 it in home. 10 00:00:47,850 --> 00:00:49,200 IFC Directory. 11 00:00:49,740 --> 00:00:56,010 You could create the script anywhere or in any directory of your choice, but to be consistent, let's 12 00:00:56,010 --> 00:00:58,070 put it in our home directory or IFC. 13 00:00:58,080 --> 00:00:59,580 I'll use a home directory. 14 00:01:00,660 --> 00:01:04,560 Then once we have that file, then the script. 15 00:01:04,560 --> 00:01:09,970 What it should do, it will create a file called Example one. 16 00:01:09,990 --> 00:01:13,950 It is just that small sample script. 17 00:01:14,790 --> 00:01:17,100 Your script can do anything that you want. 18 00:01:17,100 --> 00:01:24,810 But for this video's purpose, to show you how we could run or how we could execute shell scripts on 19 00:01:24,810 --> 00:01:27,540 remote clients is using Ansible modules. 20 00:01:28,260 --> 00:01:35,760 All right, so once we have that, then we will do as we will create our shell script, yaml file, 21 00:01:35,790 --> 00:01:41,640 define our YAML format, we'll name the playbook. 22 00:01:42,450 --> 00:01:47,490 The host could be either all or any specific host. 23 00:01:47,880 --> 00:01:53,730 The task, the name of the shell or the name of the task, actually. 24 00:01:54,060 --> 00:01:56,400 And in this case, run shell script. 25 00:01:56,760 --> 00:02:03,660 Now we need to define the module, the module that can be used to run the shell script on the remote 26 00:02:03,660 --> 00:02:04,230 client. 27 00:02:04,230 --> 00:02:13,110 And for that you have to use the module shell and then you define the absolute path of that shell script 28 00:02:13,110 --> 00:02:14,310 where it's located. 29 00:02:14,310 --> 00:02:17,850 And the last thing is we will run that YAML file. 30 00:02:18,240 --> 00:02:23,160 All right, so let's go to the very first thing is let's go to our client. 31 00:02:24,000 --> 00:02:25,830 Here is our Linux client one. 32 00:02:26,070 --> 00:02:27,930 We'll go to slash home slash. 33 00:02:27,930 --> 00:02:29,340 I have solid directory, by the way. 34 00:02:29,340 --> 00:02:32,310 I'm logged in as a root. 35 00:02:32,340 --> 00:02:34,110 Who am I? 36 00:02:34,320 --> 00:02:36,570 And I am in slash home. 37 00:02:36,570 --> 00:02:36,840 Slash. 38 00:02:36,840 --> 00:02:38,070 I have solved directory. 39 00:02:38,070 --> 00:02:44,190 You could create your shell script either by IFCO or as root. 40 00:02:44,190 --> 00:02:45,300 It doesn't really matter. 41 00:02:45,330 --> 00:02:49,200 All right, so let's first create our shell script. 42 00:02:49,200 --> 00:02:52,220 So them see file. 43 00:02:52,230 --> 00:02:56,160 This is the name I just picked randomly to create a shell script. 44 00:02:57,000 --> 00:02:58,800 All right, let's define that. 45 00:02:58,800 --> 00:03:11,040 It is going to be been a bash shell script, and we want to actually create a file called Example one. 46 00:03:11,040 --> 00:03:13,650 And for that, we'll use the command touch. 47 00:03:14,100 --> 00:03:14,520 All right. 48 00:03:14,520 --> 00:03:16,080 So that's pretty much it. 49 00:03:16,470 --> 00:03:19,500 It's just that simple script. 50 00:03:19,650 --> 00:03:22,830 And let's save it now. 51 00:03:23,160 --> 00:03:32,370 Let's go ahead and see if that C shell file has executable permission, which it does not. 52 00:03:32,730 --> 00:03:39,780 So we will give the executable permission search mod or change mod a exe c file. 53 00:03:42,760 --> 00:03:43,360 All right. 54 00:03:43,360 --> 00:03:46,460 This has the executable permission. 55 00:03:46,480 --> 00:03:50,150 Now, before we run this Shell script through Ansible. 56 00:03:50,170 --> 00:03:56,320 Let's run it on the client on this machine just to make sure that it works. 57 00:03:56,800 --> 00:03:57,130 All right. 58 00:03:57,130 --> 00:04:02,730 For that, we'll just do simply dot slash C file, S-H hit enter. 59 00:04:02,740 --> 00:04:09,370 Now, this should create a file example one in this current directory and there it is. 60 00:04:09,370 --> 00:04:11,680 Our example of one file has been created. 61 00:04:12,400 --> 00:04:13,010 Perfect. 62 00:04:13,030 --> 00:04:15,050 Now let's go ahead and delete that. 63 00:04:15,070 --> 00:04:22,000 So this way, when we create with Ansible, we should have a proof that Ansible created the file. 64 00:04:22,090 --> 00:04:23,250 All right, let's delete it. 65 00:04:23,260 --> 00:04:25,030 Let's make sure it does not have. 66 00:04:25,030 --> 00:04:26,140 Example one file. 67 00:04:26,140 --> 00:04:27,220 Note it does not. 68 00:04:27,920 --> 00:04:28,480 Perfect. 69 00:04:28,490 --> 00:04:31,460 Now let's go to our control node. 70 00:04:32,880 --> 00:04:34,590 Right here is our control node. 71 00:04:36,330 --> 00:04:38,190 Let's log in as route. 72 00:04:40,710 --> 00:04:42,450 I am in my route directory. 73 00:04:42,450 --> 00:04:45,810 Let's go to Route Ansible. 74 00:04:46,410 --> 00:04:47,720 Sorry, not route. 75 00:04:47,730 --> 00:04:51,750 It is an Etsy Ansible playbooks. 76 00:04:52,170 --> 00:04:55,440 These are where all our playbooks are located. 77 00:04:55,440 --> 00:04:59,700 And we'll use the same location to write all of our playbooks. 78 00:05:00,120 --> 00:05:05,280 All right, so we'll do them and we'll call it shell script, dot yaml. 79 00:05:06,910 --> 00:05:11,300 Dash, dash, dash to define it is a gimbal file. 80 00:05:11,320 --> 00:05:17,890 The name of the playbook playbook for shell script. 81 00:05:18,040 --> 00:05:19,570 All right then. 82 00:05:19,570 --> 00:05:22,090 Host, where do you want to execute it this time? 83 00:05:22,090 --> 00:05:30,520 Instead of picking all, I'm just going to define the IP address of our host, which is 10.2 53 1.115. 84 00:05:30,550 --> 00:05:33,430 This is the IP address of our client. 85 00:05:33,460 --> 00:05:40,900 Now let's make sure that it has not changed because it is running on dhcp. 86 00:05:40,930 --> 00:05:43,240 So I did if config. 87 00:05:43,330 --> 00:05:45,100 Let's move up and we'll see. 88 00:05:45,140 --> 00:05:50,230 ENP zero three still have 10 to 53 1.115 IP. 89 00:05:50,500 --> 00:05:51,370 All right, good. 90 00:05:51,400 --> 00:05:54,670 Let's go back to our control node. 91 00:05:55,420 --> 00:05:58,750 All right, so now the tasks. 92 00:05:58,750 --> 00:06:00,640 What do you want it to run? 93 00:06:00,730 --> 00:06:05,030 Let's name the task that it will run. 94 00:06:05,050 --> 00:06:06,280 Shell script. 95 00:06:07,300 --> 00:06:07,870 All right. 96 00:06:07,870 --> 00:06:13,360 And what module that you will use to run that task? 97 00:06:13,360 --> 00:06:15,280 We will use the module shell. 98 00:06:17,500 --> 00:06:20,520 All right, put double code slash home. 99 00:06:20,530 --> 00:06:23,290 Where is the location of your script? 100 00:06:23,800 --> 00:06:25,260 I have saw Slash. 101 00:06:25,270 --> 00:06:28,690 See that S-H double code. 102 00:06:28,690 --> 00:06:29,380 Close. 103 00:06:30,600 --> 00:06:32,100 And save the file. 104 00:06:34,910 --> 00:06:36,110 All right, the file is saved. 105 00:06:36,140 --> 00:06:37,550 Now let's run it. 106 00:06:39,460 --> 00:06:44,680 Let's see if he have written the shell's the java file correctly. 107 00:06:45,130 --> 00:06:45,700 Enter. 108 00:06:48,460 --> 00:06:49,420 All right, play. 109 00:06:49,420 --> 00:06:51,140 Play for shell script task. 110 00:06:51,160 --> 00:06:58,930 Task is for our defined host right here sheet and right here is telling it that it ran, it changed 111 00:06:59,200 --> 00:07:00,730 and it was successful. 112 00:07:00,850 --> 00:07:01,630 Nice. 113 00:07:01,630 --> 00:07:09,610 So now the only way to confirm it is by going to our client and see if it has created the example. 114 00:07:09,610 --> 00:07:12,560 One file in slash home and slash EOF solved. 115 00:07:12,910 --> 00:07:14,770 All right, I am in slash home. 116 00:07:14,770 --> 00:07:15,970 I have solved directory. 117 00:07:15,970 --> 00:07:23,320 Let's do ls minus l tr and oops it did not run. 118 00:07:24,120 --> 00:07:24,600 Hmm. 119 00:07:25,020 --> 00:07:27,300 Let's see why I did not run. 120 00:07:27,330 --> 00:07:39,210 Maybe when we ran the file, which was slash home, slash IFC file, it created the file not in the 121 00:07:39,210 --> 00:07:40,140 same directory. 122 00:07:40,140 --> 00:07:43,650 It created the file in slash root directory. 123 00:07:43,650 --> 00:07:49,670 Because right here the root is the one that is going into this machine and running that. 124 00:07:49,680 --> 00:07:57,840 So just let's do a CD and hit enter and this is going to bring me back to the root home directory. 125 00:07:57,840 --> 00:08:06,780 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 126 00:08:06,780 --> 00:08:08,220 one in this directory. 127 00:08:08,220 --> 00:08:09,390 And there you go. 128 00:08:09,390 --> 00:08:15,480 That's what I have a hunch on I would say, and that's where it created the file on. 129 00:08:15,480 --> 00:08:23,130 If you want to create the file in a different location through the shell script, then you go to the 130 00:08:23,130 --> 00:08:28,800 shell script slash home slash I of saw and see file. 131 00:08:29,610 --> 00:08:32,700 Sorry, that's vim shell script. 132 00:08:33,690 --> 00:08:34,330 At sea. 133 00:08:34,710 --> 00:08:35,280 Sorry. 134 00:08:35,610 --> 00:08:36,000 Home. 135 00:08:36,000 --> 00:08:38,280 I have saw sea fall. 136 00:08:38,490 --> 00:08:45,920 And right here, when we say touch example one will define the absolute path home I saw slash. 137 00:08:46,290 --> 00:08:46,890 All right. 138 00:08:46,890 --> 00:08:54,420 Now it should regardless where I am running it from which location on the control node, where it's 139 00:08:55,050 --> 00:08:56,070 on the client. 140 00:08:56,070 --> 00:08:59,100 When we run it right here, let's run it again. 141 00:09:00,060 --> 00:09:09,210 It will create that file in our slash home slash I of saw directory and do a list myself here and you'll 142 00:09:09,210 --> 00:09:11,110 see right here it created. 143 00:09:11,490 --> 00:09:12,060 All right. 144 00:09:12,060 --> 00:09:19,290 So that was an example of running or creating a playbook that will execute your shell scripts on your 145 00:09:19,290 --> 00:09:20,850 Linux remote clients.