Now here is the thing, automation is super super powerful.
So much so that when you happen to be making a very large change across many
devices in your network,
you might be a little intimidated to actually go through with the change.
Because wealth automation can really support charge your ability to make
changes and manage your devices,
it can also support charge your ability to completely wreck the network and
cripple all the devices in that network.
This is just a reality of automation. If you happen to construct an erroneous WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is just a reality of automation. If you happen to construct an erroneous
configuration and you suddenly,
via automation, push that change out to 1000 devices as opposed to just one
device sequentially.
Well, quite obviously the consequences are a lot larger.
So wouldn't it be cool if we could actually check the effect that our
particular change was going to make
on our devices without actually touching those devices.
Now the good news is that this is one of the things that we can do with Ansible
.
Now what I should say is that this is not an absolute full proof solution. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now what I should say is that this is not an absolute full proof solution.
There are some caveats, but we will talk about it within this particular video.
So with that said, let's dive in and see how we can check our automation
solutions before pushing the changes out
to our devices for real.
Okay, so let me just create a playbook called play2.yml going to insert mode.
Let's have our three hyphens at the top, give the playbook a name.
And I'll just call this play2 checking changes.
Let's target the hosts of the, let's go to blue box again for simplicity.
And we'll set gather facts to the value false. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's define our tasks.
So let's say I want to install engine X.
So what I will do is I will use the Ansible built in DNF module.
And let's specify that I want to have engine X installed on my target device.
So I'll say the state should be present on device.
Now right now if I go to my blue box device and I say systemctl status on
engine X.
We can actually see here that I don't have engine X on this system.
Now if I happen to run this playbook as it stands right now ordinarily, what WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now if I happen to run this playbook as it stands right now ordinarily, what
would happen is that because Ansible notices that the engine X7 is not present,
then it will begin installing the engine X package to ensure that it is present
on the system.
So what I will do here is as opposed to running it as I ordinarily would write
here,
I can invoke check mode by saying dash dash check on the command line right
here.
Watch what happens now.
Okay, so let's hit enter.
It actually simulates what would happen.
So it tells us a change would be made on the system.
But if I go to my blue box here and I rerun the command that systemctl status WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
But if I go to my blue box here and I rerun the command that systemctl status
on engine X,
it still says that the engine X service cannot be found.
It wasn't actually applied.
But if I run it and remove the check parameter here and to enter,
it looks like we have the exact same output, but because we didn't run it in
check mode,
if I go to my device right here and check the command once again,
now we can see here the system is indeed installed.
So think about it now.
Engine X is installed on a target device.
However, let's say weeks past and I didn't actually know if it was installed or WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
However, let's say weeks past and I didn't actually know if it was installed or
not.
If I just happened to run this particular playbook, which checks that it should
be present,
but it run effectively in simulation mode and check mode that is.
And to enter the check mode this time actually says that no changes would be
made.
So now we can actually logically infer via simulation that the playbook,
if it would not actually change anything in the playbook,
it's designed to ensure that engine X is installed on the system.
Then without the danger of potentially making any changes on the system,
because it is purely a simulation, we can actually infer that the package must WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
because it is purely a simulation, we can actually infer that the package must
actually be
installed on the system as it is right now.
Really, really handy.
Now also what we can do is let's maybe say we wanted to maybe update a
configuration file, okay?
So if I go into my templates directory right here and I'll just create a little
playbook or rather,
rather should I say a ginger to a template and I'll just call this example.j2.
Let's just say this template is going to say hey friends,
and I'll say my name is, and I'll just pass in the variable name, okay? WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So super, super simple template we have right here, okay?
Now let's go back and I'll go into a playbook three right here,
or rather create playbook three should I say.
And again, we'll do our usual three hyphens,
playbook three can be checking config changes.
Again, we'll target this one device, although you can do two,
if you still wish, we'll have gather facts to be false, and now we define our
tasks.
And I'll give this task your name saying push configs using templates.
So I'm going to use the Ansible built-in template module this time, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and now I'm going to specify that the source is going to be example.j2,
and the destination.
I, where do I want the file to be copied on the target device?
I will just have it in the 10th directory, and I'll just call the message
greeting.txt, okay?
So what I'm going to do here is I actually want to define this particular
variable name.
So I'm going to my host files right here, and I'll go into the playbook,
and I'll give the name of let's go with John, okay?
So check this out, if I go back, and I run my little playbook right here, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So check this out, if I go back, and I run my little playbook right here,
and again, I just run it in check modes, it tells me that if I run this,
a change would be made on the system, but right now it's not given me that much
information,
and again, just for proof, if I do an LS, notice the greeting file was not
actually created,
just merely simulated.
So this time, if we happen to make a change here, and this time we run it for
real,
give it a moment, it looks like we have the same thing, a change should be
applied,
but this time, the change actually was applied, okay?
So we do have the greeting right here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So we do have the greeting right here.
Now, this is not the interesting part that I want to show you,
I want to show you an additional flag that we can use here, and personally,
this is one I really love.
So let's maybe say I happen to make the modification of the variable itself,
okay?
So let's go into blue box, and I'll change my name here to be Simone, okay?
Now let's go back, if I happen to run the playbook and check mode again here,
it shows us a change would actually be made because that is a change within the
render
template now, the name has changed within the templates, but the cool thing is, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
template now, the name has changed within the templates, but the cool thing is,
if I want to see what the change actually would be versus what it is right now,
I.e. what the difference would be, if I run this playbook, I can say dash dash,
def with my check command.
So let's hit enter here, and look at this, we get this summary right here,
it says if we happen to run this particular file, or rather this particular
playbook,
the net effect would be we would keep this particular line here,
that would stay the same, hey friends, but this line here, my name is German,
no longer exists, it would be replaced with this line here, my name is Simone, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
no longer exists, it would be replaced with this line here, my name is Simone,
so imagine you happen to have some complex configurations, maybe a whole bunch
of DNS servers
specified within a configuration file that you're rendering, you can
effectively see
what changes you are going to make via the automation system before you apply
them for real.
So again, like I say, if I go to my device and I can't the file, the change
hasn't
actually been applied yet, however, I have simulated the change and I see here
that I'm happy that I want the fail to change, and instead to read my name as WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that I'm happy that I want the fail to change, and instead to read my name as
Simone, so what I will do now is because I know what the change is going to be,
I just run the playbook for real.
So now the change will actually be applied, I go to my device, let's cat this,
look at this right here, now we see the actual change that was simulated
beforehand,
and again, if I run check mode once again with the diff,
because the render template is no longer going to make a change,
look at this right here, it simulates that there would be no changes made,
we're getting to see the item importance of the actual module itself here. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if we change the variable, we can see the change, but also if we go and
modify the
template itself and perhaps maybe add in an additional line, let's just imagine
we were
adding in a DNS server, okay, we'll just say DNS server one, two, three, four,
okay,
whatever, it doesn't actually matter the change.
If I now re-run the simulation once again, it tells us a change would actually
be made,
and it tells us we would have an additional line of DNS server one, two, three,
four.
Now one thing just to be aware of, with respect to your playbooks, you may
actually have WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
some conditionals that do require particular changes to actually be running on
the system
in order for a particular task to execute, you may make an actual change,
and if the answerable system detects that change on the live system,
then another task might execute, perhaps you're running a handler, that type of
thing.
However, because of this, the nature of the simulation, because it won't really
make that
change, it might give you an erroneous output.
So well as this can be very, very effective, understand the limitations, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
be very aware that when you use conditionals, not all the output is going to be
100% accurate,
so still proceed with caution is what I'm saying here.
So just with respect to our check mode, we can actually run check mode on a
particular task,
as opposed to the entire playbook.
So if I go in to my playbook right here, if I just happen to specify that this
particular task
has check_mode set to the value of yes, and let's do one similar here okay, let
's create another task, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
in order to push other conflicts using template, and again I'll do something
very similar.
Let's use another template called example2.j2, the destination is going to be
temp,
and just call us one another greeting.txt.
Notice this one here has to check mode enabled for this particular task, and
this one does not.
All I'm going to do here very briefly is I'll go into my templates again, and I
'll create a file called example2.j2,
go into insert mode, order save bonjour, as another greeting, hello from name. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Okay, so let's save this right here, and if I go back, and all I will do is I
'll go into the
host files again, and all I will do is I'll update the name, this time to be
James, okay, so let's save this.
So if I happen to run the playbook right here, and to enter, we can see here
the two tasks apparently have
operated, which should mean that the greeting.txt here, that file should be
updated to reflect the change of
James. However, when I count the greeting file, notice it still is Samona, it
was ran in check mode,
however, the other task, which was not configured to run as check mode, that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
however, the other task, which was not configured to run as check mode, that
did run for real to be really
should have an example to .j2 rendering, creating the file, another greeting.
txt.
So do an LS, notice we do have this particular file, another greeting, so let's
cut this.
Look at this right here, this particular change was indeed applied to the live
system.