So friends, now that we have our variable files defined,
how about we begin writing our first Ginger templates.
So with that said, let's just get to it.
Okay, so the first thing that I'm going to do
is I'm going to go into my main directory right here
and I'm going to create a new directory
simply to hold my template to files.
So I'll just say mkdir and call this templates.
Again, call this anything you so wish.
Just be aware that whatever you name here,
you're going to have to reference
within the playbook itself.
So inside of this directory right now, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So inside of this directory right now,
all I'm going to do is I'm going to create a file
or rather a template file.
And let me just call this test.j2.
Okay, the name itself doesn't actually matter.
I would just recommend sticking with that.j2 extension.
So let's go into this particular file right here.
Now here is the thing, with respect to Ginger,
we can just begin writing text as we ordinarily would.
Say for example, I wanted to do this, okay.
This is an example of a Ginger template.
So this actually is a valid Ginger 2 template file WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So this actually is a valid Ginger 2 template file
as it is right now.
It's just not a very useful one.
We're not doing any type of variable substitution
or using any of the programmatic logic
that we can ordinarily use when working with Ginger.
So let's spice it up a little bit.
Let's add in my name is, and at this point here,
what we're going to do is we're going to use
our variable substitution.
Now similar to what we saw before within our Playbooks,
we can call these variables utilizing
double curly braces right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
double curly braces right here.
Now what exactly is the name of the variable
that I'm going to call?
Well, if we go and reference the vars, for example,
let's look at another tab.
So let me cat the hostfars,
and let's go with blue box.yaml first.
If we want to get the name, what do we do?
We're going to have to call the device roll key.
And then to get the name itself,
we will use dot notation similar to what we saw before,
to call the name key.
So that's exactly what we're going to do, device roll,
then name.
So we'll just say device underscore roll,
and then dot name. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then dot name.
Let's look at what other information we have.
We have a type, so to say my roll in the network
is as a, and then I'll have my variable substitution,
once again, we'll call device roll,
and then we'll grab the type key.
Perfect. And if we happen to go to say, for example,
the group vars and go to the all dot yaml,
we can see the domain info, we have CBT targets land,
and the location is Oregon.
Now just to highlight the spacing here,
if I wanted to have some type of particular formatting,
whereby we needed to have indentation
in our configuration file, whatever it may be, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in our configuration file, whatever it may be,
we can add these configurations,
or rather add these indentations
to our configurations, no problem at all.
We could just do, say, for example,
one, two, three, four spaces,
and just say I live in,
and then call my variable substitution, once again.
Let's look at the actual path.
We're going to have to have domain underscore info,
and then the name.
So just put that within our double color braces,
which is in the state of,
and again, domain underscore name,
and to get the actual location here,
we call the location key.
Now just to point out,
the actual example here that we're doing here WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the actual example here that we're doing here
is not exactly the most useful one, as you can imagine.
This is obviously not an actual Linux configuration.
And in fact, as it transpires with this config here,
and this config here,
because our network is so small,
we only have two devices,
and these are applied to the all group.
Realistically, we could just hard code these values
directly within the template itself.
But despite this, even though we could do this,
it still has kind of a good idea
to have this templatization on the go,
because what it means is if you want to update WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
because what it means is if you want to update
the actual variable files,
say, for example, the location happened to change,
we don't have to go into perhaps a very long
and complex Ginger2 configuration file.
We can just quickly modify these much more readable
and manageable and smaller YAML files.
So just giving you some additional context here.
So let's just do escape colon wq to write and quits.
And what I will now do is I will go and create
a little playbook that is going to effectively
render these configurations.
So let's just say touch,
and let's just call this render test dot yml, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and let's just call this render test dot yml,
and we'll use them to edit this particular file.
We'll do insert mode, we'll have our three hivens
at the top, we'll give our name of the player.
So we have the name key, and we'll just add in
testing our Ginger2 templates.
Now, who are we actually acting on, but the host key?
The notes we're going to act on the all group.
Now, to specify the tasks that I want to create,
we'll give the task a name, just say render,
config, file, and push to target.
Now, at this point here, what we're going to do
is to use that module that we talked about WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is to use that module that we talked about
in a previous video, that was the Ansible built-in
template module.
So I'll just say Ansible dot built-in dot template.
Now, like I say, we can actually shorthand this
just to have the word template,
but I like to be explicit here,
just say any type of ambiguity or confusion.
And I've just realized I have a syntax error
that should not actually be an array, let me fix this.
And as such, let me fix the indentation here,
move this out, and similarly here.
So recall with respect to the template module,
we have two parameters that are absolutely mandatory. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we have two parameters that are absolutely mandatory.
We're going to have to have the source parameter,
which is SRC, as well as the destination parameter,
dest.
Now, the source parameter is pretty much where our file
happens to reside, or rather our configuration template
happens to reside.
Now, recall what happens to be in the templates directory.
Now, the thing about Ansible is that by default,
it's going to automatically seek to look
for the templates directory.
That is the default behavior.
So because of that, all I'm going to do here
is specify that the file should just be called test.jtubic, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
is specify that the file should just be called test.jtubic,
because like I say, Ansible is going to seek to find
the templates folder and search for the contents
of it automatically.
Now, with respect to the destination,
this is really where about on the target device,
in this case here, we have all group,
which comprises of the yellow box and blue box,
where on those devices do we actually want to write
the rendered file?
So let's just maybe say we want to target
the temp directory within each device,
and let's just call the file, oh, I don't know,
let's say config test.txt, okay?
So let's press escape, colon wq2, write an quit. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's press escape, colon wq2, write an quit.
And I'll just say Ansible Playbook,
render test.yml if I hit enter here.
Oh, and as we can see here, we actually have an error
that says Ansible undefined variable.
And it tells us that the domain_name is undefined.
Now the problem is, is because in the actual template,
I reference this as domain_name,
and if we scroll on down here,
look at the actual file, it should be domain_info.
So there is a good learning example,
be sure to reference the correct keys WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
be sure to reference the correct keys
within your variable files.
So let's go quickly back into the templates,
and we're just going to insert modes.
And I'll just change this one here,
notice I actually got it correct,
the first time domain_info,
and for the second time,
I don't know why I wrote domain_name.
You at home were probably shouting at the screen
when I made that mistake, so apologies there.
Let's try this again.
Once again, always good to see when Ansible,
Spitzer and error,
and how we can actually handle that error,
it's quite instructive.
So let's try it again, one more time, hit enter.
And as we can see here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And as we can see here,
we have a change on both devices,
with nothing unreachable, nothing failed,
and nothing skipped.
So what we want to do is to go to both of those devices,
and to actually see what the result happens to be.
So if we go into the 10th directory,
do an LS, and if I let's do an LS,
and great for config,
there is the actual file right here,
let's just cat this.
And look at this right here,
notice by the way,
the actual indentation has been maintained.
We can see here,
this is an example of a Ginger template,
which was the original line I wrote. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
which was the original line I wrote.
And then we have the line,
which has the variable substitution.
My name is yellowDB,
my role in the network is as a database,
and indentations in i,
live in CBT and nuggets land,
which is in the state of Oregon.
Perfect.
Let's go and check the render configuration,
and the other device.
So going to the 10th directory,
do an LS,
and great for config, once again,
we can see that we have this particular file,
if we just cat it,
we can see the same type of thing,
this is an example of a Ginger template.
This time though,
we have the correct variable substitution, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we have the correct variable substitution,
because we now say my name is blue box,
web, my role in the network is as a web server,
once again maintaining indentation,
i, live in CBT nuggets land,
which is in the state of Oregon.
So we can clearly see here that by utilizing
the template module,
we can reference these Ginger two templates,
and populate in our variable values,
so that we are able to create different configurations
for different devices within our network.