1 00:00:06,500 --> 00:00:07,333 - In this video, 2 00:00:07,333 --> 00:00:09,773 we are going to explore the use of handlers. 3 00:00:10,840 --> 00:00:12,100 So what is a handler? 4 00:00:12,100 --> 00:00:14,371 A handler is a task that is only executed 5 00:00:14,371 --> 00:00:16,380 when it is triggered by a task 6 00:00:16,380 --> 00:00:18,310 that has changed something. 7 00:00:18,310 --> 00:00:21,750 Handlers are executed after all tasks in a play. 8 00:00:21,750 --> 00:00:23,370 So that's important to realize, 9 00:00:23,370 --> 00:00:25,110 and you need to organize the contents 10 00:00:25,110 --> 00:00:27,130 of your playbook accordingly. 11 00:00:27,130 --> 00:00:29,430 And if any task fails after the task 12 00:00:29,430 --> 00:00:30,427 that has called the handler, 13 00:00:30,427 --> 00:00:32,930 the handler is not executed at all. 14 00:00:32,930 --> 00:00:34,050 If you don't like that, 15 00:00:34,050 --> 00:00:36,950 you need to use force handlers to change that behavior. 16 00:00:36,950 --> 00:00:38,543 Let's go check out an example. 17 00:00:39,700 --> 00:00:41,740 So let's have a look at an example. 18 00:00:41,740 --> 00:00:44,640 The example is in handlers.yml. 19 00:00:44,640 --> 00:00:45,890 So what are we doing? 20 00:00:45,890 --> 00:00:49,020 We are running the playbook on the host rocky. 21 00:00:49,020 --> 00:00:50,880 We install httpd, 22 00:00:50,880 --> 00:00:53,711 then we copy over an index html file. 23 00:00:53,711 --> 00:00:57,573 That's a file that should exist in tmp index html. 24 00:00:59,500 --> 00:01:01,580 And this is the important part about the handler, 25 00:01:01,580 --> 00:01:03,780 we use notify restart web. 26 00:01:03,780 --> 00:01:06,180 Restart web actually needs to correspond 27 00:01:06,180 --> 00:01:07,650 to the name of the handler. 28 00:01:07,650 --> 00:01:09,980 And that is the case as you can see right here. 29 00:01:09,980 --> 00:01:11,660 So name it restart web. 30 00:01:11,660 --> 00:01:14,700 And then that's the interesting thing, 31 00:01:14,700 --> 00:01:17,308 I included the task that is going to fail. 32 00:01:17,308 --> 00:01:19,560 We will have a failure on purpose. 33 00:01:19,560 --> 00:01:22,160 That's exactly what I want to demonstrate. 34 00:01:22,160 --> 00:01:23,520 So I want to demonstrate 35 00:01:23,520 --> 00:01:25,526 that the handler is not going to run, 36 00:01:25,526 --> 00:01:28,170 if any of the tasks that is triggered 37 00:01:28,170 --> 00:01:30,525 after the handler is going to fail. 38 00:01:30,525 --> 00:01:33,590 So let's run it and figure out what's going to happen. 39 00:01:33,590 --> 00:01:35,683 Ansible playbook and handlers.yml. 40 00:01:39,160 --> 00:01:43,490 So here we can see copy index html has changed something. 41 00:01:43,490 --> 00:01:46,140 So it should trigger the handle normally, 42 00:01:46,140 --> 00:01:49,510 but after this handle triggering task, 43 00:01:49,510 --> 00:01:51,110 another task is failing. 44 00:01:51,110 --> 00:01:52,340 And the default behavior 45 00:01:52,340 --> 00:01:55,340 in Ansible happens to be if execution 46 00:01:55,340 --> 00:01:58,980 of a specific task, on a specific host, is failing. 47 00:01:58,980 --> 00:02:03,980 Then further play execution on that host is aborted. 48 00:02:04,120 --> 00:02:06,960 And for that reason, we don't see the handler. 49 00:02:06,960 --> 00:02:09,200 Now, if you want to change that behavior 50 00:02:09,200 --> 00:02:10,650 that is pretty easy. 51 00:02:10,650 --> 00:02:13,060 You need to get back in the handler's code 52 00:02:13,060 --> 00:02:18,060 and you need to use force handlers:yes 53 00:02:19,740 --> 00:02:21,283 or true or whatever. 54 00:02:21,283 --> 00:02:22,870 It's a boolean, 55 00:02:22,870 --> 00:02:24,710 so you can go whatever way you want. 56 00:02:24,710 --> 00:02:26,287 Ansible is pretty flexible, 57 00:02:26,287 --> 00:02:29,330 regarding the way how you write your boolean. 58 00:02:29,330 --> 00:02:30,603 So let's run it again. 59 00:02:32,570 --> 00:02:35,060 And here we can see that... 60 00:02:35,060 --> 00:02:37,130 Oh boy, nothing is happening. 61 00:02:37,130 --> 00:02:38,910 Why isn't anything happening? 62 00:02:38,910 --> 00:02:41,370 I'll take a second to think about it. 63 00:02:41,370 --> 00:02:42,760 Did you think about it? 64 00:02:42,760 --> 00:02:45,690 Have a look at the task copy index html 65 00:02:45,690 --> 00:02:47,550 which hasn't changed anything. 66 00:02:47,550 --> 00:02:50,310 And this is the mere essence of using handlers. 67 00:02:50,310 --> 00:02:52,660 Handlers are going to be operational 68 00:02:52,660 --> 00:02:54,950 if something has changed. 69 00:02:54,950 --> 00:02:58,265 So the index html already existed. 70 00:02:58,265 --> 00:03:00,700 And that means that I need 71 00:03:01,660 --> 00:03:05,343 to use an ad hoc command Ansible all minus m file 72 00:03:05,343 --> 00:03:10,343 minus a name is /var/www/html/index.html 73 00:03:14,580 --> 00:03:16,853 and state is absent. 74 00:03:20,259 --> 00:03:22,380 And then I'm going to run it again 75 00:03:22,380 --> 00:03:25,010 and then we will see what is going to happen. 76 00:03:25,010 --> 00:03:26,770 So now we can see that yeah, 77 00:03:26,770 --> 00:03:30,200 index html is changing something. 78 00:03:30,200 --> 00:03:31,870 Copy nothing is still failing, 79 00:03:31,870 --> 00:03:34,900 but the handler has restarted web. 80 00:03:34,900 --> 00:03:37,110 And that is what I wanted you to see here. 81 00:03:37,110 --> 00:03:40,950 Force handlers allows you to force the handlers 82 00:03:40,950 --> 00:03:42,500 to be started, 83 00:03:42,500 --> 00:03:46,023 even if any subsequent tasks are failing. 84 00:03:47,260 --> 00:03:48,093 And that's all.