1 00:00:07,081 --> 00:00:09,660 - In this video, we are going to take a deep dive 2 00:00:09,660 --> 00:00:12,780 into collections and further understand. 3 00:00:12,780 --> 00:00:15,460 So, we have talked about collections before 4 00:00:15,460 --> 00:00:19,040 and you know about the basic usage of the collections. 5 00:00:19,040 --> 00:00:22,950 Now let's look a little bit further. 6 00:00:22,950 --> 00:00:26,530 So in the play header, you can use the collections keyword 7 00:00:26,530 --> 00:00:28,240 and the collection keyword 8 00:00:28,240 --> 00:00:30,850 takes a list of collections as its argument. 9 00:00:30,850 --> 00:00:32,400 And we have already discussed that 10 00:00:32,400 --> 00:00:35,810 that makes working with collections a bit easier. 11 00:00:35,810 --> 00:00:37,700 And after using the collections keyword, 12 00:00:37,700 --> 00:00:40,580 the collection itself can be addressed the old way. 13 00:00:40,580 --> 00:00:44,760 So you use selinux instead of ansible.posix.selinux. 14 00:00:45,950 --> 00:00:47,800 We have already seen how this is happening 15 00:00:47,800 --> 00:00:50,350 in enforce-selinux-simplified, 16 00:00:50,350 --> 00:00:54,490 and we have also seen how the old notation was used 17 00:00:54,490 --> 00:00:56,003 in the other example. 18 00:00:57,140 --> 00:00:58,530 Now let's try to understand 19 00:00:58,530 --> 00:01:01,800 the differences in the usage between collections 20 00:01:01,800 --> 00:01:03,840 and the things that you can do with them, 21 00:01:03,840 --> 00:01:06,830 for Ansible 2.9 as well as 2.10. 22 00:01:06,830 --> 00:01:08,490 So in Ansible 2.9, 23 00:01:08,490 --> 00:01:11,440 collections are not a default part of Ansible. 24 00:01:12,542 --> 00:01:13,430 You can use them, 25 00:01:13,430 --> 00:01:14,700 but if you want to use them, 26 00:01:14,700 --> 00:01:16,590 they must be installed. 27 00:01:16,590 --> 00:01:18,870 If you don't install them, they're not there. 28 00:01:18,870 --> 00:01:21,530 So, they're not working. 29 00:01:21,530 --> 00:01:23,550 And if you're installing multiple collections, 30 00:01:23,550 --> 00:01:25,933 you should consider using a requirements file. 31 00:01:26,788 --> 00:01:27,850 A requirements file 32 00:01:27,850 --> 00:01:29,540 allows you to list all of the collections 33 00:01:29,540 --> 00:01:30,660 so that you can install them 34 00:01:30,660 --> 00:01:33,400 before actually starting to use them. 35 00:01:33,400 --> 00:01:34,310 In order to do so, 36 00:01:34,310 --> 00:01:38,817 use ansible-galaxy collection install -r requirements.yml. 37 00:01:40,930 --> 00:01:41,840 I have an example. 38 00:01:41,840 --> 00:01:46,120 Let's check out the example on an Ansible 2.9 system. 39 00:01:46,120 --> 00:01:47,040 In this video, 40 00:01:47,040 --> 00:01:49,150 it's really important that with everything we do, 41 00:01:49,150 --> 00:01:52,030 you are fully conscient 42 00:01:52,030 --> 00:01:54,750 on the version of Ansible on which we are doing it. 43 00:01:54,750 --> 00:01:58,000 So Ansible 2.9, the requirements file. 44 00:01:58,000 --> 00:01:59,770 In Ansible 2.10 and later, 45 00:01:59,770 --> 00:02:01,780 collections are installed by default. 46 00:02:01,780 --> 00:02:03,363 So that makes it a lot easier. 47 00:02:04,810 --> 00:02:06,210 Now, the next thing you wanna do 48 00:02:06,210 --> 00:02:07,610 if you're working with collections, 49 00:02:07,610 --> 00:02:10,160 you want to be able to get collection information. 50 00:02:10,160 --> 00:02:12,650 Ansible 2.10 and later 51 00:02:12,650 --> 00:02:14,570 contain better collection integration 52 00:02:14,570 --> 00:02:18,571 and you can easily find collection names using ansible-doc; 53 00:02:18,571 --> 00:02:22,180 ansible-doc-l | grep aws. 54 00:02:22,180 --> 00:02:24,560 And Ansible 2.9 only shows collections 55 00:02:24,560 --> 00:02:26,570 that have been installed. 56 00:02:26,570 --> 00:02:28,620 So collections themselves are not listed, 57 00:02:29,697 --> 00:02:32,370 just do your ansible-doc-l. 58 00:02:32,370 --> 00:02:34,870 And collection documentation is available 59 00:02:34,870 --> 00:02:39,130 while addressing collection components directly. 60 00:02:39,130 --> 00:02:40,540 And finding collection names 61 00:02:40,540 --> 00:02:43,230 only goes through the installed files. 62 00:02:43,230 --> 00:02:45,230 That's not very convenient. 63 00:02:45,230 --> 00:02:47,690 Now, how can we list collection files? 64 00:02:47,690 --> 00:02:51,360 Well, you use ansible collection list 65 00:02:51,360 --> 00:02:54,010 on Ansible 2.10 and later, 66 00:02:54,010 --> 00:02:55,390 which will show a complete list 67 00:02:55,390 --> 00:02:57,740 of all the collections that are available. 68 00:02:57,740 --> 00:02:59,760 And to verify the contents of the collection, 69 00:02:59,760 --> 00:03:01,670 you can access its physical location 70 00:03:01,670 --> 00:03:04,790 or investigate the directory structure. 71 00:03:04,790 --> 00:03:08,243 Now let's go check out how this works in real life. 72 00:03:09,300 --> 00:03:13,440 Alright, let me demonstrate how to work with collections. 73 00:03:13,440 --> 00:03:15,620 I'm on 2.9 for your information, 74 00:03:15,620 --> 00:03:20,000 and I'm using ansible-galaxy collection 75 00:03:20,000 --> 00:03:20,860 install 76 00:03:22,620 --> 00:03:24,270 ansible.posix 77 00:03:26,570 --> 00:03:27,903 Oh, that's not nice. 78 00:03:29,560 --> 00:03:30,990 This is probably a bug. 79 00:03:30,990 --> 00:03:33,430 Well, if it's a bug, then we are not going to fix it. 80 00:03:33,430 --> 00:03:36,060 But fortunately, there's the Ansible Galaxy website. 81 00:03:36,060 --> 00:03:37,863 Look, here is the posix collection. 82 00:03:39,020 --> 00:03:40,900 On the Ansible Galaxy website, 83 00:03:40,900 --> 00:03:43,483 there is an option to download the tarball. 84 00:03:45,060 --> 00:03:47,110 So let's go behind the scenes 85 00:03:47,110 --> 00:03:49,250 and let me just download the tarball. 86 00:03:49,250 --> 00:03:50,313 Saving the file. 87 00:03:52,860 --> 00:03:55,640 And next, I can get back 88 00:03:55,640 --> 00:03:58,950 to my downloads directory 89 00:04:02,430 --> 00:04:04,370 There we can see the ansible-posix. 90 00:04:04,370 --> 00:04:07,610 So, I am going to do a tar -xvf. 91 00:04:09,530 --> 00:04:13,080 You know what, let me avoid doing the Linux visitry 92 00:04:13,080 --> 00:04:14,910 and let me do an ls 93 00:04:14,910 --> 00:04:15,864 on 94 00:04:15,864 --> 00:04:16,697 .ansible. 95 00:04:17,660 --> 00:04:19,230 So we have collections. 96 00:04:19,230 --> 00:04:21,620 Just verifying the contents in collections. 97 00:04:21,620 --> 00:04:23,230 We have ansible_collections 98 00:04:23,230 --> 00:04:25,800 and in there, there is nothing. 99 00:04:25,800 --> 00:04:29,863 So I'm going to use this path that we just discovered. 100 00:04:32,650 --> 00:04:36,000 But this time I'm copying the ansible-posix 101 00:04:36,000 --> 00:04:40,643 till the .ansible/collections/ansible_collections. 102 00:04:41,700 --> 00:04:45,840 And I am going to make that my current directory as well. 103 00:04:45,840 --> 00:04:49,930 So, .ansible/collections/ansible_collections. 104 00:04:49,930 --> 00:04:50,763 There we go, 105 00:04:50,763 --> 00:04:52,943 and there I'm running tar xvf. 106 00:04:55,150 --> 00:04:56,820 So, what do we see? 107 00:04:56,820 --> 00:04:58,030 We see that it's creating 108 00:04:58,030 --> 00:05:01,510 the entire sub directory structure in here. 109 00:05:01,510 --> 00:05:03,603 That's not exactly what I wanted. 110 00:05:04,650 --> 00:05:09,170 I wanted this sub directory structure to be somewhere else. 111 00:05:09,170 --> 00:05:11,260 So, what does that mean? 112 00:05:11,260 --> 00:05:16,260 That means that I should have created, mkdir ansible. 113 00:05:16,550 --> 00:05:18,890 A directory with the name, ansible 114 00:05:18,890 --> 00:05:23,380 and in this ansible, mkdir ansible/posix. 115 00:05:23,380 --> 00:05:28,380 And then I can move everything to ansible/posix, 116 00:05:28,950 --> 00:05:32,270 and then I have my directory structure. 117 00:05:32,270 --> 00:05:34,090 So, in ansible/posix, 118 00:05:34,090 --> 00:05:37,570 we see the structure of the posix collection, 119 00:05:37,570 --> 00:05:40,563 which is everything that you can see right here. 120 00:05:42,260 --> 00:05:46,000 Wow, is that an easy way to see what is in the collection? 121 00:05:46,000 --> 00:05:47,373 No, of course not. 122 00:05:48,470 --> 00:05:51,790 But it's probably the only way in 2.9. 123 00:05:51,790 --> 00:05:56,460 2.9 was never organized to make it really accessible. 124 00:05:56,460 --> 00:05:58,370 Look, this is the best way to get in there. 125 00:05:58,370 --> 00:05:59,430 In plugins, 126 00:05:59,430 --> 00:06:02,090 you will find all the different plugins, 127 00:06:02,090 --> 00:06:04,343 and modules happen to be plugins as well. 128 00:06:05,460 --> 00:06:06,580 Amongst these plugins, 129 00:06:06,580 --> 00:06:11,580 we have acl and at and firewalld and whatever 130 00:06:11,740 --> 00:06:14,150 This is how you can figure out what it is. 131 00:06:14,150 --> 00:06:15,350 It brings it directly 132 00:06:15,350 --> 00:06:17,230 to the python scripts. 133 00:06:17,230 --> 00:06:18,640 Yes, I know. 134 00:06:18,640 --> 00:06:22,130 But it's probably the best way that you can get around. 135 00:06:22,130 --> 00:06:23,650 Based on this information, 136 00:06:23,650 --> 00:06:25,210 based on the name of the collection, 137 00:06:25,210 --> 00:06:28,910 you can next use your ansible-doc command. 138 00:06:28,910 --> 00:06:31,490 So, if I'm using ansible-doc 139 00:06:32,424 --> 00:06:33,565 on ansible 140 00:06:33,565 --> 00:06:34,398 .posix 141 00:06:35,633 --> 00:06:37,250 .selinux, 142 00:06:37,250 --> 00:06:39,650 then, I am getting access 143 00:06:39,650 --> 00:06:42,273 to the actual collection information. 144 00:06:44,100 --> 00:06:46,780 And that is how it works on 2.9. 145 00:06:46,780 --> 00:06:48,420 Is that the best way around? 146 00:06:48,420 --> 00:06:49,500 No, 147 00:06:49,500 --> 00:06:51,880 but I really think that in Ansible 2.9 148 00:06:51,880 --> 00:06:54,640 you should consider collections to be in preview, 149 00:06:54,640 --> 00:06:55,473 not in final. 150 00:06:55,473 --> 00:06:57,770 If you wanna use collections, 151 00:06:57,770 --> 00:07:01,863 make sure that you are on Ansible 2.10 or later.