1 00:00:07,263 --> 00:00:10,340 - In this video I'm going to tell you about Galaxy. 2 00:00:10,340 --> 00:00:12,569 So what is Ansible Galaxy? 3 00:00:12,569 --> 00:00:15,700 Ansible Galaxy or galaxy.ansible.com, 4 00:00:15,700 --> 00:00:18,360 is a community website behind Ansible. 5 00:00:18,360 --> 00:00:19,684 And it is mainly used 6 00:00:19,684 --> 00:00:22,943 for providing access to roles and collections. 7 00:00:23,970 --> 00:00:25,060 If you use galaxy 8 00:00:25,060 --> 00:00:27,410 you can easily find the content that you need 9 00:00:27,410 --> 00:00:31,720 even the description and the information, all about it. 10 00:00:31,720 --> 00:00:33,480 There's an ansible-galaxy command line, 11 00:00:33,480 --> 00:00:35,780 we've already seen it in the previous video 12 00:00:35,780 --> 00:00:38,330 which you can easily use to work with your content. 13 00:00:39,300 --> 00:00:42,420 And you can use ansible-galaxy collection to 14 00:00:42,420 --> 00:00:47,079 manage collections or ansible-galaxy role to manage roles. 15 00:00:47,079 --> 00:00:50,460 Instead of spending a lot of time in slides about galaxy 16 00:00:50,460 --> 00:00:51,873 let's just go check it out. 17 00:00:53,460 --> 00:00:55,430 So galaxy.ansible.com 18 00:00:55,430 --> 00:00:56,660 is where you go to. 19 00:00:56,660 --> 00:00:58,520 This is Ansible Galaxy. 20 00:00:58,520 --> 00:01:01,580 In the past Ansible Galaxy was all about roles. 21 00:01:01,580 --> 00:01:05,480 Nowadays, Ansible Galaxy is also about collections. 22 00:01:05,480 --> 00:01:06,670 But it's easy to work with 23 00:01:06,670 --> 00:01:09,560 once you understand what you're looking for. 24 00:01:09,560 --> 00:01:12,810 So let's do a search and let's go 25 00:01:12,810 --> 00:01:15,550 for this nginx that we've used before. 26 00:01:15,550 --> 00:01:18,700 So I'm using nginx and I'm pressing enter. 27 00:01:18,700 --> 00:01:20,060 Now what is happening? 28 00:01:20,060 --> 00:01:23,630 Well to start with you see 37 collections. 29 00:01:23,630 --> 00:01:25,000 Collections are a preferred way 30 00:01:25,000 --> 00:01:28,380 to work with Ansible contents nowadays. 31 00:01:28,380 --> 00:01:29,590 So that makes sense. 32 00:01:29,590 --> 00:01:32,340 And you can see that in the Ansible Galaxy collection 33 00:01:32,340 --> 00:01:34,600 there are three roles that are available 34 00:01:34,600 --> 00:01:37,940 but you can also use a filter. 35 00:01:37,940 --> 00:01:39,400 If you just want to see your roles 36 00:01:39,400 --> 00:01:43,950 then you select role from the filter and you see the roles. 37 00:01:43,950 --> 00:01:47,060 First, I would like to talk about these roles independently. 38 00:01:47,060 --> 00:01:48,318 That's especially convenient 39 00:01:48,318 --> 00:01:51,770 if you're still on Ansible 2.9 and earlier. 40 00:01:51,770 --> 00:01:54,020 Let's check out this nginx role. 41 00:01:54,020 --> 00:01:57,250 This is the nginx role that we have used before. 42 00:01:57,250 --> 00:01:58,560 So here is the website. 43 00:01:58,560 --> 00:02:00,052 This is Mr. Geerlingguy. 44 00:02:00,052 --> 00:02:03,350 This is one of the main people in the Ansible community. 45 00:02:03,350 --> 00:02:07,944 And Mr. Geerling has created this role. 46 00:02:07,944 --> 00:02:10,010 So you can find some information, 47 00:02:10,010 --> 00:02:13,170 including the information on how to use it. 48 00:02:13,170 --> 00:02:16,424 The minimal Ansible version quite important as well. 49 00:02:16,424 --> 00:02:17,997 And supportability, 50 00:02:17,997 --> 00:02:19,600 that's the information on where 51 00:02:19,600 --> 00:02:21,530 you can use this specific role. 52 00:02:21,530 --> 00:02:25,940 And also quite convenient, there is a Read Me. 53 00:02:25,940 --> 00:02:28,520 So if you want to figure out how to use this role 54 00:02:29,490 --> 00:02:31,170 then here you can see it. 55 00:02:31,170 --> 00:02:34,930 Most roles have a very detailed documentation. 56 00:02:34,930 --> 00:02:36,340 Of course the documentation 57 00:02:36,340 --> 00:02:38,861 all depends on the quality of the role. 58 00:02:38,861 --> 00:02:40,916 Some documentation is quite good. 59 00:02:40,916 --> 00:02:45,210 Some documentation sadly is not. 60 00:02:45,210 --> 00:02:46,760 Now behind Ansible Galaxy 61 00:02:46,760 --> 00:02:49,700 there is also the Ansible Galaxy command. 62 00:02:49,700 --> 00:02:52,630 My advice, use the Ansible Galaxy website 63 00:02:52,630 --> 00:02:54,690 to find the content that you need 64 00:02:54,690 --> 00:02:59,690 and then use the Ansible Galaxy command for everything else. 65 00:03:00,100 --> 00:03:03,626 So I'm using ansible-galaxy role, 66 00:03:03,626 --> 00:03:07,320 minus h, minus h, to see what we can do. 67 00:03:07,320 --> 00:03:10,840 Very convenient command is ansible-galaxy role list. 68 00:03:10,840 --> 00:03:14,440 So if I do my ansible-galaxy role list, 69 00:03:14,440 --> 00:03:17,260 I can see all the different roles that are installed. 70 00:03:17,260 --> 00:03:18,410 And what do we see? 71 00:03:18,410 --> 00:03:21,620 We see that currently there are three different directories. 72 00:03:21,620 --> 00:03:23,930 Of course, these directories are hard coded 73 00:03:23,930 --> 00:03:26,980 and can be further attuned from the ansible.cfg. 74 00:03:26,980 --> 00:03:30,100 But if I use ansible-galaxy role install, 75 00:03:30,100 --> 00:03:33,993 by default it ends in home/ansible/.ansible/roles. 76 00:03:34,950 --> 00:03:37,066 So, you know what, let's go check out 77 00:03:37,066 --> 00:03:42,066 this /home/ansible/.ansible/roles 78 00:03:43,320 --> 00:03:46,570 to see what a role is really looking like. 79 00:03:46,570 --> 00:03:49,160 I think it's worth having some insight into the roles. 80 00:03:49,160 --> 00:03:51,940 So geerlinguy.nginx 81 00:03:51,940 --> 00:03:54,223 and we can see different directories. 82 00:03:55,210 --> 00:03:58,800 The role structure, the role contents is structured 83 00:03:58,800 --> 00:04:00,580 between different directories. 84 00:04:00,580 --> 00:04:02,490 And if you use the Linux three command 85 00:04:02,490 --> 00:04:04,240 that's an easy way to figure out 86 00:04:04,240 --> 00:04:07,290 how these roles are organized. 87 00:04:07,290 --> 00:04:09,320 To start with there's default and vars. 88 00:04:09,320 --> 00:04:11,610 These are about variables. 89 00:04:11,610 --> 00:04:14,030 So default is your default variables. 90 00:04:14,030 --> 00:04:16,132 They can be overwritten and vars is 91 00:04:16,132 --> 00:04:20,480 in many cases operating system, specific variables. 92 00:04:20,480 --> 00:04:22,020 Then we have the tasks. 93 00:04:22,020 --> 00:04:24,910 Every role has task, and the convention is 94 00:04:24,910 --> 00:04:28,590 that the main playbook in any directory is the name main. 95 00:04:28,590 --> 00:04:32,250 So we can see main.yml containing the main task. 96 00:04:32,250 --> 00:04:36,150 And then you can see that there are setup playbooks as well. 97 00:04:36,150 --> 00:04:37,840 I'll show them in a while. 98 00:04:37,840 --> 00:04:41,170 Apart from that, another important directory is the handlers 99 00:04:41,170 --> 00:04:43,990 and the less important directory is the templates. 100 00:04:43,990 --> 00:04:46,280 So if your task are triggering handlers 101 00:04:46,280 --> 00:04:47,710 they are right here. 102 00:04:47,710 --> 00:04:52,010 And if your tasks are using templates 103 00:04:52,010 --> 00:04:54,460 they should be stored in the templates directory. 104 00:04:55,370 --> 00:04:57,341 Now, if you really want to understand what's going on 105 00:04:57,341 --> 00:04:59,660 we should go to the tasks directory. 106 00:04:59,660 --> 00:05:03,400 And from there open main, main.yml. 107 00:05:03,400 --> 00:05:07,750 So here we go, the main.yml and what do we see? 108 00:05:07,750 --> 00:05:10,250 We see main playbook basically. 109 00:05:10,250 --> 00:05:11,610 Well, it's not a playbook, 110 00:05:11,610 --> 00:05:14,660 it's a collection of tasks, a collection of tasks 111 00:05:14,660 --> 00:05:16,910 that through the role is going to be included 112 00:05:16,910 --> 00:05:18,490 in your playbook. 113 00:05:18,490 --> 00:05:19,710 And what do we see? 114 00:05:19,710 --> 00:05:23,950 Well, we can see a very smart way to make it, 115 00:05:23,950 --> 00:05:26,100 to make it work on any distribution. 116 00:05:26,100 --> 00:05:27,530 So we have include vars, 117 00:05:27,530 --> 00:05:29,380 which is using ansible_OS_family.yml. 118 00:05:31,071 --> 00:05:34,250 Ansible_OS_family as you probably recognize 119 00:05:34,250 --> 00:05:36,068 is an Ansible fact. 120 00:05:36,068 --> 00:05:39,344 So it will include the Ansible fact for the specific 121 00:05:39,344 --> 00:05:42,430 distribution family that you are using. 122 00:05:42,430 --> 00:05:44,600 If we don't have nginx user, 123 00:05:44,600 --> 00:05:47,620 then we are going to use set fact. 124 00:05:47,620 --> 00:05:51,950 If ansible_OS_family equals RedHat 125 00:05:51,950 --> 00:05:54,270 or ansible_OS_family equals Rocky. 126 00:05:54,270 --> 00:05:58,100 Then we are going to install desks according to setup 127 00:05:58,100 --> 00:06:02,630 RedHat.yml, et cetera, et cetera, et cetera. 128 00:06:02,630 --> 00:06:04,556 And this is how a role is structured. 129 00:06:04,556 --> 00:06:08,140 Now let me show you just one example, 130 00:06:08,140 --> 00:06:11,480 the setup RedHat.yml. 131 00:06:11,480 --> 00:06:14,836 Wow, that's a big collection of tasks, of course not. 132 00:06:14,836 --> 00:06:18,802 Roles and tasks and everything else is organized 133 00:06:18,802 --> 00:06:21,270 in a way so that it is readable. 134 00:06:21,270 --> 00:06:23,560 And here you can see tasks that are specific 135 00:06:23,560 --> 00:06:25,380 for RedHat and that is all. 136 00:06:25,380 --> 00:06:26,890 This is what's gonna be done 137 00:06:26,890 --> 00:06:29,370 if you are on the RedHat family 138 00:06:29,370 --> 00:06:32,760 and if you're on the Ubuntu family, you get different tasks. 139 00:06:32,760 --> 00:06:35,780 And that's exactly what a role is all about. 140 00:06:35,780 --> 00:06:39,260 So while you are developing your own playbook, 141 00:06:39,260 --> 00:06:40,670 you may think for yourself, 142 00:06:40,670 --> 00:06:42,710 how am I going to handle this on Ubuntu? 143 00:06:42,710 --> 00:06:44,800 How am I going to handle this on RedHat? 144 00:06:44,800 --> 00:06:47,570 Instead of thinking about all of this for yourself 145 00:06:47,570 --> 00:06:49,800 you can also use a role instead 146 00:06:49,800 --> 00:06:52,293 and that is making your life a lot easier.