- [Instructor] In the previous video,
you've learned about lookup plugins.
There are so many of them
that I feel that is needed to have a look
at a couple of them in more detail.
So to start it there's env, env for environment.
So this lookup plugin is used to set a variable
that's based on the value of a shell variable
on the managed host.
And if you combine that with a default filter,
it has a nice potential to automatically create
or modify content based on local settings.
In particular, when combined
with the template lookup plugin, it can be very useful.
Notice that a template lookup plugin
is not the same as a template module.
The template plugin issues should generate texts
that can be used in modules like copy.
Sometimes it's a little bit confusing
and you really need to clarify
what it is that you're talking about.
But this time, we are talking about env,
and template variables.
Let's have a look at setlanguage.yaml, our first example.
(keyboard clicking)
So here we have setlanguage.yaml,
and what it is doing.
Well, choosing templates to provide the right language.
So we have the variable, the variable is using language.
And then we have the lookup plugin.
The lookup plugin env for environment,
which is looking up, the local variable Lang.
The local variable Lang is setting the language.
And if this local variable is not set,
then it's using the default.
And the default...
Notice that that's a filter that we are using here.
The default is setting the value of the variable
to en_US.UTF-8, which will use English language.
Now we have the task, install MOTD in the right language.
So what do we get?
Well, we get a copy,
and copy is using lookup template motd + language.j2.
And it's writing that to the destination, etc/motd.
So we need some template files ls star j2,
and there you can see that we have an motd en,
and then motd fr.
So let me use ansible playbook on setlanguage.yaml,
and that is working out alright.
Oh, it's not working out alright.
Because apparently on ansible 1,
I need a pseudo password, -K.
There we go, become password, and then we try again.
And there we can see it installing the motd
in the appropriate language
on the different nodes that are involved.
Good, let's have a look at some more lookup plugins.
Our next example is based on pipe and lines.
The pipe and lines lookup plugins
are used to return output from a command
that has been used on a control node.
Where pipe shows raw, which is unformatted output,
and lines split the output
in lines that can be treated as a list.
In the example, let's have a look at lines.yaml.
And just to give you a tip in the example file,
change the lines to pipe and observe the difference.
Let's go check it out.
So what do we have in lines.yaml?
Well, in lines.yaml,
we see it is printing the first line of any file.
And we can see that we have debug on item 0.
So that's printing what we are currently looping over,
and then we have the loop, and the loop is happening
based on query lines on cat/etc/hosts,
and query lines on cat/etc/passwd.
So the loop is just processing over a list,
and the thing is that we should do something with it.
And let's observe what it is doing.
So ansible playbook on lines.yaml,
and there we can see,
"Print the first line of any file."
Here we have the localhost file,
and we have the password file.
And we can see that in the messages
we have the first line, and again, the first line.
In case you are wondering
where does this first line come from?
Well, the query using the lines plugin,
is generating a list.
A list is an array, and we print item 0.
We can change it.
And if I make that item 1, for instance,
then we are going to see a difference.
So right here, we can see 127,
right here we can see a root as the first line,
running it again.
And now you can see, we have the second line,
which is ::1, and the bin user.
So there is a little bit of difference.
So the next lookup plugin
that we will be looking at is URL.
The URL lookup plugin can be used
to fetch content from a URL.
The main benefit is that this allows you to use data
returned as values in variables.
And notice, that for more generic use,
you can also use the URI module.
So there's the URL lookup plugin, the URI module.
And let's go have a look at url.yaml,
and after that let's do some additional examples.
So url.yaml, here we go.
This installs web server on ansible 2.
So it installs a web server,
it uses a copy module to create an index html.
It starts and enables,
and it opens the firewall using firewall D,
and in firewall D, we open the http,
as well as the https ports.
All of that's pretty straightforward.
Then we are using the URL lookup plugin.
We do the lookup from localhost using debug,
and we print a message,
ansible 2 shows, lookup URL, http ansible 2.
So we should see the content,
the welcome message that fetched from the URL
and produced by this lookup plugin.
Let's go run it, ansible playbook on url.yaml.
And there we go, ansible 2 shows,
"Welcome to this web server."
And "Welcome to this web server,"
that is exactly what was returned from the URL.
Now, there are two more examples
that I would like to show you.
These examples are starting with the users.txt.
So here we have a list of users,
and I'm going to work with these users.
And in order to do that, I have two playbook.
Makeusers1.yaml to start with.
What is this doing?
This is populating the users from a file.
The host all, get effects no,
and then we create the remote user.
And what are we doing here?
We are doing something new, this is so cool.
We are using password,
and password is using the password lookup plugin.
And this password lookup plugin is generating credentials
for item with a length of nine.
Now, the nice thing about this password lookup plugin,
is that you can use it to dynamically generate credentials.
Then I'm using debug, debug is just printing the password
as passwords and loop is using query lines
of a cat users,txt.
This is definitely one that you want to remember.
If you want to process all lines in a specific file,
query lines on cat on that file will do it for you.
Let's go check out how this is working.
So ansible playbook on makeusers1.yaml,
and there we go.
You can see the passwords that have been generated
for all of my users.
Of course, it's doing that twice for every single server.
That's not really the thing that matters.
What matters is that we can see
for user Isabelle on ansible 2,
the password is a random string,
and all of these users have a beautiful random string.
This is really nice,
and let me show you again, the password lookup plugin
which you can use to generate dynamic passwords.
Hey, did you do the bonus part of the lab in Lesson 10?
That's all about this dynamic password.
If you didn't do it yet,
go have a look at it and try it again.
Right, I also would like to show you makeusers2,
and what are we doing in makeusers2?
Well, we are still using this lookup plugin
to define the value of the variable,
and then we are going to use that value.
So what are we going to use?
Well, look carefully.
We have the variable password defined here,
the variable password.
This is dynamically generated value.
Then we have the password property on the user,
and the password property on the user
is looping over all these users
based on the input value users.txt.
And this password property
is using the password hash filter.
So this is where we have the password plugin,
and the password hash filter working at the same time.
So the password plugin is already generating
a pretty complex string,
but in order to really secure it,
we need to provide some encryption using password hash.
And this is what is really going to make sure
that your users are getting a password that is very secure.
So I'm using ansible playbook on makeusers2.yaml,
and there we go.
This should create all of these users for us,
and you can see that is working out pretty well.
So if I'm using ansible on ansible 2, -a tail/etc/shadow,
that's where we can see the passwords.
After all, here we can see the users
with the encrypted passwords.
Notice that this time I did not define any random sort,
so it has used a random sort that is really random.
That's in the highlighted part.
And this is how you create users
that really have secure passwords.
I hope you like it.