So friends in the previous video we discussed how we can utilize loops within
our Gen 2 templates.
What we now want to do is to see how we can use conditional logic and how we're
going to learn
how to do that. Well the best place to go as always is to the documentation so
that's what
we're going to do. So again if we just happen to search for Gen 2 documentation
let's click this
top link right here and this time we're going to once again go to our control
structures right here
and if we just scroll on down a little bit we're going to come to this part
right here. So what WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here. So what
we actually see here is the F statement. Now it says here that F statement in
Ginger is comparable
with the F statement in Python. So once again if you are familiar with Python
you'll have a big
advantage with respect to these Gen 2 templates. Now if you just happen to have
familiarity with
Ansible the conditional logic would be invoked via the when keyword which again
just like the loop
keyword is ultimately acting as a front end a simplified front end for the
underlying Python code
with thin Ansible. Ginger to just happens to express this construct a lot more WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
with thin Ansible. Ginger to just happens to express this construct a lot more
like the native Python.
Now we're going to notice quite a lot of the same things that we saw with
respect to our loops once
again the control structure is going to have the similar syntax we're going to
have our curly brace
as well as our percentage sign. The difference here is that we're going to use
a different keyword
this time instead of the four keyword of course we're going to be using the F
keyword and again
the close off the control structure with percentage and curly brace. Now this
part here might like a
little bit strange it just says if users and let me just not write over that
let's try that again.
In the case here what we're actually asking is a billion value that this means WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
In the case here what we're actually asking is a billion value that this means
if users is defined
basically if we have users i.e. a users key within our variables that is going
to be denoted as true
and just like we talked about conditionals in a previous skill that means that
the underlying
code is going to execute. Now once again much like with respect to our loops we
have to manually
close them off with enginger to and the same thing is true with respect to our
if statements
we're going to have to use the end F keywords to effectively terminate any of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're going to have to use the end F keywords to effectively terminate any of
these F blocks.
Now look at this right here this example it says if users so pretty much if we
have a users key defined
then we want to do an action. Now what is the action? Well the action is to
loop through all
of the users we see four user end users and that gives us access to the iter
able element and then
we can parse out the username then notice we close the four loop here which is
this part here and the
outer if statement which is actually containing the four loop is closed at the
very end hopefully WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
very end hopefully
that makes sense basically this part here only actually happens inside of the f
condition so we
essentially wrap the f condition around it here is where we define the start
and here is where we
define the end. Now we can actually create more complex if statements you can
use things like
LF to specify other conditions if the first condition does not pass as well as
a default else
which will always execute for now we're not going to worry about that let's
just dive in and look
at a basic if statement in conjunction with our current ginger to template. So WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
at a basic if statement in conjunction with our current ginger to template. So
if we go into our
host variables for example let's perhaps remove the nugget user from the wheel
group okay let's just
edit this and what it will do is I'll remove the wheel group okay so DD to
remove and let's do the
same thing for the yellow box we're going here DD to remove and just saving qu
its so if you recall
in Linux the wheel group is used to denote admin users basically people who
have elevated privileges
perform the pseudo command that type of thing now the examples that I have
written up here are not WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
written up here are not
necessarily accurate with respect to the real world configurations on these
devices we're just
trying to manipulate data here but as per this particular example we can see
the answerable user
as a member of wheel so they do have elevated privileges as well as James
however the nugget user
just appears to be a regular user right now because we deleted that wheel
membership similarly if we
look at the yellow box very same thing is true here answerable is once again an
elevated user
or a user with elevated privileges should I say as well as is the nox account WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
or a user with elevated privileges should I say as well as is the nox account
but the nugget user
once again doesn't have that wheel access so let's imagine in our configuration
fair we wanted to
write which users happen to be admin users but we would like to have a
particular condition if they
happen to be a member of the wheel group we want to run the test and the test
is if the wheel happens
to appear then we can perform a particular action we can say something like the
user has
elevated privileges otherwise they do not have elevated privileges so that's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
elevated privileges otherwise they do not have elevated privileges so that's
basically what we're
going to try to do so let's go and check out our template right now go in and
save them template
test.j2 so what I could do is I could say as I'm looking through my users I can
add my control
logic so color brace percent and here is my condition if and then within my
quotation marks
I'll test if wheel is in the user dot groups and then I'll close off my control
structure with
percentage color brace then I can add in some new text perhaps I can say the
users on my system WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
users on my system
with elevated priffs are and then we pass in the username and then we specify
which groups
they happen to be belonging to and the hard part here might be noticing where
abouts to be closed
off the if statement so let's work our way out to in here is the first first
statement and that is
closed off here at the end and then we have our inner first statement here
which is closed off here
now inside of the first statement here we have our if statement which means we
actually want to
have the if statement in between right here see it's kind of like a mirror WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have the if statement in between right here see it's kind of like a mirror
reverse so what we want
to do here is I'm going to go in between these two control structures and ender
color brace percentage
end if so if I press escape right and quit let's clear the screen and let's try
rerunning that
playbook and to enter and we can see the configurations that have been applied
so let's go and check
the config test file and check out the result it says there are users in fact
that's a typo there
that should say their users on my system with elevated priffs are and we can WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that should say their users on my system with elevated priffs are and we can
see the answer will
use it as well as the grip and in fact what we've done here notice we actually
have a repetition
of this statement the users on my system with elevated privileges that is
because this statement
here is actually also within the loop so it's being printed twice let's
actually modify that and
this is actually good to see because very often this is how you actually do
these ginger templates
you test it out before you actually deploy and you notice that there are some
slight problems
and what do you do just like a look you just iterate and iterate until it is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and what do you do just like a look you just iterate and iterate until it is
correct so this part here
as we can see is actually below therefore inside of a for loop so what I will
do here is let's just
remove this statement we don't need that and I'll just say the users on my
system with elevated
priffs are and that is now outside of the for loop unless you delete the one
that is inside
so if I press escape colon write and quits clear the screen add it up and rerun
the playbook
okay there's a changes in play let's go and verify so go into yellow box let's
cat the
config test this looks a lot better now it just says the users on my system WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
config test this looks a lot better now it just says the users on my system
with elevated priffs
are and we see the answer will user and the groups to which answer belong to
answer will and will
similarly we also see Knox has elevated priffs and we see Knox's group
membership
Knox wheel and programming notice nowhere now because of that conditional
statement
do we actually see the nugget user and we should expect to see the same thing
on the blue box
so go to blue box and we'll cat the temp config test file enter check this out
right here exactly WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
right here exactly
as we wanted to see the users on my system with elevated priffs are the answer
will user with
the corresponding groups as well as the James user with the corresponding
groups once again
and excluding that nugget user because of that conditional logic.