1 00:00:00,870 --> 00:00:04,830 In this video, we are going to learn ansible handlers. 2 00:00:05,160 --> 00:00:08,950 Handlers are executed at the end of the play. 3 00:00:08,970 --> 00:00:18,030 Once all tasks are finished and ansible handlers are typically used to start, reload, restart and 4 00:00:18,030 --> 00:00:19,860 stop services. 5 00:00:20,340 --> 00:00:26,310 So sometimes you want to run a task only when a change is made on a machine. 6 00:00:26,790 --> 00:00:33,450 For example, you may want to restart a service if a task updates a configuration file of that service, 7 00:00:33,450 --> 00:00:37,350 but if not, the configuration file will be unchanged. 8 00:00:38,040 --> 00:00:38,370 All right. 9 00:00:38,370 --> 00:00:44,910 Remember the case when we had to reload the firewall DX because we wanted to enable HTTP service? 10 00:00:45,270 --> 00:00:48,350 Yes, that's a perfect example of using Handler. 11 00:00:48,360 --> 00:00:57,570 If you had not updated or added the HTTP service in firewall dx, then there was no need to reload the 12 00:00:57,570 --> 00:00:58,650 firewall service. 13 00:00:59,100 --> 00:01:05,220 So basically handlers are tasks that only run when notified. 14 00:01:05,220 --> 00:01:07,520 So pay close attention. 15 00:01:07,530 --> 00:01:16,290 The notified is something that has to be inside of the playbook that will indicate that the handler 16 00:01:16,290 --> 00:01:19,650 should be run at the end of the playbook. 17 00:01:20,250 --> 00:01:25,680 So each handler should have a globally unique name. 18 00:01:26,100 --> 00:01:26,400 All right. 19 00:01:26,400 --> 00:01:32,130 Let's look at look at the example of our first handler playbook. 20 00:01:32,370 --> 00:01:32,780 All right. 21 00:01:32,790 --> 00:01:34,710 Here we have the example. 22 00:01:34,710 --> 00:01:40,470 If you pay close attention right here, name, verify, Apache installation. 23 00:01:40,470 --> 00:01:42,930 So this is the name of the playbook. 24 00:01:43,260 --> 00:01:45,690 It's going to run against the local host. 25 00:01:45,690 --> 00:01:50,250 And the task, the very first task is ensure Apache is at latest version. 26 00:01:50,430 --> 00:01:50,940 All right. 27 00:01:50,940 --> 00:01:59,130 So to ensure that we'll do the job module, the name of the package will be HTTP and the state is going 28 00:01:59,130 --> 00:02:00,540 to be the latest one. 29 00:02:00,540 --> 00:02:09,000 So this latest option will ensure that it has Apache package and it is the latest one. 30 00:02:09,000 --> 00:02:14,220 Then the second task will copy updated Apache config file. 31 00:02:14,220 --> 00:02:16,770 So you might have the updated config file. 32 00:02:16,770 --> 00:02:18,600 So you will use the module copy. 33 00:02:18,600 --> 00:02:26,610 The source will be temp, http, dcop or wherever you have the updated http file located. 34 00:02:26,700 --> 00:02:32,490 Then the destination will be of course an http dhcp, which is the default location. 35 00:02:32,610 --> 00:02:38,880 Or maybe if you have changed the default location of Apache somewhere else, then you specify right 36 00:02:38,880 --> 00:02:41,220 here, now comes the notify. 37 00:02:41,400 --> 00:02:52,530 So notify is is the indicator right here that it requires Apache to be restarted name. 38 00:02:53,310 --> 00:02:54,690 That's the third task. 39 00:02:54,690 --> 00:03:01,140 Ensure Apache is running and you know how we have to define to have it running then comes in the last 40 00:03:01,140 --> 00:03:08,640 part is the handler and the handler is what name restart Apache the service named DPD and the state 41 00:03:08,670 --> 00:03:09,840 can be restarted. 42 00:03:09,900 --> 00:03:20,490 Now if you notice this restart Apache, it will restart the Hpdi toward the end of the playbook. 43 00:03:20,640 --> 00:03:27,540 And the reason we are asking it to do is because it is updating the HTTP file. 44 00:03:27,540 --> 00:03:36,450 So before it goes and and started first, it will ensure it is up and running and then it will go ahead 45 00:03:36,450 --> 00:03:37,500 and restart it. 46 00:03:37,500 --> 00:03:40,080 That's what the handlers are for. 47 00:03:40,200 --> 00:03:43,290 Next, we will take an example of our firewall. 48 00:03:43,290 --> 00:03:49,110 The playbook here is the firewall playbook and the name of the firewall. 49 00:03:49,110 --> 00:03:51,870 The playbook is enable service on firewall DX. 50 00:03:52,050 --> 00:03:58,080 It will run against local host and the very first task would be open the port for HTTP. 51 00:03:58,170 --> 00:04:05,460 The module will be firewall DX, the service will be http permanent run it about time and the state 52 00:04:05,460 --> 00:04:14,280 to enable it right now now comes in the notify part you want to use the notifier handler here where 53 00:04:14,280 --> 00:04:15,870 it will reload the firewall. 54 00:04:16,680 --> 00:04:17,040 All right. 55 00:04:17,040 --> 00:04:22,860 So the reason we are putting this here is because we don't want to reload right away, because we wanted 56 00:04:22,860 --> 00:04:25,470 to first ensure the firewall is running or not. 57 00:04:25,470 --> 00:04:28,950 So that's where we are just putting notify here. 58 00:04:28,950 --> 00:04:35,490 So it could be reloaded at the end after we ensure the firewall is running. 59 00:04:35,490 --> 00:04:35,790 All right. 60 00:04:35,790 --> 00:04:36,630 So we ensure it. 61 00:04:36,630 --> 00:04:43,170 It is up and running and started now comes in the handler and handler is saying to reload the firewall 62 00:04:43,170 --> 00:04:48,930 D which we did the notification here and the service and the name of the service is Firewall DX and 63 00:04:48,930 --> 00:04:51,150 the State is Reloaded. 64 00:04:51,660 --> 00:04:58,590 That's a real example, which we did previously for our firewall, and that's a perfect example to use 65 00:04:58,590 --> 00:04:59,820 our handlers.