- So, the next lookup plugin
that we are going to explore is fileglob.
What is this about?
Well, the fileglob lookup plugin can be used
to iterate over a list of files according
to a globbing pattern.
And that's indeed very convenient.
If you use fileglob,
on a star between single quotes,
then it'll provide a string
of comma-separated values for all files in the output.
And that allows you to work with wildcards.
I mean, have you ever tried to use the copy module
to copy files according to a wildcard pattern?
That's not really going to work.
And that is why this fileglob lookup plugin can be useful.
Now, to force the lookup plugin
to return a list of values,
instead of a string of comma-separated values,
use the query keyword instead of lookup.
Do remember most lookup plugins
can be used using lookup or query.
So, query fileglob will do what you want to do.
And in the output, you will see that it is a list
because the data is encapsulated by brackets.
Let's have a look at a couple of examples.
So, the first example is fileglob string.
And in fileglob string,
you can see it's quite a simple example,
accessing fileglob using debug
and the message is using lookup fileglob
on dot star in the current user home directory.
Notice that the indentation is weird,
but there is nothing against using 10 spaces
or even more in the indentation
as long as you're consistent.
So, I feel this urge to fix it
but really that's not necessary.
Let me show you Ansible playbook
on fileglobstring.yaml,
and there we go.
We have a string value.
So, this is a comma-separated string
with all files in just one string.
That might not be what you want to do
because it's a little bit difficult to work
with values in that way.
And that is why we also have fileglob list.
And you can see it's completely the same
but this time we are using query instead of lookup.
And if you run it filegloblist.yaml,
then this is what we get.
And as you can see,
each file is listed independently.
Why is that?
Because we are looping over them after all
and the loop this time is working successfully
because it's a list.
And you need lists, if you want to loop.
- We have just looked at the fileglob basics.
Let's dig a little bit deeper
and talk about using fileglob with wildcards.
So, the thing is the Ansible copy module
does not support wildcards.
That is so inconvenient.
And if you want to fix that, you can use a fileglob filter
in the loop to present this wildcard functionality.
Do keep in mind though, that this is not recommended
for copying many files as for angle,
every single file the copy model will be called.
So, it'll be a lot of work
but it'll work and I will show you
how exactly.
So for this demo,
I have
two demo files, wildcardcopy1.yaml
and wildcardcopy2.yaml.
What are we doing in wildcardcopy1.yaml?
Well, we are trying to copy test files.
So, test files should be existing
in the current user home directory
mkdir -/testfiles
and let's use
touch
- /testfiles
/file
(1...100).txt
LS until the -/testfiles.
And there you can see, I just created a hundred files.
This is a convenient way to indicate range.
And what happens if you use Ansible playbook
on wildcardcopy1.yaml?
Well, we can see that the copy module did not
like this very much.
It's telling us, "Could not find or access test files star."
So, that's not working.
And that is because we are using
the wrong approach in this file.
Let's have a look at wildcard copy two
and in wildcard copy two,
we do use the right approach.
So, loop is used over query fileglob.
Fileglob is
working on
all files in test files
and that makes sure that they can be copied over one by one.
So, the loop
is
producing a list of files,
query is how you produce a list.
And this list of files is what we are defining
in the loop variable.
And next item is used to iterate over them one by one.
Let me also show you that this works
but you need to be patient.
As you can see it's going there one by one,
so will be busy for the next two minutes or so, doing so.
- So, here we are.
Two minutes later, we've got the result.
So again, it works, but it's not perfect
if you need to go over lots of files.
Do remember the synchronized model
to work with lots of files.