1 00:00:06,813 --> 00:00:09,170 - All right, so we need to write a playbook 2 00:00:09,170 --> 00:00:10,960 that uses the ngnx role 3 00:00:10,960 --> 00:00:14,323 to install ngnx on (mumbles) family host. 4 00:00:15,200 --> 00:00:19,933 So, let me first use ansiable-galaxy role list 5 00:00:21,220 --> 00:00:22,780 just to verify. 6 00:00:22,780 --> 00:00:23,730 And there we go, 7 00:00:23,730 --> 00:00:26,440 geerlingguy nginx is already installed. 8 00:00:26,440 --> 00:00:27,273 Geerlingguy. 9 00:00:27,273 --> 00:00:29,370 If geerlingguy has written it, is good. 10 00:00:29,370 --> 00:00:31,670 You can safely use it. 11 00:00:31,670 --> 00:00:34,550 So, let me create lab8.yaml 12 00:00:36,630 --> 00:00:39,200 and in lab8.yaml 13 00:00:40,620 --> 00:00:44,763 install nginx, hosts, all. 14 00:00:46,850 --> 00:00:50,680 In tasks, well, no tasks, 15 00:00:50,680 --> 00:00:53,663 we are going to start with pre-tasks. 16 00:00:54,500 --> 00:00:55,900 So, what are we going to do? 17 00:00:56,740 --> 00:00:59,920 Well, I'm still in development phase. 18 00:00:59,920 --> 00:01:02,453 Let's do remove Apache. 19 00:01:05,640 --> 00:01:07,533 Next, do we need anything else? 20 00:01:10,380 --> 00:01:12,200 Perform package update 21 00:01:13,520 --> 00:01:17,423 and then we can use the roles. 22 00:01:18,500 --> 00:01:21,850 So, in the roles we are going to run 23 00:01:23,905 --> 00:01:24,805 geerlingguy.nginx. 24 00:01:27,240 --> 00:01:28,510 And after running the role, 25 00:01:28,510 --> 00:01:30,680 remove the yum package cache. 26 00:01:30,680 --> 00:01:33,410 Well, formally you can use any tasks. 27 00:01:33,410 --> 00:01:36,080 If you wanna be more specific about it, 28 00:01:36,080 --> 00:01:39,413 you can run post tasks. 29 00:01:40,370 --> 00:01:44,180 And I think we have a problem here in the indentations. 30 00:01:44,180 --> 00:01:45,363 So, let me fix that. 31 00:01:47,170 --> 00:01:50,220 And let's have a look at the example as well. 32 00:01:50,220 --> 00:01:53,530 So, the role is geerlingguy nginx 33 00:01:53,530 --> 00:01:55,570 pre-task roles and post task, 34 00:01:55,570 --> 00:01:58,820 right now we have them all in the play header 35 00:01:58,820 --> 00:02:00,513 as level one items. 36 00:02:01,390 --> 00:02:04,660 This is why I love having a look at examples. 37 00:02:04,660 --> 00:02:08,190 So, pre-tasks, and there you can see pre-tasks and roles 38 00:02:08,190 --> 00:02:11,300 and that is looking okay. 39 00:02:11,300 --> 00:02:13,050 So, I can get back to my lab eight. 40 00:02:13,050 --> 00:02:14,770 So, remove Apache. 41 00:02:14,770 --> 00:02:16,223 How are we going to do that? 42 00:02:17,440 --> 00:02:21,750 Well, if we are going to do it decently, 43 00:02:21,750 --> 00:02:26,750 service, name httpd, state stopped, enabled no, 44 00:02:35,730 --> 00:02:37,870 as well as package. 45 00:02:42,710 --> 00:02:43,543 Oh, 46 00:02:47,380 --> 00:02:48,750 I'm skipping over the name here 47 00:02:48,750 --> 00:02:50,800 because that doesn't really add anything. 48 00:02:51,680 --> 00:02:56,680 Name httpd, state absent, 49 00:02:58,370 --> 00:03:00,220 and oh, now look at that. 50 00:03:00,220 --> 00:03:02,170 We need some conditionals as well. 51 00:03:02,170 --> 00:03:05,780 And in order to make that a conditional, 52 00:03:05,780 --> 00:03:07,023 shall we we use a block? 53 00:03:08,270 --> 00:03:12,303 So, in a block we can include multiple tasks. 54 00:03:24,790 --> 00:03:26,730 And this again is one of these cases 55 00:03:26,730 --> 00:03:29,260 where I think you should verify your syntax 56 00:03:29,260 --> 00:03:33,330 this time let's go to the Ansible documentation. 57 00:03:33,330 --> 00:03:35,513 So, docs.ansible.com. 58 00:03:36,620 --> 00:03:39,203 So, let's go to the Ansible documentation, 59 00:03:40,140 --> 00:03:42,650 community documentation. 60 00:03:42,650 --> 00:03:43,483 Oh, 61 00:03:46,750 --> 00:03:51,383 2.9 and I want an example for block. 62 00:03:52,270 --> 00:03:54,120 Block is typically one of these cases 63 00:03:54,120 --> 00:03:55,410 that you don't use too often, 64 00:03:55,410 --> 00:03:58,820 so it's wise to look it up and there we go. 65 00:03:58,820 --> 00:04:01,820 So, we have tasks and in tasks, all right, 66 00:04:01,820 --> 00:04:04,400 the block is just a part of the task. 67 00:04:04,400 --> 00:04:06,420 So, it's the other way around, 68 00:04:06,420 --> 00:04:07,863 smart move to look it up. 69 00:04:09,030 --> 00:04:12,920 So, block is just a part of the task 70 00:04:12,920 --> 00:04:14,800 and yeah, that is what we've got here. 71 00:04:14,800 --> 00:04:16,460 So, this should be good. 72 00:04:16,460 --> 00:04:21,460 And then how did we have the dependencies? 73 00:04:21,640 --> 00:04:26,270 You can see the dependencies are in the block specification. 74 00:04:26,270 --> 00:04:29,910 The when statement is in the block specification 75 00:04:29,910 --> 00:04:32,450 and you know what, 76 00:04:32,450 --> 00:04:35,660 when Ansible facts distribution equals CentOS, 77 00:04:35,660 --> 00:04:37,090 this is so close to what I need 78 00:04:37,090 --> 00:04:39,580 and I'm just going to copy paste it. 79 00:04:39,580 --> 00:04:42,200 So, oh, there we go. 80 00:04:42,200 --> 00:04:46,380 So, I'm first finishing this block configuration 81 00:04:47,230 --> 00:04:50,880 and that is gonna be right here. 82 00:04:50,880 --> 00:04:53,920 So, when Ansible facts distribution 83 00:04:54,920 --> 00:04:58,360 that needs to be indented at a block level. 84 00:04:58,360 --> 00:04:59,437 So, we have that all right. 85 00:04:59,437 --> 00:05:02,010 And we will continue this later. 86 00:05:02,010 --> 00:05:03,720 Name perform package update, 87 00:05:03,720 --> 00:05:05,010 that's a part of the block. 88 00:05:05,010 --> 00:05:10,010 So, that would be, well, you know what? 89 00:05:11,260 --> 00:05:13,500 We are on (mumbles) family only. 90 00:05:13,500 --> 00:05:14,770 I'm changing my mind. 91 00:05:14,770 --> 00:05:16,060 Why would you use package, 92 00:05:16,060 --> 00:05:18,960 if you are on one specific distribution? 93 00:05:18,960 --> 00:05:22,640 Always go for the most specific solution. 94 00:05:22,640 --> 00:05:24,230 Now, we have this yum 95 00:05:24,230 --> 00:05:28,630 and yum certainly has something to update packages. 96 00:05:28,630 --> 00:05:32,580 Ansible-doc on yum will tell us about it. 97 00:05:32,580 --> 00:05:35,050 So, yum updates only, 98 00:05:37,774 --> 00:05:38,810 that suite is okay. 99 00:05:38,810 --> 00:05:42,380 And force yum to check if cache is out of date 100 00:05:42,380 --> 00:05:44,880 and re-download if needed. 101 00:05:44,880 --> 00:05:48,693 I think that should be yum update only. 102 00:05:50,470 --> 00:05:53,340 So, let's double check these updates 103 00:05:53,340 --> 00:05:57,690 especially in the list. 104 00:05:57,690 --> 00:06:00,330 So, we need to specify what we want to do, right? 105 00:06:00,330 --> 00:06:02,590 So, I am going to use state 106 00:06:02,590 --> 00:06:06,780 and there we can see state latest. 107 00:06:06,780 --> 00:06:11,480 So, that means that I am going to use 108 00:06:13,540 --> 00:06:17,687 name, star, state latest. 109 00:06:19,320 --> 00:06:20,543 That should be doing it. 110 00:06:21,470 --> 00:06:24,010 When Ansible distribution facts equals. 111 00:06:24,010 --> 00:06:28,920 Now, the when can be specified as a list. 112 00:06:28,920 --> 00:06:33,303 So, Ansible facts distribution underscore family. 113 00:06:37,060 --> 00:06:41,940 It's all about family is either Red Hat 114 00:06:44,170 --> 00:06:49,170 Or Ansible facts distribution family is Rocky. 115 00:06:59,740 --> 00:07:00,850 Hey, is that correct? 116 00:07:00,850 --> 00:07:03,150 No, because if you use a list, 117 00:07:03,150 --> 00:07:04,823 then all of them must be true. 118 00:07:05,830 --> 00:07:09,430 So, we better get back to an or statement. 119 00:07:09,430 --> 00:07:14,430 So, when, Ansible facts distribution blah blah, 120 00:07:14,500 --> 00:07:16,150 this is what we need. 121 00:07:16,150 --> 00:07:18,980 Next, we are going to include the role finally. 122 00:07:18,980 --> 00:07:20,670 And then we have the post task 123 00:07:21,917 --> 00:07:25,810 and in the post task we are going to do what? 124 00:07:25,810 --> 00:07:29,280 We are going to run just one task, 125 00:07:29,280 --> 00:07:32,355 which is going to be the yum task again. 126 00:07:32,355 --> 00:07:36,923 And what is the yum task going to do? 127 00:07:37,860 --> 00:07:40,250 Yum task is... 128 00:07:40,250 --> 00:07:43,760 Well we have an update cache and an update only. 129 00:07:43,760 --> 00:07:46,770 So, last task is to run the yum package cache 130 00:07:46,770 --> 00:07:47,920 but where is it? 131 00:07:47,920 --> 00:07:49,580 Sudo minus I. 132 00:07:49,580 --> 00:07:53,063 We have this var cache directory. 133 00:07:55,750 --> 00:07:58,900 And in var cache, we have a dnf directory. 134 00:07:58,900 --> 00:08:01,980 And in the dnf directory, just looks like a cache. 135 00:08:01,980 --> 00:08:04,040 And I don't know 136 00:08:04,040 --> 00:08:06,470 there's a specific option to remove the cache, 137 00:08:06,470 --> 00:08:08,330 but I do know 138 00:08:08,330 --> 00:08:12,333 that we are just going to use file path var cache, 139 00:08:17,180 --> 00:08:22,180 dnf, state absent recurse. 140 00:08:24,590 --> 00:08:25,920 Yes. 141 00:08:25,920 --> 00:08:27,150 Is this going to do it? 142 00:08:27,150 --> 00:08:28,840 Well, at least it worked 143 00:08:28,840 --> 00:08:30,483 giving it a first try. 144 00:08:31,350 --> 00:08:34,920 So, Ansible playbook on lab8.yaml. 145 00:08:34,920 --> 00:08:35,910 Here we go. 146 00:08:35,910 --> 00:08:36,780 And oh, no. 147 00:08:36,780 --> 00:08:37,670 Yeah, of course. 148 00:08:37,670 --> 00:08:40,270 If you make big playbooks like this 149 00:08:40,270 --> 00:08:43,360 then you might get errors. 150 00:08:43,360 --> 00:08:48,320 You can see that yum name starred doesn't work out. 151 00:08:48,320 --> 00:08:53,320 So, let's get back to the Ansible doc for yum. 152 00:08:53,730 --> 00:08:58,023 And do we see anything for name argument? 153 00:08:59,020 --> 00:09:02,470 So, name, oh, when using status latest 154 00:09:02,470 --> 00:09:06,610 this can be a single quote, star, single quote. 155 00:09:06,610 --> 00:09:08,820 So, single quote, star, single quote. 156 00:09:08,820 --> 00:09:10,020 It's a quoting issue. 157 00:09:10,020 --> 00:09:11,110 Often in Ansible, 158 00:09:11,110 --> 00:09:12,623 it will be a quoting issue. 159 00:09:13,660 --> 00:09:17,690 So, let's put this between single quotes 160 00:09:18,600 --> 00:09:23,000 and let's give it another try, try number two. 161 00:09:23,000 --> 00:09:25,630 And what do we get? 162 00:09:25,630 --> 00:09:27,479 That's an interesting one. 163 00:09:27,479 --> 00:09:32,479 The role geerlingguy nginx was not found in home Ansible. 164 00:09:34,230 --> 00:09:36,280 Yeah, that's me, that's a typo. 165 00:09:36,280 --> 00:09:39,430 Geerling is the family name 166 00:09:39,430 --> 00:09:42,470 and guy that is the dude, 167 00:09:42,470 --> 00:09:43,510 the Geerling dude. 168 00:09:43,510 --> 00:09:45,193 And that's a double G. 169 00:09:47,890 --> 00:09:48,723 As you can see, 170 00:09:48,723 --> 00:09:50,780 we are doing a little bit better, but oh, no. 171 00:09:50,780 --> 00:09:52,610 Remove Ansible. 172 00:09:52,610 --> 00:09:53,443 What do we get? 173 00:09:53,443 --> 00:09:56,950 Conditional check, Ansible facts distribution family 174 00:09:56,950 --> 00:09:59,950 or Ansible facts distribution Rocky error 175 00:09:59,950 --> 00:10:02,910 while evaluating conditional. 176 00:10:02,910 --> 00:10:05,742 And we can see, 177 00:10:05,742 --> 00:10:10,742 has no attribute distribution family. 178 00:10:11,810 --> 00:10:13,173 That's an interesting one. 179 00:10:14,120 --> 00:10:16,430 We can quickly check that Ansible 180 00:10:16,430 --> 00:10:19,413 on Ansible one minus m setup, 181 00:10:21,320 --> 00:10:26,080 and let's just search for family. 182 00:10:26,080 --> 00:10:28,970 And aha! It's not distribution family, 183 00:10:28,970 --> 00:10:31,000 it is OS family. 184 00:10:31,000 --> 00:10:32,437 And everybody, you might think, 185 00:10:32,437 --> 00:10:34,547 "Hey Sander, don't you know where you were going 186 00:10:34,547 --> 00:10:36,070 when you were starting?" 187 00:10:36,070 --> 00:10:36,903 Of course I do. 188 00:10:36,903 --> 00:10:39,110 But the thing is that 189 00:10:39,110 --> 00:10:41,860 it's so easy to get confused 190 00:10:41,860 --> 00:10:45,480 and you will learn so much more from my errors. 191 00:10:45,480 --> 00:10:50,480 I mean, if everything is working smooth from the beginning 192 00:10:50,550 --> 00:10:52,640 you are going to try to memorize it. 193 00:10:52,640 --> 00:10:54,580 I don't want you to memorize it. 194 00:10:54,580 --> 00:10:58,100 I want you to understand how you can find the information. 195 00:10:58,100 --> 00:11:01,783 So, OS family let's work with OS family. 196 00:11:03,370 --> 00:11:04,650 Yes. OS family. 197 00:11:04,650 --> 00:11:09,650 There we go again, Ansible playbook on lab8.yaml. 198 00:11:11,178 --> 00:11:13,020 And of course perform package update 199 00:11:13,020 --> 00:11:14,943 that might take a minute or two. 200 00:11:18,070 --> 00:11:20,770 So, as you can see the packages update is finally done 201 00:11:20,770 --> 00:11:22,920 and well, this is sufficient proof 202 00:11:22,920 --> 00:11:25,970 that the Pre-tasks are doing what they need to do. 203 00:11:25,970 --> 00:11:30,500 Right now, we are in the Ansible role. 204 00:11:30,500 --> 00:11:32,460 So, the role is continuing 205 00:11:32,460 --> 00:11:35,843 and we should be good in just a little while. 206 00:11:37,850 --> 00:11:39,290 You can see a little bit of work 207 00:11:39,290 --> 00:11:40,910 to be done on the directory. 208 00:11:40,910 --> 00:11:42,460 So, what do we see a recurse option 209 00:11:42,460 --> 00:11:47,070 requires state to be directory, owner, root, blah blah. 210 00:11:47,070 --> 00:11:51,593 So, how about we do that without? 211 00:11:53,540 --> 00:11:56,930 Then the sad thing is that you need to run it again. 212 00:11:56,930 --> 00:11:58,830 Do we really need to run it again? 213 00:11:58,830 --> 00:12:00,760 Let's have a look at Ansible playbook 214 00:12:00,760 --> 00:12:04,100 dash dash help pipe less. 215 00:12:04,100 --> 00:12:08,520 We have not yet looked at all these different options. 216 00:12:08,520 --> 00:12:10,110 And what I'm interested in here 217 00:12:10,110 --> 00:12:13,630 is list task and start at task. 218 00:12:13,630 --> 00:12:16,750 So, Ansible playbook lab8.yaml 219 00:12:16,750 --> 00:12:20,240 dash dash list tasks is showing us what? 220 00:12:20,240 --> 00:12:23,180 Is showing us all these different tasks. 221 00:12:23,180 --> 00:12:28,077 So, I am going to use a start at task file. 222 00:12:30,240 --> 00:12:31,810 That makes it so much easier 223 00:12:31,810 --> 00:12:33,910 to just run this file task again 224 00:12:33,910 --> 00:12:35,580 and there you can see without a recurse 225 00:12:35,580 --> 00:12:37,100 it is doing all right. 226 00:12:37,100 --> 00:12:38,780 So, it was a little bit of work. 227 00:12:38,780 --> 00:12:40,840 I hope you appreciated the insight 228 00:12:40,840 --> 00:12:42,510 into getting to the result 229 00:12:42,510 --> 00:12:47,340 but this is what I had in mind for the lesson eight lab. 230 00:12:47,340 --> 00:12:49,203 Let's continue with the next lesson.