1 00:00:00,630 --> 00:00:07,590 So at this point we have learned that you could use Ansible Playbook to execute Bash Shell script at 2 00:00:07,590 --> 00:00:09,600 a remote client locations. 3 00:00:10,200 --> 00:00:16,110 Now in this video, we will learn how we could use those same script and schedule them through the current 4 00:00:16,110 --> 00:00:21,510 job and setting that cron job can be done through Ansible playbook. 5 00:00:21,810 --> 00:00:26,730 So in this video we will write a playbook and we'll name it cron job YAML. 6 00:00:26,730 --> 00:00:35,460 And this playbook will include scheduling a job as a root user and that that cron will run every Thursday 7 00:00:35,460 --> 00:00:43,610 at 10 a.m. and the job that it will kick off is slash home slash Afzal see file search. 8 00:00:43,680 --> 00:00:44,580 That's the script. 9 00:00:44,580 --> 00:00:46,050 It will kick off. 10 00:00:46,530 --> 00:00:49,470 And by the way, this is the file that we created earlier. 11 00:00:49,470 --> 00:00:53,100 And that file has to be executed, executed by root. 12 00:00:53,460 --> 00:00:53,760 All right. 13 00:00:53,760 --> 00:00:58,880 So let's look at our YAML file and we'll use a Vim editor. 14 00:00:58,890 --> 00:01:00,990 Define the name of the YAML file. 15 00:01:01,020 --> 00:01:03,120 Then dash, dash, dash name. 16 00:01:03,120 --> 00:01:05,440 Create a cron job, then host. 17 00:01:05,460 --> 00:01:14,160 Where do you want to run that or set that cron job on all hosts that are located in at see ansible and 18 00:01:14,190 --> 00:01:15,180 host file. 19 00:01:15,540 --> 00:01:15,880 All right. 20 00:01:15,900 --> 00:01:18,440 Then we'll give a space to keep it neat and clean. 21 00:01:18,450 --> 00:01:19,740 Define the task. 22 00:01:19,740 --> 00:01:22,640 Then the name of the task scheduled cron. 23 00:01:22,650 --> 00:01:29,740 Now we'll use the cron module to actually perform this ansible function. 24 00:01:29,760 --> 00:01:32,760 Now the name this job is scheduled by Ansible. 25 00:01:32,760 --> 00:01:41,940 This is the name or the description that will be added on top of the actual cron job inside the cron 26 00:01:41,940 --> 00:01:43,680 job and you run cron tab. 27 00:01:44,010 --> 00:01:51,840 Now the minute you want to run it at zero minute hour at 10 a.m., then the day this is going to be 28 00:01:51,840 --> 00:02:00,300 the day of the month, every day of the month, then the month every month, then the week day you want 29 00:02:00,300 --> 00:02:00,960 to run. 30 00:02:00,960 --> 00:02:04,670 This is the day of the week which shows that run every Thursday. 31 00:02:04,680 --> 00:02:10,440 Then you have the user run it as root and the job or script name that you want to run. 32 00:02:10,710 --> 00:02:12,030 Sorry, schedule. 33 00:02:12,210 --> 00:02:19,740 Then of course, once that playbook is created, then we will run this playbook through our ansible 34 00:02:20,010 --> 00:02:29,400 playbook space cron job yaml file and it should create the cron tab entry in our remote client. 35 00:02:29,400 --> 00:02:32,250 All right, so let's first go to our remote client. 36 00:02:34,120 --> 00:02:36,050 This is our Linux one machine. 37 00:02:36,070 --> 00:02:45,790 I am logged in as root and when you do cron tab minus l to list what is scheduled to be executed on 38 00:02:45,790 --> 00:02:47,770 this machine by cron tab. 39 00:02:47,860 --> 00:02:50,080 You run it, you don't get anything. 40 00:02:50,080 --> 00:02:53,910 It means nothing has been scheduled on this machine. 41 00:02:53,920 --> 00:03:02,050 Now let's go to our Ansible control node and write that playbook. 42 00:03:02,050 --> 00:03:02,740 So which two? 43 00:03:02,740 --> 00:03:03,460 Actually we are in? 44 00:03:03,490 --> 00:03:04,990 We are etsi ansible playbook. 45 00:03:04,990 --> 00:03:05,500 Good. 46 00:03:05,500 --> 00:03:07,200 Where am I logged in as root. 47 00:03:07,210 --> 00:03:13,210 Now create use VM editor to create cron job yaml. 48 00:03:13,330 --> 00:03:14,770 That's the name I picked. 49 00:03:14,770 --> 00:03:16,120 You could pick any name. 50 00:03:16,150 --> 00:03:18,010 Define dash, dash, dash. 51 00:03:18,010 --> 00:03:23,470 And the name of the playbook is create a cron job. 52 00:03:23,800 --> 00:03:25,180 All right, hosts. 53 00:03:25,810 --> 00:03:27,730 All right, then. 54 00:03:27,730 --> 00:03:28,780 Tasks. 55 00:03:30,450 --> 00:03:34,500 Then the name of the task schedule. 56 00:03:36,680 --> 00:03:37,460 Cron. 57 00:03:37,910 --> 00:03:38,680 All right. 58 00:03:38,690 --> 00:03:46,550 The module that you wanted to use for this playbook or for this specific task and that is cron are the 59 00:03:46,550 --> 00:03:48,890 name which is will be the description. 60 00:03:49,100 --> 00:03:55,940 This job is scheduled by Ansible Mainnet. 61 00:03:56,840 --> 00:04:00,260 Make sure you put double coat and zero. 62 00:04:01,330 --> 00:04:03,670 Our double coat. 63 00:04:03,670 --> 00:04:04,900 Sorry, colon. 64 00:04:04,900 --> 00:04:09,670 Double coat, 10 a.m. de. 65 00:04:11,770 --> 00:04:12,910 Every day. 66 00:04:14,950 --> 00:04:16,450 Which is the day of the month. 67 00:04:16,450 --> 00:04:20,080 You wanted to have it every day of the month. 68 00:04:20,110 --> 00:04:21,370 Then a month. 69 00:04:22,340 --> 00:04:23,390 Every month. 70 00:04:23,660 --> 00:04:29,070 That's why we're putting in this asterisks, if you're familiar with how you could write a cron job. 71 00:04:29,090 --> 00:04:33,140 I'm sure you know exactly what I am writing it up now. 72 00:04:33,140 --> 00:04:33,920 Weekday. 73 00:04:34,840 --> 00:04:37,680 He wanted to run every Thursday. 74 00:04:37,690 --> 00:04:42,730 So the sun is zero, Monday one, Tuesday to Wednesday three and Thursdays. 75 00:04:42,730 --> 00:04:47,950 For now, the user who do you wanted to run it as? 76 00:04:48,010 --> 00:04:49,780 We wanted to run it as route. 77 00:04:49,840 --> 00:04:50,290 All right. 78 00:04:50,290 --> 00:04:51,550 So that is all done. 79 00:04:51,550 --> 00:04:54,610 Now, what do you want to run the job? 80 00:04:54,610 --> 00:04:55,900 I want you to run. 81 00:04:57,210 --> 00:04:58,140 Slash home. 82 00:04:58,140 --> 00:05:01,320 I have XL slash C file. 83 00:05:01,320 --> 00:05:03,150 That's the script I want to run. 84 00:05:03,750 --> 00:05:06,030 All right, all good. 85 00:05:06,910 --> 00:05:17,800 Set in final save it now let's go ahead and run this ansible playbook and cron job. 86 00:05:17,800 --> 00:05:20,140 And again, let's cross our fingers. 87 00:05:20,170 --> 00:05:22,240 Let Ansible do the magic. 88 00:05:22,450 --> 00:05:23,080 All right. 89 00:05:23,260 --> 00:05:25,270 Play name is good. 90 00:05:25,270 --> 00:05:27,610 Gathering information is good as task is good. 91 00:05:27,610 --> 00:05:30,490 And it completed no error messages. 92 00:05:30,490 --> 00:05:31,050 Excellent. 93 00:05:31,050 --> 00:05:31,750 So it worked. 94 00:05:31,750 --> 00:05:35,140 So let's confirm it by going to our. 95 00:05:36,900 --> 00:05:46,240 For a more client or now when we run cross tab minus L and you will see right here this job is scheduled 96 00:05:46,240 --> 00:05:47,020 by Ansible. 97 00:05:47,020 --> 00:05:53,470 This is the name or the description that I was telling you that we defined right here, and by default 98 00:05:53,470 --> 00:06:00,820 it will add this hash or pound sign with Ansible, which shows that it is created by Ansible, of course. 99 00:06:00,850 --> 00:06:09,190 And look at this entry zero, which is right here, minute ten, which is the hour, then asterisk for 100 00:06:09,190 --> 00:06:11,490 the day, asterisk for the month and the week. 101 00:06:11,500 --> 00:06:13,690 Day is the fourth, which is. 102 00:06:16,760 --> 00:06:18,830 Which is Thursday. 103 00:06:18,860 --> 00:06:19,280 Yes. 104 00:06:19,280 --> 00:06:22,470 So then the entry worked. 105 00:06:22,550 --> 00:06:24,760 Which what do you want to do? 106 00:06:24,770 --> 00:06:26,930 That's the script we wanted to execute. 107 00:06:27,680 --> 00:06:28,220 All right. 108 00:06:28,220 --> 00:06:37,640 So that's how you could schedule a job and schedule it on the cron tab using Ansible playbook.