So friends in the previous video, we learned how we could use the user module
to create
a simple user account on both of our managed devices.
What we now want to do is to look at that group module so that we can
subsequently create
more groups on our devices and then we'll use the user account to actually add
those
users to those existing groups.
So what we're waiting for, let's get to it.
So all I will do is I'll create a new playbook, let's call this 1 play 2 and
let's go in
and edit it.
So we have our three hyphens at the top and the name of this playbook is going WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we have our three hyphens at the top and the name of this playbook is going
to be play
2 testing the group module.
Once again, we're going to target the all group and this time I'm going to
actually
set gather facts to false to speed things up a little bit.
So the first thing I will do is I'll specify my tasks right here and the first
task is
going to be, let's just say task 1 creating group 1.
So we're going to use the Ansible built in group module and what do we have to
supply
here where we're going to have to supply the names that is a mandatory
parameter and the
name of the group in this case here is going to be group 1. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
name of the group in this case here is going to be group 1.
Now what I'm also going to do is I'm going to set the state to present meaning
that I
want this group to be on the device.
So for now all I will do here is I'll just write and quit and let me go over to
my managed
devices for a moment.
So if I cat the Etsy group file, notice we actually do not have a group 1 group
account
but when we run this playbook, things should be a little bit different.
So that is exactly what we'll do.
We'll say Ansible playbook.
I will say play to that YML answer. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I will say play to that YML answer.
And as we can see here, we have two changes, one in the blue box and another on
the yellow
box.
That is a good sign.
So let's go and check.
So we'll go to the yellow box.
Let's arrow up and we'll just cat the grips once again.
Look at this down the very bottom.
We have group 1 and we have automatically be assigned the group ID of 1 1 0 0
and the
same thing if we go over to the blue box, let's just cat Etsy group and to
enter, look
at the very bottom, we see group 1 there as well.
So check this out right now.
Let me go back to my yellow box right here and I'm going to say ID Fred. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Let me go back to my yellow box right here and I'm going to say ID Fred.
Now what this actually shows us, it shows us Fred's group membership.
We can see Fred is a member of the Fred group and that is the only one Fred is
a member
of.
So what if I go into my playbook and I specify that Fred should be a member of
group 1.
Let's see what actually happens.
So all I'll do is I'll create play 3 right now.
Just do my basics, my 3 hyphens, get the player name and I'll target the all
group.
Let's just say gather facts as equal to false and now we define our tasks.
The name here is going to be add Fred to group 1. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The name here is going to be add Fred to group 1.
Now quick question, which module should I use here?
Should it actually be the group module or will it be the user module?
Well the answer is actually the user module because recall we use the user
module to assign
users to already existing groups.
We use the group module to create those groups.
Now the group 1 here has been created in the previous playbook.
All we want to do is assign the user to that particular group.
So I'll use the user module and I'll specify that the name is going to be Fred WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So I'll use the user module and I'll specify that the name is going to be Fred
and all I'm
going to do is I'm going to specify which groups Fred should be a member of.
Now watch this if I just say group 1 right here and I leave this as it is.
Watch what happens when I run this playbook.
So let's run play 3.YML.
I'll answer.
We have a change on two devices.
Let's go back to our yellow box then.
So if I arrow up and to enter, look at this.
Fred is actually a member of two groups now.
He's a member of the Fred group and group 1 now.
Isn't this a little bit strange?
Then I say earlier on that we would have to use the append parameter to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Then I say earlier on that we would have to use the append parameter to
actually append
Fred onto new groups.
Otherwise the group would just override Fred's group membership.
Well this is something I really want to highlight to you.
This is only applicable to the secondary groups.
This will in fact not override Fred's primary membership to his automatically
created Fred
group.
So just be aware of that distinction.
So check this out then.
Let's go back and make a modification.
What we will do is we'll go back into the second playbook right here and let's
create
another group here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
another group here.
So we'll say name.
I'll just say task 2 creating group 2 and I'll say answerable built in group
and the
name this time is going to be group 2 and the state well that's going to be
present.
Perfect.
So let's just write and quit and let's rerun playbook 2 and to enter.
You can see we have a change on both of our devices if we go back to the yellow
box to
clear the screen and we'll cat.
It's a group.
Then the very bottom we can see here we have group number 2 and the same thing WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Then the very bottom we can see here we have group number 2 and the same thing
will be
true on the blue box.
Now if we go back to our playbook 3 right here let's modify this.
Let's just try to assign Fred membership to group number 2 now.
Okay so check this out.
Now just before I do this remember Fred's current membership is with the
primary group
Fred and group 1.
When we push this playbook watch what happens if I enter now the task actually
says here
adding Fred to group 1 I really should have changed that but nevertheless the
actual group
has been created as group 2.
This part here doesn't actually matter.
If I go back here and I see ID Fred look at this look at Fred's group WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If I go back here and I see ID Fred look at this look at Fred's group
membership Fred
is still a member of the primary group Fred but Fred is not a member of only
group 2 as
a secondary group group one's membership has now been revoked and the same
thing is going
to be true on the blue box device.
So just be aware when we are overriding group membership that doesn't actually
apply to
the primary group itself that's still going to stay there.
Very very important detail to remember.
So let's go back to our playbook right here and all I'll do is I will modify
this to add
Fred back to group 1 and this time I'm going to set the append value to true to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Fred back to group 1 and this time I'm going to set the append value to true to
check this
out now okay let's write and quit let's run the playbook for play number 3 and
to enter
we see a change and if we go back now and let's arrow up here look at this.
Whilst we only pushed a change for group number 1 due to the fact we've app
ended Fred
has now 2 secondary memberships to both group 1 and group 2 that is the effect
that we're
getting here and all I'll do is I'll copy over play number 2 and I'll create WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
getting here and all I'll do is I'll copy over play number 2 and I'll create
one called
play 4.yml and let's go in here okay.
So I'll create task number 3 I'm just creating group 3 I'll use Ansible built
in group once
again the name this time will be group 3 and the state is going to be present
and let's
just add another one for good measure that's going to be task 4 creating group
4 we'll
have Ansible built in group this one here is going to have the name of group 4
and the
state is going to be present.
Now one thing I want to highlight to you here check this out okay if I go to
the top right
now and let's just create a new task here called name I'll just create new user WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
now and let's just create a new task here called name I'll just create new user
and
I'll use the Ansible built in user module and we'll have a user called Michael
we'll
give Michael a shell of then bash to give a user ID of 1 1 2 2 now when it
comes to groups
now what we'll try to do here is we'll try to assign a group membership to a
group that
doesn't actually exist yet so we'll say group number 4 okay.
So remember the play book is going to execute sequentially we will attempt to
perform this
particular task first and when they go to assign group 4 that will not be WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular task first and when they go to assign group 4 that will not be
created yet
that will not get created until we get down to the bottom of the play book okay
.
So watch what happens here I will say Ansible play book play 4.yml and to enter
look at this
right here tells me that group 4 does not exist so we're actually getting a
failure
here now if I just modify this I'm going to play 4 and let me just copy this
right here
and all I will do is I'll move this particular task down to the bottom of the
play so it's
now going to execute after the creation of group 4 and all I will do is I'll WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
now going to execute after the creation of group 4 and all I will do is I'll
scroll up
to the top and I'll just remove all this here so now the task is going to be
creating all
the groups ensuring they're on the actual device and then we can assign the
membership
okay so let's check this out we'll say Ansible play book play 4.yml and to
enter now we seem
to have a success and if we actually go back to our devices if I cat the Etsy
group file
we can indeed see that we have these additional groups right here and when it
comes to the
user Michael Michael is a member of group number 4 as per our direction now let WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
user Michael Michael is a member of group number 4 as per our direction now let
me just briefly
change this right now and I'm going to play 4 once again and let's say I wanted
to add
Michael to multiple groups okay because like I say right here if I say ID
Michael Michael
only as a member of the primary group of Michael and one second group group 4
now we know we
can sequentially just keep adding on different groups but if you want to do in
one fell swoop
remember as per the documentation this particular parameter takes a list of
strings so what I
can do is I can say I want Michael to be a member of group 1 and then have a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
can do is I can say I want Michael to be a member of group 1 and then have a
comma to
specify a new value okay so then I can say group 2 as well as group 3 now I
could just
add these particular groups and right now what would happen this would override
Michael's
group 4 membership but I don't want to do that what I want to do is I want to
say a
pen is equal to true now I run the playbooks or just say answer my playbook
play 4 dot yml
and to enter check this out right here let's go back to our devices let's check
the ID for
Michael and look at this Michael here is now a member of the Michael primary WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Michael and look at this Michael here is now a member of the Michael primary
group as expected
but also a member of groups 1 2 3 and 4