1 00:00:06,570 --> 00:00:09,980 - In this video, we are going to explore the use of blocks. 2 00:00:09,980 --> 00:00:11,340 So what is a block? 3 00:00:11,340 --> 00:00:12,660 Well, the block is a electrical group 4 00:00:12,660 --> 00:00:15,300 of tasks and blocks are convenient 5 00:00:15,300 --> 00:00:18,290 because you can use them to control how tasks are executed 6 00:00:18,290 --> 00:00:19,650 in an easy way. 7 00:00:19,650 --> 00:00:23,030 If you want a web statement to apply to multiple tasks 8 00:00:23,030 --> 00:00:24,680 that's where blocks are coming in 9 00:00:25,970 --> 00:00:29,930 blocks can also be used in error condition handling. 10 00:00:29,930 --> 00:00:32,870 So you can use block to define the main tasks to run. 11 00:00:32,870 --> 00:00:35,780 And then you'll use rescue to define tasks that run. 12 00:00:35,780 --> 00:00:38,780 If tasks that are defined in the block are failing 13 00:00:38,780 --> 00:00:42,750 and you use always to define tasks that will run regardless 14 00:00:42,750 --> 00:00:46,410 of success or failure of block and ratio tasks. 15 00:00:46,410 --> 00:00:48,110 So that makes a block can be used 16 00:00:48,110 --> 00:00:50,650 like an if then else statement 17 00:00:50,650 --> 00:00:53,950 you should notice that items cannot be used on blocks. 18 00:00:53,950 --> 00:00:55,447 That's an inconvenience. 19 00:00:55,447 --> 00:00:58,690 Do remember it when you start working with blocks 20 00:00:58,690 --> 00:00:59,890 let's go check this out. 21 00:01:01,530 --> 00:01:04,113 So in our first example, 22 00:01:05,468 --> 00:01:07,710 this is a very simple example. 23 00:01:07,710 --> 00:01:09,490 And in the simple example, we are running 24 00:01:09,490 --> 00:01:13,340 against all hosts and we have a block. 25 00:01:13,340 --> 00:01:15,868 As you can see the play. 26 00:01:15,868 --> 00:01:18,416 Heather starts with tasks. 27 00:01:18,416 --> 00:01:22,796 Then before you start actually specifying the tasks 28 00:01:22,796 --> 00:01:26,350 there is this block section and below the block section 29 00:01:26,350 --> 00:01:28,200 as child elements through the block 30 00:01:28,200 --> 00:01:30,677 you'll find the actual tasks 31 00:01:30,677 --> 00:01:33,380 the tasks themselves are not very interesting. 32 00:01:33,380 --> 00:01:36,910 It's just installing and running a service. 33 00:01:36,910 --> 00:01:38,745 The interesting part is that the 34 00:01:38,745 --> 00:01:41,490 when statement applies to the entire block. 35 00:01:41,490 --> 00:01:44,300 so when Ansible distribution is Rocky 36 00:01:44,300 --> 00:01:45,770 that is how it's going to work. 37 00:01:45,770 --> 00:01:48,253 Then the entire block is going to be executed. 38 00:01:50,051 --> 00:01:53,267 So Ansible playbook on blocks dot YML. 39 00:01:59,440 --> 00:02:02,620 It's showing us that everything is working now. 40 00:02:02,620 --> 00:02:05,470 Let's also prove the negative. 41 00:02:05,470 --> 00:02:08,210 That is something that I would like to advise you. 42 00:02:08,210 --> 00:02:11,697 Anyway, if you write a playbook to do something 43 00:02:11,697 --> 00:02:13,560 then it's nice to see 44 00:02:13,560 --> 00:02:16,580 that the playbook is actually doing what you expected to do 45 00:02:16,580 --> 00:02:19,730 but you should also test the opposite, meaning 46 00:02:19,730 --> 00:02:21,790 that you should make sure that the playbook is 47 00:02:21,790 --> 00:02:26,056 not doing it when the condition is not true. 48 00:02:26,056 --> 00:02:30,416 So in my configuration where I only have Rocky machines 49 00:02:30,416 --> 00:02:33,430 well, it's easy to put anything else. 50 00:02:33,430 --> 00:02:34,640 And there we can see skipping 51 00:02:34,640 --> 00:02:37,520 and skipping and that's exactly what we needed. 52 00:02:37,520 --> 00:02:40,730 Those second example is blocks 2 dot YML 53 00:02:41,930 --> 00:02:43,560 is a little bit more complex. 54 00:02:43,560 --> 00:02:44,393 So what do we see? 55 00:02:44,393 --> 00:02:47,860 We see a block, a rescue, and an always. 56 00:02:47,860 --> 00:02:49,910 So block is what we want to do. 57 00:02:49,910 --> 00:02:52,170 If the block tasks don't work 58 00:02:52,170 --> 00:02:54,850 we are going to execute the rescue tasks. 59 00:02:54,850 --> 00:02:59,060 And always we are going to learn this specific command. 60 00:02:59,060 --> 00:03:00,637 I've kept it simple by using the command module. 61 00:03:00,637 --> 00:03:04,092 So the command module is a removing a file command 62 00:03:04,092 --> 00:03:06,978 usr bin RM var www. 63 00:03:06,978 --> 00:03:09,500 Then it is creating a file 64 00:03:09,500 --> 00:03:13,300 and then it is writing a message to, to the logs. 65 00:03:13,300 --> 00:03:15,568 It said, is this best practice? 66 00:03:15,568 --> 00:03:18,057 it's not let's change it. 67 00:03:18,057 --> 00:03:21,880 Why would we want to use the command model? 68 00:03:21,880 --> 00:03:25,140 If you want to remove a file, I mean, file 69 00:03:26,780 --> 00:03:29,193 path or name if you want, 70 00:03:30,123 --> 00:03:34,440 var WW, HTML, index dot HTML, 71 00:03:35,780 --> 00:03:38,300 state absent 72 00:03:38,300 --> 00:03:40,360 that will remove it as well. 73 00:03:40,360 --> 00:03:43,870 And then we have this other command to create. 74 00:03:43,870 --> 00:03:45,570 Get out of here. 75 00:03:45,570 --> 00:03:48,424 We are going to use file again 76 00:03:48,424 --> 00:03:53,424 and file is going to use the path. 77 00:03:54,010 --> 00:03:58,050 So the path is TMP rescue file 78 00:03:58,050 --> 00:04:03,050 and state touch touch will make sure that it is created. 79 00:04:04,420 --> 00:04:06,540 If it does not yet exist. 80 00:04:06,540 --> 00:04:08,853 We only cannot really change the 81 00:04:08,853 --> 00:04:12,151 the logger command into an Ansible model. 82 00:04:12,151 --> 00:04:16,290 So I will keep this logger command as it is. 83 00:04:16,290 --> 00:04:19,040 Sometimes command is unavoidable, but Hey 84 00:04:19,040 --> 00:04:20,370 it's a best practice. 85 00:04:20,370 --> 00:04:21,240 If you can 86 00:04:21,240 --> 00:04:26,240 you should avoid using command or shell even worse. 87 00:04:27,090 --> 00:04:28,840 Let's go for Ansible playbook 88 00:04:28,840 --> 00:04:31,773 blocks 2 dot YML and see how it is working. 89 00:04:33,600 --> 00:04:35,060 So what do we see? 90 00:04:35,060 --> 00:04:37,320 Well, it remove the file back to the playbook, 91 00:04:37,320 --> 00:04:38,318 remove a file. 92 00:04:38,318 --> 00:04:40,140 That's what it needed to do. 93 00:04:40,140 --> 00:04:43,570 So the file at this point is removed, which means 94 00:04:43,570 --> 00:04:47,432 that the rescue doesn't have to happen. 95 00:04:47,432 --> 00:04:49,763 Now I'm going to learn it again. 96 00:04:52,550 --> 00:04:56,310 Now we can see that removal a file is giving an okay and Hey 97 00:04:56,310 --> 00:04:59,450 we don't see the rescue and you know why we don't 98 00:04:59,450 --> 00:05:00,610 see the rescue? 99 00:05:00,610 --> 00:05:04,040 That is because of the nature of rescue 100 00:05:04,040 --> 00:05:06,175 rescue will earn tasks. 101 00:05:06,175 --> 00:05:10,650 If tasks that are defined in the block are failing. 102 00:05:10,650 --> 00:05:13,470 We don't have a failure on remove a file because 103 00:05:13,470 --> 00:05:15,946 of the item, potent nature of the playbook. 104 00:05:15,946 --> 00:05:20,550 So you know what it happens to be that using command. 105 00:05:20,550 --> 00:05:22,570 Wasn't such a bad idea. 106 00:05:22,570 --> 00:05:27,140 Anyway, so I am getting back to command 107 00:05:28,210 --> 00:05:31,540 and we are going to rewrite command. 108 00:05:31,540 --> 00:05:33,150 And what is this command? 109 00:05:33,150 --> 00:05:36,780 Well, the command is removing, right? 110 00:05:36,780 --> 00:05:41,130 So I'm using cmd RM var www and so on. 111 00:05:44,060 --> 00:05:47,730 And then we are running it again. 112 00:05:47,730 --> 00:05:49,733 So here we go, running it again. 113 00:05:50,970 --> 00:05:52,210 And this time, yay 114 00:05:52,210 --> 00:05:56,760 the command is failing and the file is created now 115 00:05:56,760 --> 00:05:58,600 again, and this is an important thing. 116 00:05:58,600 --> 00:06:00,830 In fact, that I wanted you to be aware of. 117 00:06:00,830 --> 00:06:04,220 This is all about the item, potent nature of Ansible. 118 00:06:04,220 --> 00:06:06,400 If you are using Ansible models 119 00:06:06,400 --> 00:06:09,770 most Ansible models are item potent by nature. 120 00:06:09,770 --> 00:06:13,050 So they will just give an okay 121 00:06:13,050 --> 00:06:15,490 if no changes were were needed. 122 00:06:15,490 --> 00:06:16,630 If you have command 123 00:06:16,630 --> 00:06:20,970 and command is trying to remove a file that doesn't exist 124 00:06:20,970 --> 00:06:24,680 then the command will give you a non-zero return code 125 00:06:24,680 --> 00:06:26,440 which is what we see here. 126 00:06:26,440 --> 00:06:28,220 Return code one. 127 00:06:28,220 --> 00:06:30,620 That's obvious because the command does not exist. 128 00:06:30,620 --> 00:06:32,000 That's a real failure. 129 00:06:32,000 --> 00:06:34,110 And that is why in this second attempt 130 00:06:34,110 --> 00:06:36,340 the rescue is going to be effective. 131 00:06:36,340 --> 00:06:38,453 And in the first attempt, it is not.