Okay friends, so now that we understand how to utilize Ginger2 to do things
like variable substitution
as well as to create loops and conditional statements, let's now use Ginger2 to
actually
make a configuration change on our devices.
Now the actual configuration that we are going to build is going to be really
quite simple.
All we're going to do is we're going to change the configurations of our DNS
server for each
of the devices in our system.
So here is what I'm going to do, I'm first going to go to my host variables and
let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
let's
go into let's say blue box for example, okay.
So inside of here, let me just add the key DNS servers and this is going to be
an array
object that's going to allow me to specify all of the servers that I want to
use.
So let's just say we want to use, in this case here, 8.8.8.8, as well as 8.8.4.
4.
So let's copy it right and quit.
And for the yellow box, again, very important, we want to use the same key.
So it's going to be DNS servers and then we specify the servers that we want to
use.
So let's just maybe say this one is going to use 8.8.8.8, as well as let's
maybe say WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
maybe say
1.1.1.1, which I don't believe actually as a DNS server, but nevertheless, it's
just
for a demonstration.
So what we want to do is we want to create a template that is going to make
that configuration
change.
So go into our templates directory.
Okay, here we are.
And I'm going to create a new template just called, let's just say DNS dot j2.
Okay.
So what is the base template going to look like?
Well, think about what file are we going to try to modify?
In this case here, I'm going to try to modify the 8.8.8. resolve.conf WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
In this case here, I'm going to try to modify the 8.8.8. resolve.conf
configuration file.
So let's go and look at that for reference.
I will just say cat, 8.8. resolve.conf.
And this is the file right here.
Now think about it.
What would we do if we wanted to have multiple deformed servers?
So the way we'd actually modify this, we could say sudo vim, 8.8. resolve.conf.
Going to insert more than again, this is not necessarily the way we'd want to
do this.
It's just for demonstration.
You can manage this directly via network manager.
And if you want to add in another server, i.e. we have multiple servers, we
would say,
name server once again, and then specify the corresponding server configuration WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
name server once again, and then specify the corresponding server configuration
, which
would be in this case here, maybe a 1.1.1.1.
So basically, imagine this as a template.
Okay, let's copy this here.
And we'll go to our templates right here.
So let's paste this in.
Now, let's just go back to this right here in our press escape colon quits and
exclamation
mark.
We don't want to write those changes.
Let's just tap this to ensure that there's nothing being changed.
We can see we didn't actually modify anything.
Now think about this.
When we happen to push our rendered configuration, let's just leave this part WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
When we happen to push our rendered configuration, let's just leave this part
here at the top.
That is the comment generated by network manager and search local domain.
Let's just leave that.
Okay.
So we'll just hard code that directly into our template.
Now this part here, we see the same thing repeating twice.
If we see a repetition, this is a candidate for a loop.
So we're going to invoke a loop.
And within that loop, we want to hard code the word name server followed by a
variable
value, which will actually denote the IP address configuration of the name
server and we'll
call that directly from our host variable file. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
call that directly from our host variable file.
So basically, we want a loop and within that loop, a variable.
Okay.
So let's actually try to modify this and insert mode and let's use our loop.
Okay.
So color brace percentage same and I'll just say for server, again, call this
anything
you so wish it's just a variable value in and what is the actual key here?
Okay.
So we can see here the key to be of use is DNS underscore servers.
We're going to have to reference that.
Okay.
So DNS underscore servers and then percentage color brace that gives us our for WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So DNS underscore servers and then percentage color brace that gives us our for
loop.
Now let me delete this low entry.
And like I say, the name server, we're going to leave that word hard coded, but
the actual
value here, this is going to vary.
So how do we actually access the variable?
We'll just say double color brace.
And all we need to do here is use the variable server because that's going to
give us each
element within the array, ie each IP address.
Now what are we actually missing here?
We have started a for loop.
Therefore, we're going to have to close it.
So we'll say color brace percent and four percent color brace. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we'll say color brace percent and four percent color brace.
So if I press escape colon wq, now we have the DNS dot J2.
Okay.
So let's go and check this out.
Then let's go and I'll just copy over my playbook and let's just call this or
render
to dot yml.
Okay.
So let's go in here and modify it.
So the source this time is not going to be test dot J2.
That is not the template we want.
The template we want this time is going to be DNS dot J2.
Now that is the file that we're going to render, but where do we actually want
to place
that file? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that file?
Well, in the destination parameter, we want to specify that this should go into
the Etsy
resolve dot conf file.
So if I just escape colon, write and quit, clear the screen and just say Ans
ible hyphen
playbook and also render to and enter and we can see the changes have been
applied.
So let's go to each device.
So if I go to the yellow box and I just say vim, it's a resolve dot conf.
Look at this right here.
The yellow box has the configuration name server, eight dot eight dot eight dot
eight
and name server one dot one dot one dot one just as we saw within the variable WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and name server one dot one dot one dot one just as we saw within the variable
files for
that particular host.
Now if we go to the blue box and say vim, it's a resolve dot conf.
Look at this right here, we see name server eight dot eight dot eight dot eight
and name
server eight dot eight dot four dot four again, exactly mirroring the
configurations that we
have for the host variables for that particular device.