Okay friends, so let's walk through how to actually solve this particular
problem.
So what I will do here is I will just create a Playbook which is called this "
Users and
Groups" the name itself doesn't actually matter, not specify what it should be
called and let's
go in here.
So, very first time we'll do we'll have our three hyphens at the top, it is a Y
AML file
after all and I'll give this Playbook a name or just say "Creating Users and
Groups".
Now who was I specified behind the target?
Well, I said we should target all the hosts within our network so I'll just say
the all
group.
So if I don't have to gather facts, I'm going to set gather facts to be false, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if I don't have to gather facts, I'm going to set gather facts to be false,
just in order
to speed the Playbook up.
And now what I want to do is I want to effectively define my variables.
So here's my Vars right here okay.
Now you should also have this particular text for your right here that I've
provided within
this particular skill.
So if I just copy all this information right here and all I'll do is paste the
10 and
fix the indentation.
Okay, so let's now define our tasks that we want to actually perform.
And the first task is going to be given a name and we'll just simply say "
Create the
Groups".
Because if you recall in the Skill whereby we created users and groups, we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Because if you recall in the Skill whereby we created users and groups, we
learned that
it is best practice to actually create the group first in order to avoid any
type of
problems.
Now what are we actually going to use?
We're going to be using the Ansible built-in group module.
So again if you're in the examination and you want to maybe get some
information, we
can say Ansible dot and then just say Ansible built-in and maybe basically get
all the documentation
on how to actually use this particular module.
Now again at the very bottom here, we actually get some examples which is quite
handy.
So all I will do here is I'll just copy this information here because it's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So all I will do here is I'll just copy this information here because it's
quite a good
idea to just copy and paste to not worry about indentation quite so much.
And if I just go back out and I go back into my playbook, all I will do is I'll
just simply
paste this in.
And again I'll just have to fix the indentation, kill so let me just get rid of
this line here
and I'll just modify this to have the correct heading.
Now here's the thing, we have two options where we can actually solve this
particular
problem.
It's what we can do is we can just manually specify the group name that we want
to create
such as heroes and then effectively copy the same task and then populate the
value of villains. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
value of villains.
Now because we only have two groups and it's not really much of a slog to do
that, that
is what I'm just going to do.
So let me create the heroes group and again let's make use of the old copy
paste to keep
things nice and quick.
Gives us more time to think in the exam rather than spending time typing.
Now I'll just paste this all the one in and in fact let me just say hero grip
account,
just that's a little bit more descriptive.
And this one here I'll just say create the villain and of course I'm going to
have to
change the value here to villains.
So the next thing we want to do is we want to create our users and add them to
a particular
group.
So we want to use the user module this time. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we want to use the user module this time.
So if we check the documentation for the user module that's going here, once
again we
can scroll through all of the different options and parameters, we can see some
examples that
we can use as a basic template.
Now just one thing I want to highlight to you using the modules here are the
examples
and the modules page as a template is a very very good time saving ideas but
you do have
to be careful.
Let me show you why.
Say for example this one here, this one looks pretty innocuous, it looks like
it does create
a user and gives me a comment, a user ID and it assigns that user to a group WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
a user and gives me a comment, a user ID and it assigns that user to a group
called admin.
Just be aware that you happen to be reading what the example is attempting to
do and also
perhaps if you're unsure also read the description of the parameters.
The reason why I'm saying this is because in this example here, this is
actually going
to add the user to a primary group of admin.
So this key here is adding to the primary group whereas if I scroll on down a
little
bit further, we can see here this particular option actually uses the parameter
groups,
not just group.
See that plural and because of this, if you recall when we talked about users WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
See that plural and because of this, if you recall when we talked about users
and groups,
this is how we can add users to a second group which in this case here is what
we would rather
do and again we can verify this in the actual output here by just looking at
the parameters.
Group sets the primary group and groups allows us to add secondary groups that
the user is
going to be added to.
So it's this particular one that we want to use.
So let's go and grab this here.
Okay, so let's grab you, copy this, back into our playbook and all I'll do is
paste this
in right here and then fix the indentation.
So we indent in four spaces, one, two, three, four. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we indent in four spaces, one, two, three, four.
Now what you could do I suppose is for every single user, you could just keep
copying,
pasting in this task and just pass in the hard code of values as you need.
But of course, this is not really the most elegant solution nor is it the most
efficient
solution in terms of saving time.
So what I want to do is I actually want to invoke a loop to loop through all of
the different
users I have and just populate the values here with variable values.
So for now what I will do here is I'm going to remove this shell option because
we're
not specified to specify a particular shell.
So let's just get rid of this with DD and I'll just give this a task of create WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's just get rid of this with DD and I'll just give this a task of create
the users.
Now like I say what I want to do is I want to loop through all of my data.
Now how do I do that?
Well, if you look at the top key here, the actual object is known as movie
people.
And if I iterate through the first iteration is going to give me this object
right here,
the name and the type and the second iteration will give me this element Spider
man and heroes
and then the third Superman heroes, then Joker villains, so on so forth.
So check this out.
What I will do here is at the very bottom at the same level of indentation as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
What I will do here is at the very bottom at the same level of indentation as
my actual
module itself, I will specify I'm going to be looping through something.
Now what am I going to be looping through?
Well, if I just use my double curly braces, I will specify that I want to be
looping through
my movie people object.
Now recall when we happen to invoke a loop and answerable, the constituent
parts can be
referenced via the keyword item.
It's a special keyword with an answerable.
So let's get rid of this hard code of value here.
And again, let's have my double curly braces and I'll specify my item.
Now it's not actually correct quite just yet because the item itself in this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now it's not actually correct quite just yet because the item itself in this
case here
because in first iteration, we're going to get both the name and the type.
You see that because pretty much we can see the little hyphen here that's going
to give
us the first object, second one here, third one here.
So we want to be a little bit more precise and as we iterate through each item,
in the
case of getting used on name, we also want to grab the name key to just get
this piece
of the data that we're looking through.
And then on second iteration, just grab this name to get this piece of
information.
So on so forth.
So let's scroll on down and I'll say the name is going to be item dot name. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's scroll on down and I'll say the name is going to be item dot name.
Now the groups that we're going to be belonging to, but also going to be loop
ing through.
So let's have my double curly braces.
And I will say item and what do I want to grab this time?
Well, if they happen to be a hero, I want to grab the type heroes and if they
are a villain,
we can grab the type to get the villain.
So again, we're going to grab the type key this time.
Now this is actually not all, but also going to have to provide a password for
each of
these accounts.
Now if we go back in and consult the documentation, if we scroll on down, we
can see we can actually WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
can see we can actually
create a password.
In fact, if I just say forward slash and just type in password and hit enter,
it's going
to snap us to the relevant part, which is also a good time saving piece of
information.
So we can see the password parameter.
It says here, if provided set the user's password to the provided encrypted
hash if
you're on Linux.
Okay.
So think about this.
Remember when we dealt with this in the earlier scale within this course, what
we can
do is we can actually run this through a filter.
So what I will do here is I'll use the password key right here.
Now here's the thing.
If the user happens to be a hero, I want to give them the password of hero pass WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If the user happens to be a hero, I want to give them the password of hero pass
.
Okay.
So my single quotation marks, I will specify hero pass and then I will use my
pipe to invoke
a filter and then I will say I want to use the password hash filter and then
within my
parentheses, I'll specify the hash I want to use, which in this case here will
be five,
one, two and again, close my parentheses.
So think about it like this.
This here is going to look through all the users as it stands right now.
Grab out their name, populate it into the name parameter, take it or type and
assign it to
the group's parameter. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the group's parameter.
Then we're going to give everyone a password of hero pass and make sure that
password is
hashed.
Now the problem is, is that we don't want to give absolutely everyone this
particular
password.
We only want to give half of them that password and the other half a password
of villain pass.
So we have a condition right here.
So it's a good idea maybe to actually invoke your conditional logic, ie
utilizing a when
statement.
So what I can do here is below at the same level of indentation as my look, I
will use
my when keyword.
Now what is a condition I want to specify? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now what is a condition I want to specify?
Well, specify when the item dot type that I'm looping through, which is going
to specify
if I'm a villain or if I am a hero, when I evaluate, so I'll use my double
equal sign,
when it evaluates to the value of heroes.
So pretty much this task is going to look through everything that we have and
then evaluate
if the item dot type that we're looping through comes to heroes, then we'll do
all of this
stuff right here.
Okay.
So we're only effectively going to create what is now the hero users.
So that's only one part of the task.
We also have our villains. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We also have our villains.
So what we can do therefore is if I just copy all of this right here, just grab
you and
again, I just go to the margin and I just paste all of this in again, boom.
As so we can just make a quick modification just to create the villain users in
this case
here and all we're going to do here is change the password to villain pass.
And this is only going to run when we evaluate the type we're looping through
evaluates to
villains.
So let's just save this right here.
And for now, if I happen to log into my devices, let's go into device one, for
example, we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
example, we
can see we're on device one.
If I happen to go into the exit password file, notice we don't have any
accounts for Batman,
Superman, Spider-Man, that type of thing.
And similarly, if I go into the exit group file also, slowing one down, notice
we don't
have any group for heroes or villains.
That should change when we're on this play book.
So let's exit back out, get back to our control node.
If I just run the playbook of answerable play book users and grips and to enter
, we now create
the grips and the users and make all the assignments.
And if we actually log into our device now, device number one, if I say less on WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And if we actually log into our device now, device number one, if I say less on
the exit
pass wd file right here.
And if I scroll all the way down, we can see here at the bottom, we can see we
have Batman,
Spider-Man, Superman, Joker, Green Goblin and Lex Luthor and similar live, I
press Q.
And I also say less on the exit group file right here and to enter scroll all
the way
down, we can see here we have our heroes group and who's in the heroes, we have
Batman,
Spider-Man and Superman and the villains group, we have Joker, Green Goblin and
Lex Luthor.
Now the crucial point to note here is if I just happened to try to become one
of the
users and I say substitute user to Batman, Batman should have a password of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
users and I say substitute user to Batman, Batman should have a password of
hero pass,
so let's type in hero pass.
And as we can see here, we're successfully logged in as Batman if I exit out.
If I try to become, say for example, the Joker, Joker should have a password of
villain pass.
So if I hit enter, now we can see here I became the Joker account.
And the same thing will be true over on device number 2.