- In this video, you will learn about lookup plugins.
So lookup plugins are created to use data
from external sources like files and environment variables.
It's pretty cool.
It allows you to fetch data from outside Ansible
no matter where.
And there's quite a few of them.
Use ansible-doc -t lookup -l for a complete list.
And once you found a plugin that looks interesting,
aou can use ansible-doc -t lookup
followed by the plugin-name for its documentation.
I am going to pick out
some of the most interesting lookup plugins
and talk about them.
Just be aware that there is more where this is coming from.
Right, so lookup plugins can be called
in two different ways.
You can call them using lookup or query
where lookup is used to read file content into a variable
and query is used to read the file content into a variable
where the content is stored as a list.
Now here we have an overview
of some of the useful lookup plugins.
File, which reads content of files from the control node.
There is template which processes the contents
of a template.
You would use it as lookup template, mytemplate.j2.
There is env which allows you to use an environment variable
that is used on the control node.
So here we see lookup env DB_password.
So that is going to the control node
and on the control node
it will check in the current shell environment
for the value of the variable DB_password.
There is url, which allows you to get contents from a URL.
So lookup url on http://example.com, for instance.
This is not something that you would use a lot
to discover what is offered by a website
but the url lookup plugin is very useful
to work with API endpoints.
There is pipe which returns the output of a command
that is executed on the control host
like query, pipe, ls files,
where ls files is the actual command.
There is k8s which is for Kubernetes.
It allows you to fetch a Kubernetes object.
As in lookup Kubernetes, kind is deployment,
namespace is default.
It will produce a list of all the deployments
in the default Kubernetes namespace.
Now let's have a closer look at the file lookup plugin.
So in the example line that you can see right here,
the file lookup plugin is used to read the contents
of the file /etc/passwd into the variable myusers.
So vars is setting a variable
and this variable is dynamically created
based on the result of lookup file /etc/passwd.
Also convenient, the file plugin
can take multiple file names as its argument
where each file contents will be separated by a comma.
And you can use query, if you want to store
the file contents in a list.
So vars, myfiles, query file, /etc/hosts, /etc/motd
is producing the list with the file contents.
Let's have a look at an example.
So the example file is readkeys and what are we doing?
We are working and let's just do this on host ansible2.
We are working on ansible2 on the variables users,
student and ansible.
So the task is create users, name item, loop users
and then it's adding authorized keys.
Now, how is that working?
Well, it is working on user item
and the key is using lookup,
file lookup plugin.
Then we can see item plus, item plus is the username
and this username is id_rsa.pub.
So it's looking up id on _rsa.pub in the username.
Hmm. Is it going to work as such?
Probably not because this item needs to be there
but it might be helpful to understand why it is not working.
So ansible-playbook on readkeys.yaml is doing what?
So what is happening?
Well, as authorized key, you can see it's complaining.
Unable to find student id_rsa.pub.
I think this is telling all about what's going wrong.
It's using this item in the file lookup plugin.
The item is student and this expects local directory
with the name student to exist with the id_rsa.pub.
So I can really easily fix it.
If you use mkdir ansible student and I'm using copy.
It's not elegant but this time I just want to show you
a quick solution of how we can get there.
So I'm mainly going to create these pub files.
So let's copy it to ansible as well as to student
and then we can try to do it again.
Obviously, if you wanna go for the nice solution
then you are going to use Ansible
to create that local user directory
in the current project directory or somewhere else
and you are going to generate an SSH public private key pair
as well.
That will be an interesting challenge
but honestly, we shouldn't care about that
because this is really about how we can use
the file lookup plugin.
This is just one example of using a lookup plugin.
There are so many more examples that can be imagined
but I hope this is giving you inspiration
to start using lookup plugins.