1 00:00:07,200 --> 00:00:10,560 - In this video, you will learn about Ansible roles. 2 00:00:10,560 --> 00:00:11,800 So what is a role? 3 00:00:11,800 --> 00:00:15,030 Well, a role is a community provided resource 4 00:00:15,030 --> 00:00:17,720 to make working with Ansible easier. 5 00:00:17,720 --> 00:00:20,910 They provide standardized solutions for common tasks. 6 00:00:20,910 --> 00:00:23,080 The idea is very simple to understand. 7 00:00:23,080 --> 00:00:26,790 Let's say you want to use Ansible to configure Docker. 8 00:00:26,790 --> 00:00:27,623 Do you really think 9 00:00:27,623 --> 00:00:29,560 that you are the first person on the planet 10 00:00:29,560 --> 00:00:31,970 who wants to use Ansible to configure Docker? 11 00:00:31,970 --> 00:00:33,290 Most likely not. 12 00:00:33,290 --> 00:00:35,390 That's where roles come in. 13 00:00:35,390 --> 00:00:36,380 From the community, 14 00:00:36,380 --> 00:00:38,780 different members of the community create roles 15 00:00:38,780 --> 00:00:42,420 and they publish these roles on Ansible Galaxy. 16 00:00:42,420 --> 00:00:43,560 And the nice thing is, 17 00:00:43,560 --> 00:00:46,090 you can fetch them from Ansible Galaxy, 18 00:00:46,090 --> 00:00:47,850 include them in your playbooks, 19 00:00:47,850 --> 00:00:50,370 and just use them as standardized code. 20 00:00:50,370 --> 00:00:52,190 And that allows you to focus on the things 21 00:00:52,190 --> 00:00:54,530 that need to be different in your environment. 22 00:00:54,530 --> 00:00:56,130 That's what roles are all about. 23 00:00:57,540 --> 00:00:59,670 So, yeah, that's what I just said. 24 00:00:59,670 --> 00:01:01,130 Instead of finding out yourself, 25 00:01:01,130 --> 00:01:04,400 you can use community provided roles in your playbook. 26 00:01:04,400 --> 00:01:06,000 And the only thing you need to do, 27 00:01:06,000 --> 00:01:09,560 is to add site-specific configuration using variables 28 00:01:09,560 --> 00:01:12,240 and learn everything else from the roles. 29 00:01:12,240 --> 00:01:13,980 You can also work with customer roles 30 00:01:13,980 --> 00:01:15,600 where you create your own roles. 31 00:01:15,600 --> 00:01:17,230 That will be the next step. 32 00:01:17,230 --> 00:01:20,500 If you think that there is not sufficient roles available, 33 00:01:20,500 --> 00:01:22,940 you can develop some for yourself. 34 00:01:22,940 --> 00:01:24,140 When you are working with roles, 35 00:01:24,140 --> 00:01:27,143 it's important to understand task execution order. 36 00:01:28,680 --> 00:01:30,700 Tasks in roles are normally executed 37 00:01:30,700 --> 00:01:33,250 before other tasks in the playbook. 38 00:01:33,250 --> 00:01:34,790 If you want to change that behavior, 39 00:01:34,790 --> 00:01:36,920 you can you can use pre_tasks 40 00:01:36,920 --> 00:01:39,630 to execute tasks before the role. 41 00:01:39,630 --> 00:01:41,250 That is where you need to make sure 42 00:01:41,250 --> 00:01:42,690 that your system is updated, 43 00:01:42,690 --> 00:01:44,300 some configuration is started, 44 00:01:44,300 --> 00:01:45,970 firewall software is installed, 45 00:01:45,970 --> 00:01:49,540 or whatever task you need to take care of, 46 00:01:49,540 --> 00:01:52,290 and which is not taken care of by the role. 47 00:01:52,290 --> 00:01:54,570 So if it isn't taken care of by the role 48 00:01:54,570 --> 00:01:55,890 and if it's important to happen 49 00:01:55,890 --> 00:01:58,323 before you run your playbook, run pre_task. 50 00:01:59,860 --> 00:02:02,740 If your pre_tasks are using handlers, 51 00:02:02,740 --> 00:02:04,790 then handlers that are triggered by pre_tasks 52 00:02:04,790 --> 00:02:07,233 are also executed before the roles. 53 00:02:08,570 --> 00:02:10,303 Let's go check out an example. 54 00:02:12,150 --> 00:02:16,853 So the roles example are in the Git repository in roles. 55 00:02:17,810 --> 00:02:18,770 So what do we have? 56 00:02:18,770 --> 00:02:21,500 We have Nginx role, 57 00:02:21,500 --> 00:02:25,780 and in Nginx role, we are using Geerlingguy.nginx. 58 00:02:25,780 --> 00:02:29,823 And this role is going to be executed only on Ansible two. 59 00:02:30,780 --> 00:02:34,560 In fact, we can even make it more minimal. 60 00:02:34,560 --> 00:02:36,210 This is all that we need. 61 00:02:36,210 --> 00:02:39,270 So you need to tell where the role needs to be executed, 62 00:02:39,270 --> 00:02:41,853 and you need to specify the names of the roles 63 00:02:41,853 --> 00:02:43,453 that you want to execute. 64 00:02:44,450 --> 00:02:47,110 Notice that you can include more than one role. 65 00:02:47,110 --> 00:02:50,750 So here, I'm using one role only, which is pretty common. 66 00:02:50,750 --> 00:02:52,780 But if in the playbook you are creating 67 00:02:52,780 --> 00:02:54,830 a solution-based on multiple roles, 68 00:02:54,830 --> 00:02:57,650 you can include all of them right here 69 00:02:57,650 --> 00:03:00,640 in the order in which you want to include them. 70 00:03:00,640 --> 00:03:01,920 There's one requirement though, 71 00:03:01,920 --> 00:03:06,690 and that is that, Geerlingguy Nginx role is installed. 72 00:03:06,690 --> 00:03:10,630 I will tell you later how you can easily find roles, 73 00:03:10,630 --> 00:03:14,133 but for now, Ansible Galaxy minus minus help. 74 00:03:14,970 --> 00:03:16,770 I like using the minus minus help 75 00:03:16,770 --> 00:03:19,760 especially since the Ansible Galaxy command 76 00:03:19,760 --> 00:03:23,540 has changed with the appearance of the collection. 77 00:03:23,540 --> 00:03:24,920 There we can see there's a role 78 00:03:24,920 --> 00:03:29,183 and then using install Geerlingguy.nginx. 79 00:03:32,130 --> 00:03:33,660 So that is downloading the role 80 00:03:33,660 --> 00:03:37,290 that is developed by Mr. Geerling. 81 00:03:37,290 --> 00:03:40,150 Next, I can use Ansible Playbook 82 00:03:40,150 --> 00:03:42,590 just the way we used to do it 83 00:03:42,590 --> 00:03:46,880 on this Nginxrole.yml. 84 00:03:46,880 --> 00:03:49,650 So here we can see that Ansible Playbook 85 00:03:49,650 --> 00:03:52,223 Nginxrole.yml is working. 86 00:03:53,190 --> 00:03:55,643 So it is doing everything 87 00:03:55,643 --> 00:03:59,460 that it is requested to do in the role. 88 00:03:59,460 --> 00:04:02,900 That might take a minute if there is no conflicts. 89 00:04:02,900 --> 00:04:06,480 Notice that because of the HTTPD, 90 00:04:06,480 --> 00:04:09,840 that we've been working with throughout in this course, 91 00:04:09,840 --> 00:04:10,820 we might have an issue. 92 00:04:10,820 --> 00:04:13,690 If HTTPD is already listening on part 80, 93 00:04:13,690 --> 00:04:16,210 you're not going to be able to work with Nginx 94 00:04:16,210 --> 00:04:18,360 but that's not an issue here. 95 00:04:18,360 --> 00:04:21,930 And that's an example of how you can use roles 96 00:04:21,930 --> 00:04:23,010 in an easy way. 97 00:04:23,010 --> 00:04:24,560 Now let's go and investigate 98 00:04:24,560 --> 00:04:27,403 what is really happening when you are using roles.