1 00:00:07,011 --> 00:00:10,230 - In this video, you will learn about lookup plugins. 2 00:00:10,230 --> 00:00:12,440 So lookup plugins are created to use data 3 00:00:12,440 --> 00:00:16,220 from external sources like files and environment variables. 4 00:00:16,220 --> 00:00:17,170 It's pretty cool. 5 00:00:17,170 --> 00:00:20,260 It allows you to fetch data from outside Ansible 6 00:00:20,260 --> 00:00:21,590 no matter where. 7 00:00:21,590 --> 00:00:23,950 And there's quite a few of them. 8 00:00:23,950 --> 00:00:28,100 Use ansible-doc -t lookup -l for a complete list. 9 00:00:28,100 --> 00:00:31,660 And once you found a plugin that looks interesting, 10 00:00:31,660 --> 00:00:33,640 aou can use ansible-doc -t lookup 11 00:00:33,640 --> 00:00:37,260 followed by the plugin-name for its documentation. 12 00:00:37,260 --> 00:00:38,660 I am going to pick out 13 00:00:38,660 --> 00:00:40,660 some of the most interesting lookup plugins 14 00:00:40,660 --> 00:00:42,300 and talk about them. 15 00:00:42,300 --> 00:00:45,403 Just be aware that there is more where this is coming from. 16 00:00:46,240 --> 00:00:48,340 Right, so lookup plugins can be called 17 00:00:48,340 --> 00:00:49,720 in two different ways. 18 00:00:49,720 --> 00:00:52,900 You can call them using lookup or query 19 00:00:52,900 --> 00:00:56,550 where lookup is used to read file content into a variable 20 00:00:56,550 --> 00:01:00,030 and query is used to read the file content into a variable 21 00:01:00,030 --> 00:01:02,993 where the content is stored as a list. 22 00:01:04,370 --> 00:01:05,560 Now here we have an overview 23 00:01:05,560 --> 00:01:08,090 of some of the useful lookup plugins. 24 00:01:08,090 --> 00:01:12,540 File, which reads content of files from the control node. 25 00:01:12,540 --> 00:01:15,490 There is template which processes the contents 26 00:01:15,490 --> 00:01:16,660 of a template. 27 00:01:16,660 --> 00:01:21,660 You would use it as lookup template, mytemplate.j2. 28 00:01:22,550 --> 00:01:25,810 There is env which allows you to use an environment variable 29 00:01:25,810 --> 00:01:28,090 that is used on the control node. 30 00:01:28,090 --> 00:01:30,720 So here we see lookup env DB_password. 31 00:01:30,720 --> 00:01:32,990 So that is going to the control node 32 00:01:32,990 --> 00:01:34,060 and on the control node 33 00:01:34,060 --> 00:01:36,770 it will check in the current shell environment 34 00:01:36,770 --> 00:01:39,313 for the value of the variable DB_password. 35 00:01:40,450 --> 00:01:44,730 There is url, which allows you to get contents from a URL. 36 00:01:44,730 --> 00:01:49,730 So lookup url on http://example.com, for instance. 37 00:01:49,730 --> 00:01:52,480 This is not something that you would use a lot 38 00:01:52,480 --> 00:01:55,120 to discover what is offered by a website 39 00:01:55,120 --> 00:01:59,350 but the url lookup plugin is very useful 40 00:01:59,350 --> 00:02:02,320 to work with API endpoints. 41 00:02:02,320 --> 00:02:04,860 There is pipe which returns the output of a command 42 00:02:04,860 --> 00:02:07,410 that is executed on the control host 43 00:02:07,410 --> 00:02:09,980 like query, pipe, ls files, 44 00:02:09,980 --> 00:02:13,520 where ls files is the actual command. 45 00:02:13,520 --> 00:02:17,100 There is k8s which is for Kubernetes. 46 00:02:17,100 --> 00:02:20,360 It allows you to fetch a Kubernetes object. 47 00:02:20,360 --> 00:02:22,930 As in lookup Kubernetes, kind is deployment, 48 00:02:22,930 --> 00:02:24,220 namespace is default. 49 00:02:24,220 --> 00:02:26,760 It will produce a list of all the deployments 50 00:02:26,760 --> 00:02:28,723 in the default Kubernetes namespace. 51 00:02:30,130 --> 00:02:34,360 Now let's have a closer look at the file lookup plugin. 52 00:02:34,360 --> 00:02:37,593 So in the example line that you can see right here, 53 00:02:38,470 --> 00:02:41,600 the file lookup plugin is used to read the contents 54 00:02:41,600 --> 00:02:46,470 of the file /etc/passwd into the variable myusers. 55 00:02:46,470 --> 00:02:48,377 So vars is setting a variable 56 00:02:48,377 --> 00:02:51,190 and this variable is dynamically created 57 00:02:51,190 --> 00:02:56,190 based on the result of lookup file /etc/passwd. 58 00:02:56,260 --> 00:02:58,040 Also convenient, the file plugin 59 00:02:58,040 --> 00:03:01,060 can take multiple file names as its argument 60 00:03:01,060 --> 00:03:05,970 where each file contents will be separated by a comma. 61 00:03:05,970 --> 00:03:08,450 And you can use query, if you want to store 62 00:03:08,450 --> 00:03:10,700 the file contents in a list. 63 00:03:10,700 --> 00:03:15,040 So vars, myfiles, query file, /etc/hosts, /etc/motd 64 00:03:15,040 --> 00:03:17,343 is producing the list with the file contents. 65 00:03:18,510 --> 00:03:20,110 Let's have a look at an example. 66 00:03:21,750 --> 00:03:26,540 So the example file is readkeys and what are we doing? 67 00:03:26,540 --> 00:03:31,540 We are working and let's just do this on host ansible2. 68 00:03:33,770 --> 00:03:37,160 We are working on ansible2 on the variables users, 69 00:03:37,160 --> 00:03:39,170 student and ansible. 70 00:03:39,170 --> 00:03:43,890 So the task is create users, name item, loop users 71 00:03:43,890 --> 00:03:46,420 and then it's adding authorized keys. 72 00:03:46,420 --> 00:03:48,730 Now, how is that working? 73 00:03:48,730 --> 00:03:52,260 Well, it is working on user item 74 00:03:52,260 --> 00:03:54,623 and the key is using lookup, 75 00:03:57,360 --> 00:03:58,733 file lookup plugin. 76 00:04:00,179 --> 00:04:03,010 Then we can see item plus, item plus is the username 77 00:04:03,010 --> 00:04:05,667 and this username is id_rsa.pub. 78 00:04:07,330 --> 00:04:12,330 So it's looking up id on _rsa.pub in the username. 79 00:04:13,630 --> 00:04:15,420 Hmm. Is it going to work as such? 80 00:04:15,420 --> 00:04:20,150 Probably not because this item needs to be there 81 00:04:20,150 --> 00:04:23,633 but it might be helpful to understand why it is not working. 82 00:04:26,606 --> 00:04:30,487 So ansible-playbook on readkeys.yaml is doing what? 83 00:04:33,360 --> 00:04:34,990 So what is happening? 84 00:04:34,990 --> 00:04:38,850 Well, as authorized key, you can see it's complaining. 85 00:04:38,850 --> 00:04:42,740 Unable to find student id_rsa.pub. 86 00:04:42,740 --> 00:04:46,773 I think this is telling all about what's going wrong. 87 00:04:47,688 --> 00:04:50,750 It's using this item in the file lookup plugin. 88 00:04:50,750 --> 00:04:54,200 The item is student and this expects local directory 89 00:04:54,200 --> 00:04:58,847 with the name student to exist with the id_rsa.pub. 90 00:05:00,480 --> 00:05:03,900 So I can really easily fix it. 91 00:05:03,900 --> 00:05:07,990 If you use mkdir ansible student and I'm using copy. 92 00:05:07,990 --> 00:05:10,440 It's not elegant but this time I just want to show you 93 00:05:10,440 --> 00:05:14,200 a quick solution of how we can get there. 94 00:05:14,200 --> 00:05:18,933 So I'm mainly going to create these pub files. 95 00:05:21,090 --> 00:05:25,330 So let's copy it to ansible as well as to student 96 00:05:26,170 --> 00:05:29,740 and then we can try to do it again. 97 00:05:29,740 --> 00:05:33,000 Obviously, if you wanna go for the nice solution 98 00:05:33,000 --> 00:05:34,890 then you are going to use Ansible 99 00:05:34,890 --> 00:05:38,630 to create that local user directory 100 00:05:38,630 --> 00:05:41,230 in the current project directory or somewhere else 101 00:05:41,230 --> 00:05:44,610 and you are going to generate an SSH public private key pair 102 00:05:44,610 --> 00:05:45,660 as well. 103 00:05:45,660 --> 00:05:47,240 That will be an interesting challenge 104 00:05:47,240 --> 00:05:49,810 but honestly, we shouldn't care about that 105 00:05:49,810 --> 00:05:52,760 because this is really about how we can use 106 00:05:52,760 --> 00:05:54,610 the file lookup plugin. 107 00:05:54,610 --> 00:05:59,220 This is just one example of using a lookup plugin. 108 00:05:59,220 --> 00:06:02,550 There are so many more examples that can be imagined 109 00:06:02,550 --> 00:06:04,660 but I hope this is giving you inspiration 110 00:06:04,660 --> 00:06:07,093 to start using lookup plugins.