1 00:00:06,700 --> 00:00:08,370 - A completely different category 2 00:00:08,370 --> 00:00:10,920 is Using Plugin-Based Inventory. 3 00:00:10,920 --> 00:00:12,780 Let's go check it out. 4 00:00:12,780 --> 00:00:15,770 So inventory can be written in any format, 5 00:00:15,770 --> 00:00:17,100 which is the old format. 6 00:00:17,100 --> 00:00:19,630 It's still a very common format. 7 00:00:19,630 --> 00:00:22,460 The new alternative is the YAML format. 8 00:00:22,460 --> 00:00:26,669 An inventory since Ansible 2.4 is managed through plugins. 9 00:00:26,669 --> 00:00:29,610 The ansibe.cfg lists which plugins are used 10 00:00:29,610 --> 00:00:31,060 in the inventory section, 11 00:00:31,060 --> 00:00:34,580 if you're using inventory based plugins at least. 12 00:00:34,580 --> 00:00:36,280 Notice that you may have never seen it 13 00:00:36,280 --> 00:00:40,210 because inventory based plugins are still not very common 14 00:00:40,210 --> 00:00:43,680 because the ini format is so simple and so convenient 15 00:00:43,680 --> 00:00:45,410 for many environments. 16 00:00:45,410 --> 00:00:47,530 Both using plugins allows Ansible 17 00:00:47,530 --> 00:00:49,970 to support new inventory format 18 00:00:49,970 --> 00:00:51,860 by just providing a new plugin. 19 00:00:51,860 --> 00:00:54,753 So it makes it a lot more flexible. 20 00:00:55,680 --> 00:00:58,050 We have the ini plugin, which is a default, 21 00:00:58,050 --> 00:01:00,950 which supports old cell inventory format. 22 00:01:00,950 --> 00:01:02,420 We have the script plugin 23 00:01:02,420 --> 00:01:05,150 which supports old cell dynamic inventory. 24 00:01:05,150 --> 00:01:08,910 Dynamic inventory, where in the in the old days 25 00:01:08,910 --> 00:01:11,730 you would fetch dynamic inventory scripts 26 00:01:11,730 --> 00:01:14,470 from the community websites. 27 00:01:14,470 --> 00:01:17,930 There's a YAML plugin, which supports new cell YAML format. 28 00:01:17,930 --> 00:01:22,090 And these are the options and you need to enable the plugins 29 00:01:22,090 --> 00:01:25,750 that you want to support in your ansible.cfg. 30 00:01:25,750 --> 00:01:27,130 And notice that using plugins 31 00:01:27,130 --> 00:01:29,790 is preferred over dynamic inventory scripts. 32 00:01:29,790 --> 00:01:33,710 Before continuing let's check out the ansible.cfg 33 00:01:33,710 --> 00:01:38,710 for the current plugins that are used. 34 00:01:38,810 --> 00:01:39,930 So here we are. 35 00:01:39,930 --> 00:01:41,650 I'm going to show you sudu vim 36 00:01:41,650 --> 00:01:43,937 on etc/ansible/ansible.cfg. 37 00:01:46,930 --> 00:01:49,683 And we are going to look for inventory. 38 00:01:51,070 --> 00:01:52,580 This is not the one, oops. 39 00:01:52,580 --> 00:01:55,180 I want the next one and the next one. 40 00:01:55,180 --> 00:01:58,090 This is what we are looking for, inventory plugins. 41 00:01:58,090 --> 00:02:02,250 Inventory plugins is use the share ansible plugins 42 00:02:02,250 --> 00:02:04,893 inventory, or that's just the plugins path. 43 00:02:05,860 --> 00:02:08,423 What else do we have? 44 00:02:11,190 --> 00:02:12,023 There we go. 45 00:02:12,023 --> 00:02:15,540 We can see the inventory section right here. 46 00:02:15,540 --> 00:02:17,620 This is what it should be looking like. 47 00:02:17,620 --> 00:02:22,050 And you can see that the enable inventory plugins, 48 00:02:22,050 --> 00:02:24,400 the default is set to host_list, script, auto, yaml, 49 00:02:24,400 --> 00:02:25,707 ini and toml 50 00:02:26,680 --> 00:02:28,990 and you can see these settings right here. 51 00:02:28,990 --> 00:02:31,000 Do remember if you want to use plugins 52 00:02:31,000 --> 00:02:35,680 you need this enabled plugins in your local ansible.cfd. 53 00:02:35,680 --> 00:02:38,810 I'm just showing it here to show you what is hard coded 54 00:02:38,810 --> 00:02:39,833 as a default. 55 00:02:40,700 --> 00:02:42,940 And as it is coded as a default, 56 00:02:42,940 --> 00:02:44,610 you don't really have to specify it 57 00:02:44,610 --> 00:02:47,090 but if you want to do something specific 58 00:02:47,090 --> 00:02:50,030 apart from the common inventory plugin, 59 00:02:50,030 --> 00:02:53,650 you will need to open an inventory section 60 00:02:53,650 --> 00:02:57,960 in your ansible.cfg to provide that. 61 00:02:57,960 --> 00:03:00,453 Good, let's check out some of these formats. 62 00:03:01,660 --> 00:03:06,130 So here we have an example where the ini plugin is used. 63 00:03:06,130 --> 00:03:09,090 This is what we've seen throughout in this course. 64 00:03:09,090 --> 00:03:12,130 So you have the group name, below the group name 65 00:03:12,130 --> 00:03:13,490 which is between square brackets. 66 00:03:13,490 --> 00:03:15,840 You have the list of the hosts 67 00:03:15,840 --> 00:03:17,780 and you can specify Nestle groups as well 68 00:03:17,780 --> 00:03:20,220 which is what we see in [Allserver:children] 69 00:03:20,220 --> 00:03:24,490 where web servers and dbservers are a member of all servers. 70 00:03:24,490 --> 00:03:26,840 Right, if you would be using the yaml plugin 71 00:03:26,840 --> 00:03:28,920 then this is what it looks like. 72 00:03:28,920 --> 00:03:33,450 So the parent level group is all servers, 73 00:03:33,450 --> 00:03:35,330 children is defining the children 74 00:03:35,330 --> 00:03:38,050 and we have webservers, dbservice and so on. 75 00:03:38,050 --> 00:03:40,810 You can also put your variables, you can put everything 76 00:03:40,810 --> 00:03:43,350 in this yaml based plugin. 77 00:03:43,350 --> 00:03:45,780 Hey before you are going to work with these inventories 78 00:03:45,780 --> 00:03:50,570 I do need to show you the documentation behind the plugins. 79 00:03:50,570 --> 00:03:55,453 So if you use ansible.docs -t inventory -l, 80 00:03:57,100 --> 00:03:58,160 we get a list, 81 00:03:58,160 --> 00:04:00,830 oh, ansible-doc of course. 82 00:04:00,830 --> 00:04:03,630 We get a list of all the different inventory types. 83 00:04:03,630 --> 00:04:05,630 And did you know that every inventory type 84 00:04:05,630 --> 00:04:08,530 has its own documentation? 85 00:04:08,530 --> 00:04:11,220 So we use ini and here we can see the example 86 00:04:11,220 --> 00:04:13,320 for ini based inventory, 87 00:04:13,320 --> 00:04:15,460 including variables and everything 88 00:04:15,460 --> 00:04:17,860 that you might want to do. 89 00:04:17,860 --> 00:04:20,210 We use YAML and there you can see 90 00:04:20,210 --> 00:04:23,650 what it looks like all the way to the bottom 91 00:04:23,650 --> 00:04:25,700 for the YAML based inventory. 92 00:04:25,700 --> 00:04:27,610 You are going to appreciate this example 93 00:04:27,610 --> 00:04:31,460 because it's telling you how exactly to compose it 94 00:04:31,460 --> 00:04:34,580 including the variables that you might be including 95 00:04:34,580 --> 00:04:36,710 at a specific level. 96 00:04:36,710 --> 00:04:38,330 And what else do we have? 97 00:04:38,330 --> 00:04:42,290 Well, let's get back to the list and let's have a look 98 00:04:42,290 --> 00:04:46,430 at one of the sources that is not so common. 99 00:04:46,430 --> 00:04:48,220 Kubernetes for instance. 100 00:04:48,220 --> 00:04:51,500 So -t inventory Kubernetes 101 00:04:51,500 --> 00:04:53,900 and there we go. 102 00:04:53,900 --> 00:04:57,260 We see the example that can be used 103 00:04:57,260 --> 00:04:59,460 for Kubernetes based inventory. 104 00:04:59,460 --> 00:05:02,600 Ooh, this is all about Kubernetes authentication 105 00:05:02,600 --> 00:05:06,960 which means you are going to hand it over to Kubernetes. 106 00:05:06,960 --> 00:05:08,550 That is what corresponds 107 00:05:08,550 --> 00:05:10,860 to the old scripting style inventory. 108 00:05:10,860 --> 00:05:13,500 And you can use some references 109 00:05:13,500 --> 00:05:16,220 to other scripting style inventories as well. 110 00:05:16,220 --> 00:05:18,810 Like Azure Resource Manager, 111 00:05:18,810 --> 00:05:21,630 like foreman, like Docker machine, 112 00:05:21,630 --> 00:05:24,150 like just a host list, 113 00:05:24,150 --> 00:05:29,140 like openshift or openstack or whatever. 114 00:05:29,140 --> 00:05:31,930 Now we should focus at this point on ini 115 00:05:31,930 --> 00:05:34,400 in YAML based inventories. 116 00:05:34,400 --> 00:05:37,260 We will see these specific inventories later. 117 00:05:37,260 --> 00:05:38,730 So let's get back to the slides 118 00:05:38,730 --> 00:05:41,623 and talk about inventory script conversion. 119 00:05:42,468 --> 00:05:45,040 So ini based inventory is the standard, 120 00:05:45,040 --> 00:05:48,520 but you might want to move over to YAML based inventory 121 00:05:48,520 --> 00:05:50,940 which seems to be the future anyway. 122 00:05:50,940 --> 00:05:54,370 You can easily convert using the Ansible inventory command. 123 00:05:54,370 --> 00:05:55,285 So for instance, use 124 00:05:55,285 --> 00:06:00,285 ansible-inventory --yaml -i inventory --list 125 00:06:00,374 --> 00:06:02,740 - -output inventory.yaml. 126 00:06:03,950 --> 00:06:06,960 So let me just demonstrate how to do that 127 00:06:06,960 --> 00:06:10,690 based on the inventory that we are going to use later 128 00:06:10,690 --> 00:06:13,310 in the windows, 129 00:06:13,310 --> 00:06:15,530 in the lesson about windows. 130 00:06:15,530 --> 00:06:20,270 But important, when ini-based inventory contains variables, 131 00:06:20,270 --> 00:06:22,640 they maybe messed up after conversion 132 00:06:22,640 --> 00:06:24,610 and you should always check 133 00:06:24,610 --> 00:06:28,720 because you might have to manually fix it. 134 00:06:28,720 --> 00:06:31,280 Of course you can easily avoid it 135 00:06:31,280 --> 00:06:34,910 by using the approach of putting your variables 136 00:06:34,910 --> 00:06:36,630 in group_vars/ and host_vars/. 137 00:06:36,630 --> 00:06:39,920 Hey, I'm mentioning the recommended approach here. 138 00:06:39,920 --> 00:06:44,340 I think it's time to think about the recommendation 139 00:06:44,340 --> 00:06:46,360 where to put your inventory variables 140 00:06:46,360 --> 00:06:49,560 because how are you going to use group_vars/ and host_vars/ 141 00:06:49,560 --> 00:06:51,510 if you're using Ansible tower, 142 00:06:51,510 --> 00:06:54,250 then you might need to be to get back 143 00:06:54,250 --> 00:06:57,340 to inventory based variables anyway. 144 00:06:57,340 --> 00:06:59,470 So that is really something to think about 145 00:06:59,470 --> 00:07:02,110 but more about tower in the next lesson. 146 00:07:02,110 --> 00:07:04,713 For now, let's check out this conversion. 147 00:07:06,170 --> 00:07:09,310 So in order to show you the conversion, 148 00:07:09,310 --> 00:07:11,563 let's go to the windows directory. 149 00:07:12,610 --> 00:07:15,130 That's a project directory that we will discuss 150 00:07:15,130 --> 00:07:16,250 in more detail later. 151 00:07:16,250 --> 00:07:18,570 For now I only want to have a look at 152 00:07:18,570 --> 00:07:20,730 the inventory where you can see 153 00:07:20,730 --> 00:07:22,650 that there are three groups 154 00:07:22,650 --> 00:07:25,220 and there are variables for the group win. 155 00:07:25,220 --> 00:07:28,120 And that is what makes this an interesting one. 156 00:07:28,120 --> 00:07:29,720 So if I wanna do the conversion, 157 00:07:29,720 --> 00:07:34,720 then ansible-inventory --yaml to specify the format 158 00:07:34,979 --> 00:07:39,979 - i inventory --list --output winventory.yaml. 159 00:07:43,930 --> 00:07:46,840 So that is creating winventory.yaml. 160 00:07:46,840 --> 00:07:49,270 And there we go, we can see that the conversion 161 00:07:49,270 --> 00:07:51,640 actually has worked pretty well. 162 00:07:51,640 --> 00:07:55,230 So it works with the implicit host group all, 163 00:07:55,230 --> 00:07:58,160 we don't see that anywhere in the original inventory 164 00:07:58,160 --> 00:08:00,260 but it's an implicit host group so it's there, 165 00:08:00,260 --> 00:08:02,080 which is children, 166 00:08:02,080 --> 00:08:04,330 children file and lamp and ungrouped. 167 00:08:04,330 --> 00:08:08,700 Ungrouped are the hosts that are a member of no group at all 168 00:08:08,700 --> 00:08:10,303 that doesn't apply in this case. 169 00:08:11,175 --> 00:08:12,190 Then we have win. 170 00:08:12,190 --> 00:08:13,920 And in win we have the hosts 171 00:08:13,920 --> 00:08:16,500 and the host windows.example.com. 172 00:08:16,500 --> 00:08:18,400 And here we have all the variables that apply 173 00:08:18,400 --> 00:08:20,500 to that specific host. 174 00:08:20,500 --> 00:08:23,960 That is looking pretty good. 175 00:08:23,960 --> 00:08:25,930 Let's get back to the slide one more time 176 00:08:25,930 --> 00:08:28,910 because we need to talk about inventory sources 177 00:08:28,910 --> 00:08:30,730 a little bit more to make sure 178 00:08:30,730 --> 00:08:32,653 you fully understand what's going on. 179 00:08:34,350 --> 00:08:37,090 So inventory is just discussed as plugin-based. 180 00:08:37,090 --> 00:08:41,040 You can use ansible-doc -t inventory -l 181 00:08:41,040 --> 00:08:42,240 for a complete list. 182 00:08:42,240 --> 00:08:43,393 We've already seen it. 183 00:08:44,300 --> 00:08:47,780 The inventory enabled plugins option in ansible.cfg, 184 00:08:47,780 --> 00:08:52,080 as how you determine what plugins needs to be enabled. 185 00:08:52,080 --> 00:08:55,140 So here we can see an example where host_list 186 00:08:55,140 --> 00:08:58,230 and script and auto and yaml and ini and toml, 187 00:08:58,230 --> 00:09:00,740 all of them are enabled. 188 00:09:00,740 --> 00:09:03,080 And if you are using legacy inventory scripts, 189 00:09:03,080 --> 00:09:05,800 they are still supported because of the script plugin. 190 00:09:05,800 --> 00:09:08,170 That's a nice backward compatibility 191 00:09:08,170 --> 00:09:10,070 that is implemented and that goes 192 00:09:10,070 --> 00:09:12,230 without any additional work. 193 00:09:12,230 --> 00:09:15,580 And if you wanna use additional inventory plugins, 194 00:09:15,580 --> 00:09:17,450 you need to add them in product based 195 00:09:17,450 --> 00:09:20,170 or generic ansible.cfg. 196 00:09:20,170 --> 00:09:23,500 Well, let's make sure you understand this. 197 00:09:23,500 --> 00:09:26,930 This product based or generic ansible.cfg, 198 00:09:26,930 --> 00:09:28,180 they are not merged. 199 00:09:28,180 --> 00:09:32,230 So either in product based where no generic ansible.cfg 200 00:09:32,230 --> 00:09:34,860 is considered or in generic ansible.cfg, 201 00:09:34,860 --> 00:09:37,610 if you don't have a project based inventory. 202 00:09:37,610 --> 00:09:39,530 Use a fully qualified collection name 203 00:09:39,530 --> 00:09:41,643 if the plugin is a part of a collection. 204 00:09:42,580 --> 00:09:45,080 And that's all you need to know about inventories.