- In this lesson, we will explore how you can create users
with random passwords.
So the password lookup plugin
can be used to generate random passwords
and store these passwords in a local file.
And note that strictly, the password plugin
generates a random string
and using it as a password makes it a password.
So you can use it to get any kind of random string.
And you can use it to define a variable.
As in my password,
look at password credentials plus item length is six.
Watch the documentation for the password plugin
because it does have a couple of other options as well,
apart from the length.
So this defines a directory with the name credentials,
with a file that matches the users name
so that the password can be handed out to the user later.
And after defining the variable,
you can use it as a password
by applying the password hash filter.
Let's have a look at userswithpassword.yaml for an example.
And if you want to, you can use cleanupusers.yaml
to clean up the users that were just created.
Just for your convenience
because we are working with users so often in this course
you might want to clean them up a little bit.
So let's have a look at users with passwords.
And there we go.
What do we see?
We define this variable
and the variable is set to my passwords.
Look up password credentials, plus item length six.
Then the debug module is going to print the password.
Here we generate a random password
and we want to see what it is like.
So we do that for user item.
What is this item?
Well, item as you know comes from a loop.
Here we have the loop
which is using the lines query on the file, users dot txt.
And this file uses dot txt is processed one by one.
Then we have user and in user we define item
with the passwords that was just randomly generated.
And now we are using the password hash filter.
On this my password to hash it.
We require the password to be updated on create of the user.
And we are using the loop again of users dot txt.
So before we run this, let's have a look at users dot txt.
and there you can see couple of users exist
and let's run it ansible playbook on users with password.
And there we go.
Oh, we get, okay.
Okay, okay.
Well all these, okays, it's kind of boring.
I don't wanna see okays.
I want to see something changing.
So let's do cleanup with users,
which is going to remove all these users.
Notice that here as well,
we are using the lookup plugin with a query
on users dot txt to remove them.
So ansible playbook, cleanup users, there we go.
All users are disappearing and then we can run our playbook
with users with password again
and that will actually create the users.
Here we go.
All the users have been created with their new password
and that is how you can use the plugin
as well as the filter to provide for dynamic passwords.