1 00:00:06,860 --> 00:00:08,110 - So, the next lookup plugin 2 00:00:08,110 --> 00:00:11,017 that we are going to explore is fileglob. 3 00:00:11,017 --> 00:00:12,510 What is this about? 4 00:00:12,510 --> 00:00:14,970 Well, the fileglob lookup plugin can be used 5 00:00:14,970 --> 00:00:17,030 to iterate over a list of files according 6 00:00:17,030 --> 00:00:18,420 to a globbing pattern. 7 00:00:18,420 --> 00:00:20,780 And that's indeed very convenient. 8 00:00:20,780 --> 00:00:22,040 If you use fileglob, 9 00:00:22,040 --> 00:00:25,460 on a star between single quotes, 10 00:00:25,460 --> 00:00:26,810 then it'll provide a string 11 00:00:26,810 --> 00:00:30,020 of comma-separated values for all files in the output. 12 00:00:30,020 --> 00:00:32,580 And that allows you to work with wildcards. 13 00:00:32,580 --> 00:00:35,100 I mean, have you ever tried to use the copy module 14 00:00:35,100 --> 00:00:37,730 to copy files according to a wildcard pattern? 15 00:00:37,730 --> 00:00:39,440 That's not really going to work. 16 00:00:39,440 --> 00:00:43,840 And that is why this fileglob lookup plugin can be useful. 17 00:00:43,840 --> 00:00:45,220 Now, to force the lookup plugin 18 00:00:45,220 --> 00:00:46,820 to return a list of values, 19 00:00:46,820 --> 00:00:49,920 instead of a string of comma-separated values, 20 00:00:49,920 --> 00:00:52,430 use the query keyword instead of lookup. 21 00:00:52,430 --> 00:00:54,830 Do remember most lookup plugins 22 00:00:54,830 --> 00:00:58,090 can be used using lookup or query. 23 00:00:58,090 --> 00:01:01,720 So, query fileglob will do what you want to do. 24 00:01:01,720 --> 00:01:04,130 And in the output, you will see that it is a list 25 00:01:04,130 --> 00:01:08,640 because the data is encapsulated by brackets. 26 00:01:08,640 --> 00:01:11,350 Let's have a look at a couple of examples. 27 00:01:11,350 --> 00:01:14,733 So, the first example is fileglob string. 28 00:01:15,690 --> 00:01:16,740 And in fileglob string, 29 00:01:16,740 --> 00:01:20,360 you can see it's quite a simple example, 30 00:01:20,360 --> 00:01:22,411 accessing fileglob using debug 31 00:01:22,411 --> 00:01:26,390 and the message is using lookup fileglob 32 00:01:26,390 --> 00:01:29,773 on dot star in the current user home directory. 33 00:01:30,820 --> 00:01:34,170 Notice that the indentation is weird, 34 00:01:34,170 --> 00:01:38,300 but there is nothing against using 10 spaces 35 00:01:38,300 --> 00:01:39,890 or even more in the indentation 36 00:01:39,890 --> 00:01:41,790 as long as you're consistent. 37 00:01:41,790 --> 00:01:44,150 So, I feel this urge to fix it 38 00:01:44,150 --> 00:01:46,500 but really that's not necessary. 39 00:01:46,500 --> 00:01:48,380 Let me show you Ansible playbook 40 00:01:48,380 --> 00:01:50,500 on fileglobstring.yaml, 41 00:01:51,740 --> 00:01:53,730 and there we go. 42 00:01:53,730 --> 00:01:54,910 We have a string value. 43 00:01:54,910 --> 00:01:57,700 So, this is a comma-separated string 44 00:01:57,700 --> 00:02:00,850 with all files in just one string. 45 00:02:00,850 --> 00:02:03,420 That might not be what you want to do 46 00:02:03,420 --> 00:02:05,990 because it's a little bit difficult to work 47 00:02:05,990 --> 00:02:07,430 with values in that way. 48 00:02:07,430 --> 00:02:10,360 And that is why we also have fileglob list. 49 00:02:10,360 --> 00:02:12,480 And you can see it's completely the same 50 00:02:12,480 --> 00:02:16,950 but this time we are using query instead of lookup. 51 00:02:16,950 --> 00:02:20,153 And if you run it filegloblist.yaml, 52 00:02:22,620 --> 00:02:24,913 then this is what we get. 53 00:02:26,031 --> 00:02:26,981 And as you can see, 54 00:02:30,260 --> 00:02:32,773 each file is listed independently. 55 00:02:33,750 --> 00:02:34,583 Why is that? 56 00:02:34,583 --> 00:02:36,870 Because we are looping over them after all 57 00:02:36,870 --> 00:02:39,620 and the loop this time is working successfully 58 00:02:39,620 --> 00:02:41,080 because it's a list. 59 00:02:41,080 --> 00:02:44,273 And you need lists, if you want to loop. 60 00:02:45,400 --> 00:02:48,740 - We have just looked at the fileglob basics. 61 00:02:48,740 --> 00:02:50,110 Let's dig a little bit deeper 62 00:02:50,110 --> 00:02:53,310 and talk about using fileglob with wildcards. 63 00:02:53,310 --> 00:02:55,200 So, the thing is the Ansible copy module 64 00:02:55,200 --> 00:02:57,150 does not support wildcards. 65 00:02:57,150 --> 00:02:58,950 That is so inconvenient. 66 00:02:58,950 --> 00:03:01,660 And if you want to fix that, you can use a fileglob filter 67 00:03:01,660 --> 00:03:06,223 in the loop to present this wildcard functionality. 68 00:03:07,350 --> 00:03:09,870 Do keep in mind though, that this is not recommended 69 00:03:09,870 --> 00:03:11,680 for copying many files as for angle, 70 00:03:11,680 --> 00:03:15,220 every single file the copy model will be called. 71 00:03:15,220 --> 00:03:16,640 So, it'll be a lot of work 72 00:03:16,640 --> 00:03:20,150 but it'll work and I will show you 73 00:03:20,150 --> 00:03:21,363 how exactly. 74 00:03:22,730 --> 00:03:24,210 So for this demo, 75 00:03:24,210 --> 00:03:25,350 I have 76 00:03:25,350 --> 00:03:28,270 two demo files, wildcardcopy1.yaml 77 00:03:28,270 --> 00:03:29,863 and wildcardcopy2.yaml. 78 00:03:30,820 --> 00:03:34,050 What are we doing in wildcardcopy1.yaml? 79 00:03:34,050 --> 00:03:38,007 Well, we are trying to copy test files. 80 00:03:38,007 --> 00:03:40,480 So, test files should be existing 81 00:03:40,480 --> 00:03:42,040 in the current user home directory 82 00:03:42,040 --> 00:03:43,530 mkdir -/testfiles 83 00:03:45,740 --> 00:03:47,731 and let's use 84 00:03:47,731 --> 00:03:48,564 touch 85 00:03:52,566 --> 00:03:54,483 - /testfiles 86 00:03:57,236 --> 00:03:58,090 /file 87 00:04:00,902 --> 00:04:04,510 (1...100).txt 88 00:04:04,510 --> 00:04:06,213 LS until the -/testfiles. 89 00:04:07,440 --> 00:04:10,680 And there you can see, I just created a hundred files. 90 00:04:10,680 --> 00:04:14,220 This is a convenient way to indicate range. 91 00:04:14,220 --> 00:04:17,400 And what happens if you use Ansible playbook 92 00:04:17,400 --> 00:04:19,497 on wildcardcopy1.yaml? 93 00:04:22,690 --> 00:04:26,383 Well, we can see that the copy module did not 94 00:04:27,870 --> 00:04:29,100 like this very much. 95 00:04:29,100 --> 00:04:33,300 It's telling us, "Could not find or access test files star." 96 00:04:33,300 --> 00:04:34,970 So, that's not working. 97 00:04:34,970 --> 00:04:37,330 And that is because we are using 98 00:04:37,330 --> 00:04:38,940 the wrong approach in this file. 99 00:04:38,940 --> 00:04:41,230 Let's have a look at wildcard copy two 100 00:04:41,230 --> 00:04:43,330 and in wildcard copy two, 101 00:04:43,330 --> 00:04:46,370 we do use the right approach. 102 00:04:46,370 --> 00:04:49,583 So, loop is used over query fileglob. 103 00:04:50,829 --> 00:04:53,660 Fileglob is 104 00:04:53,660 --> 00:04:55,110 working on 105 00:04:55,110 --> 00:04:56,960 all files in test files 106 00:04:56,960 --> 00:05:01,550 and that makes sure that they can be copied over one by one. 107 00:05:01,550 --> 00:05:03,120 So, the loop 108 00:05:03,120 --> 00:05:03,953 is 109 00:05:04,880 --> 00:05:06,720 producing a list of files, 110 00:05:06,720 --> 00:05:09,690 query is how you produce a list. 111 00:05:09,690 --> 00:05:12,960 And this list of files is what we are defining 112 00:05:12,960 --> 00:05:13,970 in the loop variable. 113 00:05:13,970 --> 00:05:18,970 And next item is used to iterate over them one by one. 114 00:05:18,970 --> 00:05:22,310 Let me also show you that this works 115 00:05:22,310 --> 00:05:25,003 but you need to be patient. 116 00:05:27,850 --> 00:05:29,720 As you can see it's going there one by one, 117 00:05:29,720 --> 00:05:32,813 so will be busy for the next two minutes or so, doing so. 118 00:05:34,580 --> 00:05:35,413 - So, here we are. 119 00:05:35,413 --> 00:05:37,760 Two minutes later, we've got the result. 120 00:05:37,760 --> 00:05:40,730 So again, it works, but it's not perfect 121 00:05:40,730 --> 00:05:43,250 if you need to go over lots of files. 122 00:05:43,250 --> 00:05:46,080 Do remember the synchronized model 123 00:05:46,080 --> 00:05:47,623 to work with lots of files.