1 00:00:07,060 --> 00:00:09,050 - In this video, I will show you how to create 2 00:00:09,050 --> 00:00:10,800 your own roles. 3 00:00:10,800 --> 00:00:14,960 Back to roles that is for a simple reason that roles, 4 00:00:14,960 --> 00:00:16,860 well, they're part of the collections. 5 00:00:23,620 --> 00:00:25,670 So I want to show you what you can do 6 00:00:25,670 --> 00:00:27,440 to set up your own role. 7 00:00:27,440 --> 00:00:31,630 And in order to do so, let me go to the roles directory 8 00:00:31,630 --> 00:00:35,343 and let's investigate motd as well as motd2. 9 00:00:35,343 --> 00:00:36,600 What is the thing? 10 00:00:36,600 --> 00:00:40,873 If you use Ansible galaxy role in it, 11 00:00:43,000 --> 00:00:45,570 my role, for instance, 12 00:00:45,570 --> 00:00:48,970 then you create the directory circulates required 13 00:00:48,970 --> 00:00:50,920 to create your own role. 14 00:00:50,920 --> 00:00:54,680 So if I use tree, my role, then what do I get? 15 00:00:54,680 --> 00:00:56,330 I get these different directories 16 00:00:57,200 --> 00:00:59,053 that you should always have in role. 17 00:00:59,900 --> 00:01:02,230 Now you can just fill it out 18 00:01:02,230 --> 00:01:04,190 and make sure that the appropriate information 19 00:01:04,190 --> 00:01:06,320 is where it needs to be. 20 00:01:06,320 --> 00:01:08,980 I am going to show you in the role sub directory 21 00:01:08,980 --> 00:01:12,000 the motd role. 22 00:01:12,000 --> 00:01:16,920 And in the motd role, we find a very simple example. 23 00:01:16,920 --> 00:01:19,760 First, I cleaned up the directory searcher, 24 00:01:19,760 --> 00:01:22,520 everything that's not required is deleted. 25 00:01:22,520 --> 00:01:27,230 So we only have defaults meta tasks and templates. 26 00:01:27,230 --> 00:01:29,923 If you go there in order of importancy, 27 00:01:29,923 --> 00:01:32,820 first in meta, what do we have in meta? 28 00:01:32,820 --> 00:01:34,480 Meta is for metadata 29 00:01:34,480 --> 00:01:38,340 and metadata is information that will typically end up 30 00:01:38,340 --> 00:01:39,693 in Ansible Galaxy. 31 00:01:40,570 --> 00:01:45,440 So this is where you are going to make sure that all the 32 00:01:46,280 --> 00:01:48,490 important information is there. 33 00:01:48,490 --> 00:01:51,240 Well, this role is going to be so important. 34 00:01:51,240 --> 00:01:53,923 We need Ansible 2.8 and later. 35 00:01:55,200 --> 00:01:59,800 Of course change anything that needs to be changed. 36 00:01:59,800 --> 00:02:02,810 Next, let's have a look at tasks. 37 00:02:02,810 --> 00:02:04,493 In tasks there's main.yml. 38 00:02:05,460 --> 00:02:08,270 And in this main.yml, this task is very simple. 39 00:02:08,270 --> 00:02:11,863 This task is only copying an motd file. 40 00:02:12,750 --> 00:02:17,750 The sources templates, motd destination is motd. 41 00:02:17,910 --> 00:02:19,550 And you know what? 42 00:02:19,550 --> 00:02:23,290 Let's make that new motd so that we don't override anything 43 00:02:23,290 --> 00:02:24,363 and mess up things. 44 00:02:25,200 --> 00:02:28,160 So this task is working with the template. 45 00:02:28,160 --> 00:02:32,883 The template is what we have right here, the motd.j2. 46 00:02:33,790 --> 00:02:37,440 And in this template, we can see there's a couple of Ansible 47 00:02:37,440 --> 00:02:40,760 effects and there is a contact message, 48 00:02:40,760 --> 00:02:44,080 contact system manager if anything is wrong. 49 00:02:44,080 --> 00:02:47,260 So the system manager is a variable that you need to provide 50 00:02:47,260 --> 00:02:51,740 and it is provided through defaults main.yml. 51 00:02:51,740 --> 00:02:53,080 The thing in the defaults 52 00:02:53,080 --> 00:02:56,090 is that defaults contains some standard variables 53 00:02:56,090 --> 00:02:58,190 and it's okay to override them. You don't need them. 54 00:02:58,190 --> 00:03:00,350 You wanna work with own. That's okay. 55 00:03:00,350 --> 00:03:03,880 We just need a default value for the variables to ensure 56 00:03:03,880 --> 00:03:05,620 that the role is not going to fill 57 00:03:05,620 --> 00:03:09,100 because you are missing default values. 58 00:03:09,100 --> 00:03:13,780 And the rest of it is going to happen from the Playbook. 59 00:03:13,780 --> 00:03:16,150 Now notice that in the directory that you are using, 60 00:03:16,150 --> 00:03:19,020 the roles are supposed to be in the roles directory. 61 00:03:19,020 --> 00:03:23,330 Oh, oh, my role, wrong directory my friends. 62 00:03:23,330 --> 00:03:26,090 Might as well rm minus rf it 63 00:03:26,090 --> 00:03:29,030 because we don't need it anymore anyways. 64 00:03:29,030 --> 00:03:33,110 Let's have a look at motdrole.yml, 65 00:03:33,110 --> 00:03:34,520 where we can see 66 00:03:36,790 --> 00:03:40,773 that this role is executed on Ansible2. 67 00:03:41,650 --> 00:03:45,970 The other information, not really important. 68 00:03:45,970 --> 00:03:48,210 The important part is right here. 69 00:03:48,210 --> 00:03:52,450 So roles, we call role motd and how we override system, 70 00:03:52,450 --> 00:03:53,800 system manager. 71 00:03:53,800 --> 00:03:55,320 Now what's going to be the results? 72 00:03:55,320 --> 00:04:00,230 Well, Ansible playbook motdrole.yml 73 00:04:00,230 --> 00:04:02,163 is going to do the role. 74 00:04:03,240 --> 00:04:06,200 And if you check Ansible on Ansible2 75 00:04:07,651 --> 00:04:09,190 - a 76 00:04:09,190 --> 00:04:10,023 cat etc 77 00:04:12,003 --> 00:04:12,836 newmotd, 78 00:04:14,910 --> 00:04:17,230 then what do we see? 79 00:04:17,230 --> 00:04:19,810 We see, yay, welcome to Ansible2. 80 00:04:19,810 --> 00:04:21,500 Everything is looking okay. 81 00:04:21,500 --> 00:04:25,780 And the interesting fact, it is overwritten at the variable. 82 00:04:25,780 --> 00:04:28,780 Now there's a couple of more things that I want to show you 83 00:04:28,780 --> 00:04:31,700 about roles, and that is these pre and posts. 84 00:04:31,700 --> 00:04:35,500 If you look at pre tasks, what do we see in pre tasks? 85 00:04:35,500 --> 00:04:38,300 This is an example of how you can use pre tasks. 86 00:04:38,300 --> 00:04:42,100 Why would you want to update your cash before rolling motd? 87 00:04:42,100 --> 00:04:43,020 I don't know 88 00:04:43,020 --> 00:04:45,270 but I just wanted to show you the pre-task 89 00:04:45,270 --> 00:04:47,003 and that is how it works. 90 00:04:48,007 --> 00:04:51,200 And so pre-task is important. 91 00:04:51,200 --> 00:04:54,110 Pre-task is to define the order of tasks 92 00:04:54,110 --> 00:04:56,520 and how they are going to be started. 93 00:04:56,520 --> 00:04:59,180 And that is about it. Enough about roles. 94 00:04:59,180 --> 00:05:00,963 Let's go to the end of lesson lab.