So friends, now that we know how to use basic variable substitution within our
Ginger 2 templates,
let's now take a look at a little bit more of an advanced subject, and that is
utilizing
loops within our templates. So with that said, let's just get to it. Now, one
of the best places
that I would reference that you go to if you want to learn how to use Ginger
templating any more
expressive manner, and that is to go to the actual Ginger 2 documentation
itself. So if we collect
the link for Ginger 2 documentation here. So as we can see here, we are WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the link for Ginger 2 documentation here. So as we can see here, we are
presented with a table of
contents. There are a whole number of different sections that we can explore,
but if we scroll
down to list of control structures right here and click this link, what it says
here is that a
control structure refers to all things that control the flow of a program. So
we have things like
conditional statements, but the thing that we want to be looking at is this one
right here, a for loop.
So a key point to understand is that when we want to utilize a loop within Ans
ible,
we just use the loop keyword. But ultimately, Ansible is acting as an WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we just use the loop keyword. But ultimately, Ansible is acting as an
abstraction layer presenting a
yaml interface to interact with the underlying Python code and the underlying
Python code is
utilizing a for loop. So we can actually see here the syntax is using the
keyword for and then we
specify a variable here, which can be any name you so wish, although is a good
idea and good practice
to have something sensible, some descriptive. And then we have the keyword in,
and then we target
the iterable object that we want to loop over. So an iterable object, that is
anything that can be
broken down into smaller components, such as an array can be broken down into WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
broken down into smaller components, such as an array can be broken down into
individual elements
within the array. This is what we're talking about here. Now, when we happen to
utilize the
variable here, given the name user, and then this particular variable here, as
denoted by the name
user gives us access to each individual element. So if that element also has
multiple keys, we can,
in this example here, use dot notation to access the username key. Now the
interesting thing to
note is that if you happen to have experience with Python, this part here looks
very, very familiar. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
very, very familiar.
But with Python, the for loop is going to automatically close itself. In the
case of ginger too, that is
not true. We actually have to be very explicit and manually close each for loop
that we happen to open.
And we do this as we can see here with this end for statement, again, within
the colubracing
percentage signs. So if we happen to have multiple loops, ie nested loops, we
're also going to happen
to have multiple end for statements. That's kind of the rules here at play. Now
, very, very important.
Recall with respect to our variable substitution, we could just use our double WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Recall with respect to our variable substitution, we could just use our double
curly braces.
When we want to be using these control structures here within ginger too, we're
going to have our
curly brace and then our percentage sign. See that? Colubrace percentage. And
then the actual control
structure, whereas when we're actually referencing the actual variable, which
we're looping through
here, we still use the double curly braces. That is the gist of it. Now, if you
happen to have
familiarity with Python, this will be much more natural to you. Whereas if your
only exposure is
to answerable itself directly, this might take a few tries just to get WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to answerable itself directly, this might take a few tries just to get
comfortable. But like I say,
with everything, it just is a little bit of practice to get familiar. So let's
actually add to our
variable files and begin building out a larger configuration file rendered via
the use of loops
within our logic. So check this out then. So let's go into the host files here
and let's go into
blue box dot yml. So all I will do is I'll add in some new information. Okay.
So let's add in some
users here. Now this doesn't actually have to be accurate. Like you say, we're
just creating some
dummy configurations just to test out how we can use these templates. So let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
dummy configurations just to test out how we can use these templates. So let's
just say on the
blue box here, let's imagine we happen to have a user called Ansible, which we
actually do.
And then we'll specify the group to which Ansible belongs to. So we'll say it
belongs to the Ansible
group, as well as at the real group. Again, we're not caring about the actual
accuracy here,
I think they're a little bit off. Let's add a new name. We'll have the nugget
user. The group's
belonging here is going to be nugget user, as well as let's just say the real
group as well.
And let's imagine it's also part of the, oh, I don't know, let's say the
marketing department. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
marketing department.
Okay. And let's maybe add another name, let's just say go with James, add to
the grips of James.
We have the primary James group, James can be part of the real group, and James
can be also in the
engineer group. Okay. So write and quit, let's go into the yellow box here, let
's do a similar
thing. So we'll add in the users, add the name of once again the grips will be
Ansible, as well as
we'll add in the nugget user as well here, add on nugget user and we'll, and
let's just say on this
particular device, instead of James, we have a user called Knox. In fact, let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular device, instead of James, we have a user called Knox. In fact, let's
just add marketing
here just for a little bit of consistency, not that it matters. So Knox is in
the Knox primary
group, Knox is also part of the wheel, and let's say Knox is in the programming
department. Okay.
So check this out. Let's go back to our template right here. So I want to
invoke my loop. Now remember
how to do this, curly brace percentage time. And now I specify my loop
contracts, I'll say four,
and now I can choose a variable of any name I so wish. So I could say for pizza
, and the users,
then I have my percentage sign and curly brace. And the reality is this is WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
then I have my percentage sign and curly brace. And the reality is this is
absolutely valid.
We have our four keyword, the N keyword, and the actual object we're going to
look through,
which is going to be the users key, which is corresponding here, this
particular key here.
So that gives us access to each element, which is this one here, the first
element,
and then the second elements. And then we can call the individual keys within
that particular
elements of say, for example, upon first iteration, we could grab the name key.
The key point to know,
though, is that each element in the array is going to be represented with
whatever keyword you use WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
whatever keyword you use
here. Now right now, I'm using pizza just to show how flexible this can be. It
's not actually a
very good thing because it's not actually descriptive of what we're looking
through,
not exactly best practice, but it can work just to show you. So if I wanted to
actually get the
variable here, I could just have my double curly braces and say pizza, which
gives me access to
each iteration and then call the key that I want to target, which in this case
here would be the
name key. Okay, this would actually print out the names. Now what am I going to
do here? Because I
have opened my for loop, I'm going to have to close it. So I'll have my curly WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
have opened my for loop, I'm going to have to close it. So I'll have my curly
brace and then
a percent and then I will say end for and then again, curly brace percent. This
here should be
absolutely valid. So let's try this. And if I just press escape, colon wq,
writing quits,
let's see the back. If I clear the screen and I rerun the same playbook, which
we'll call the
same template just a modified version of the templates, let's hit enter and see
what happens.
Okay, we seem to have a change on both devices. Let's go and confirm. So on the
yellow box,
we're going to the 10 directory that's done LS and great for config. Once again WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we're going to the 10 directory that's done LS and great for config. Once again
, that version
that be overwritten with the new config and check this out right here, we
actually have rendered
the correct template. Here is all the original stuff. But down here we have on
my system, I have
the following users, Ansible, nugget user and Knox. Notice the way we were able
to correctly parse
out the names, despite using a rather poorly worded variable name and pizza.
Okay, didn't
actually matter. Again, we go to the blue box here. Let's cd into temp. Let's
cat config.txt.
This time we have the original text. And then again, we have added on on my WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This time we have the original text. And then again, we have added on on my
system, I have
the following users, Ansible, nugget user and James this time. Now here is an
interesting thing.
Suppose you want to have another loop that's going to look through all of the
groups that
each member belongs to. This is going to require a nested loop. This actually
is quite interesting
and may be confusing when you first see it because we have two different loops.
We're going to have
to close both of them with two end four statements. So let's go and check this
out. So let's go back
into the template. Now what I'm going to do here is I'm going to just change WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
into the template. Now what I'm going to do here is I'm going to just change
the name of the variable
pizza to something that actually is descriptive now I've actually highlighted
the ability of
being able to choose anything user wish. And I'll just use a much more
conventional name such as
the word user. So I'll say for user and users. Okay, now I'm going to have to
change what I'm
calling here and reference the name of the variable so that it's match up. So
we have user dot name.
As I loop through each of these users, I also want to add in the group
membership for each user. Okay,
and that was just a little bit of indentations to make things a little bit WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and that was just a little bit of indentations to make things a little bit
clearer. And I'll just
say groups colon and then I'm going to have my programmatic logic once again.
So color brace percent
and now I'll say for and again choose any variable use a wish but because it's
a little bit more
descriptive, I will say group and and what am I actually looping through? Well,
I'm actually within
a nested loop. So I'm going to have to call the user variable here which access
es each elements
again before this would have actually been pizza as I discussed but now it's
back to being user.
And what is the actual key that gives me all of the array objects? I eat all of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And what is the actual key that gives me all of the array objects? I eat all of
the groups what we
can see here from the actual documentation. This is the first iteration and if
we grab the groups key
that gives us access to this iterable object which is answerable and we'll
which we'll look through
and then upon second iteration we get the nugget user again grab the groups key
and then we get
this element which we can look through. So we're going to say user dot groups
and again we're
going to have a percentage sign and color brace and just to make things a
little bit more readable
I'll have let's say a hyphen and then just give the name of the actual group. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
I'll have let's say a hyphen and then just give the name of the actual group.
So I'll have my double
color brace and then give the variable name group. Once again choose any name
you so wish for this
group just happens to be descriptive now because we have opened our four
statements here. This is
actually now being closed off with this particular four statement the next one
to it the one below
but we still have this particular four statement opened up and that means we're
going to have to
close it again so it's going to be closed at the very end. So we just say in
four percentage
and color brace if I now escape writing quits clear the screen say answerable WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and color brace if I now escape writing quits clear the screen say answerable
hyphen playbook
render test at YML hit enter and we can see from the recap we have updated the
files.
Let's go to the yellow box here let's cut the file if I hit enter look at this
right here.
Now we have the original text followed by on my system I have the following
users
answerable and then the groups here and then we have iterated through all of
the groups
then we have nugget user iterated through all the nugget user groups and then n
ox we have iterated
through all of noxes groups now if we go on to the other machine we should see
something very
similar but this time actually we should see the James user in amongst the WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
similar but this time actually we should see the James user in amongst the
configuration.
So let's cut the file as we can see here there we go right there we have answer
able with the
answerable groups nugget user with all of their groups and James with all of
James's groups.
So that is how we can utilize groups within ginger tool like I say if this is
your first exposure
this can be a little bit overwhelming the key here as always is just a little
bit of practice a
little bit of familiarity but now that we've got that under a belt the next
thing I want to talk
to you about is how we can use conditional logic again this time within our WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to you about is how we can use conditional logic again this time within our
ginger tool templates.