Now in this particular video, what I want to do is to turn your attention to
some task scheduling options that we have available to us.
So right now we are using the Ansible Automation system, so we obviously know
the automation is a quite good thing.
And one of the benefits of automation is that while we cut out things such as
human error,
or at least we try to minimize the possibility of human error.
Now even before we were using all of these cool automation tools such as Ans
ible or Terraform or whatever it may be,
there were actually options to automatically schedule tasks directly on your WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
there were actually options to automatically schedule tasks directly on your
Linux system.
So say for example, you wanted to perform a really, really important task,
but let's maybe say it was at an inconvenient time.
So let's maybe say you wanted to create some type of tar archive, a backup of
your files,
but basically you didn't want to do it during the week.
Let's maybe say you wanted to perform this task at 8pm in the evening, you know
,
sometime well after everyone had left the office.
Well, it would be kind of annoying to have to drive back in at 8pm, log into WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Well, it would be kind of annoying to have to drive back in at 8pm, log into
the system and then perform that backup.
Not exactly a good idea.
Similarly, let's maybe say you wanted to perform some type of task during
office hours,
but you really don't want to run the risk of forgetting to do this task.
It's so important that it cannot be missed.
Well, once again, the solution to that type of problem would be to schedule a
task.
So that it executes automatically without you having to deal with it manually.
Basically, it takes you out of the equation, you don't have to be at the system WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
at the time of the process.
And it also doesn't require that you remember about the process just at the
time it's supposed to happen.
So there are different ways to do such a thing, we can do something like a cron
job.
This would be a very good solution if you wanted to continually perform some
type of task.
However, what if you just wanted to perform some task on a one-off basis?
Maybe you want to run a script one time at one particular hour on one
particular date and that is it.
In this case, the solution that we could use would be the "at" commands. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Pretty much, this allows us to schedule a particular task or run a particular
command at a very specific time.
Say for example, let's do an LS, we can see what I have right here.
What I'm going to do is I'm going to schedule the creation of a text file and
let's maybe say one minute time.
If I just say date, we can see the time is five minutes past two in the
afternoon.
And also it is at now plus one minute.
All I'm going to do is echo in, "test file" into a file called "test.txt". WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
All I'm going to do is echo in, "test file" into a file called "test.txt".
And then once I'm done, I can press Ctrl D. Now if I happen to do an LS, notice
the file is not yet created.
But if I just wait that minute, then it will be created.
So let's just do a date to see the time right now, give it a few more seconds.
Let's do a date again, now a minute has elapsed. If I do an LS, check it out
right here.
The file hasn't been created. It was created exactly one minute after we had
scheduled it as per instructions.
So if I cat the file right there, now this is obviously a very, very trivial WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if I cat the file right there, now this is obviously a very, very trivial
example.
Like I say, maybe you want to perform some type of backup, some type of update,
maybe some type of check of a configuration file.
Having the ability to do this via the "at" utility is going to allow us to
schedule this as a one time job.
So whilst I have the ability to perform this type of automation on this
particular system, this is only one machine.
What if I want to perform this task across maybe 500 machines I'm managing?
Well I don't want to log in one by one by one by one and do that.
That's going to take a lot of time. Instead, what if we can use Ansible to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
That's going to take a lot of time. Instead, what if we can use Ansible to
effectively automate the automation?
So that means we can push out that "at" command with the actual time that you
want to run.
And we can push it to all of our devices concurrently using automation.
So if I go to my browser, if I just search for the Ansible at module, we will
see this particular link here.
Let's click it. Now again, notice this is part of the Ansible POSIX collection.
So it's not going to be part of your built-in. Therefore, we're going to have
to ensure that we do have our Ansible POSIX collection installed. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to ensure that we do have our Ansible POSIX collection installed.
We can do this via this particular command, although if you have been following
along from previous skills,
this particular collection should already exist on the device, but nevertheless
, let's just type it in.
As we can see here, I already have it. So if we check this in NOPS, as we can
see, we can use this module to schedule a command or run a script.
And have that run once in the future, like I say once is the key here.
If we scroll on down, we can see some of the available options.
We have the command parameter. This is going to take a string. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
We have the command parameter. This is going to take a string.
And pretty much, this is just going to be the command that you want to run in
the future.
In my case here, when I happen to create that file, I run the echo command and
pipe it into a text file.
If I wanted that particular command to be scheduled via at using Ansible, I
would pass that command directly into the command parameter.
Now this part here is also important. This is the count. Notice it takes an
integer value.
This is the amount of units in the future that you want to have elapsed before
the command runs.
Now the actual unit here depends on what you specify. Are you going to do in WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now the actual unit here depends on what you specify. Are you going to do in
minutes, hours, whatever it may be?
That can actually be controlled via another parameter. So again, if I scroll on
down, here we have the option to specify exactly what units we want.
So do we want to schedule something a few minutes in the future?
Or maybe hours, perhaps some days? Or maybe even weeks in the future, we have
this ability to specify this directly via the unit parameter.
Now one other parameter that I just want to show you is if we just scroll on up
a little bit.
Now notice we have this script file option. This is the option we could use if WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now notice we have this script file option. This is the option we could use if
we actually want to run a parameter.
We actually want to run a particular script in the future, not just a regular
command.
So why don't we go and give this a shot then? So the first thing I'm going to
do is I'm going to create a very simple bash script.
It's going to perform a very simple task. Now like I say, this is not going to
be the most real world example.
It's just to highlight the utility here. And you can use your imagination to
think about all the weird and wonderful things that you could potentially do
with a different style script.
So let's go and create one right here. I'll just say them and I'll just say my WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let's go and create one right here. I'll just say them and I'll just say my
bash.sh and I'm going to insert mode.
And all I will do at the top here is I'm going to add in my shebang and then
forward slash bin forward slash bash.
If you recall from the RHCSA examination, this is how we start off a bash
script.
The bash script is going to be super, super simple. All we're going to do is to
echo in the words.
This is just a little test and I'm going to redirect that into a file called my
example dot txt. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Okay, so we have this little script currently at resides in this particular
directory home and simple directory.
And what I want to do is I want this script to be present on both of my managed
devices.
That is the blue box and the yellow box. So what am I going to do?
Well, I'm going to use the copy module. So let me just begin here by saying
them play one dot ymail.
I'll go into insert mode, have my three hyphens at the top and give the play a
name.
I'll just say play one copy script to targets.
Who do I want to target? I want to target the all group and I'm going to set WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Who do I want to target? I want to target the all group and I'm going to set
gather facts to the value of false to speed things up a little bit.
Now what I'm going to do here is to find my tasks. So the task here is going to
be called copy script to my servers.
Now what am I going to do right here? Well, I'm going to use the Ansible built
in copy module.
Recall we saw this in a previous skill and this is going to allow us to copy a
particular file from the control mode here to the target destinations.
Now I'm going to have to supply some particular parameters namely the source
parameter.
Basically where can I find the file on my control machine that I want to copy WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Basically where can I find the file on my control machine that I want to copy
and all I'll do is specify the path so you can find that here and home Ansible.
And let me just check the name of the file once again. It's called my bash.sh.
And now I specify the destination where abouts I want to copy this on the
target device.
Now just because this is a little demo I'll just copy it into the temp
directory.
So I'll copy it into temp my bash.sh so this is where it's going to be found on
the yellow box and the blue box.
Now one thing to note here when I happen to copy this particular file on my WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now one thing to note here when I happen to copy this particular file on my
control node to the blue box and yellow box on my targets.
I want to make sure that this script is going to be executable because after
all that isn't either bash script and it's going to need those particular
permissions.
So I'm not just going to specify that I'm going to copy this particular file
from here to here.
I want to ensure that it has those permissions.
Now if you check the documentation you will see the way we can do this is by
using the mode parameter.
All I'm going to do here is specify the mode is going to be 0 7 5 5. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
All I'm going to do here is specify the mode is going to be 0 7 5 5.
That's just the optonation to ensure that the owner has redry executable access
and the groups and others also have red and execute.
So with that now done what I'm going to do here is just save this for now.
So all I will do here is say Ansible Playbook play one dot y mail and to enter.
And as we can see here we have now copied over her scripts to those target
devices.
So if I see the end of the temp directory and do an LS and I just grip SH.
As we can see here we do have this script present now on this device. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
As we can see here we do have this script present now on this device.
So I cut this there is the file and similarly if we go to the blue box CDN to
the temp directory to an LS and more grip for SH.
Once again we also have this particular file within this particular directory.
Now if I do an LS dash L and I grip for SH once again notice we have executable
permissions for all groups.
So with that now done we actually want to use the Ansible Playbook to
effectively call those particular scripts and execute them.
So here is what we're going to do. We'll create a new play called play two dot WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So here is what we're going to do. We'll create a new play called play two dot
y mail and let's go into insert mode.
We'll have our free hyphens at the top and we just call this play two testing
the ads module.
We're going to target the all-grut once again and we're also going to set
gather facts to false once again.
Now we'll define our tasks. In the first task we'll be task one.
Test the service is running. So all I'll do is use the Ansible built-in service
module.
And now what I'm going to do is specify the service that I want to test and it
's going to be the ad daemon.
And now we'll specify the states and I want to ensure that it is started and I WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And now we'll specify the states and I want to ensure that it is started and I
want to ensure that it is enabled.
So if we happen to restart the server the ad service is going to automatically
begin.
So when we have verified that ad is indeed running an operational on our system
we want to use that particular program.
So we just say task two. We just say run scripts in the future.
So what we're going to do here is use the Ansible POSIX ads module this time.
And what I want to do is I want to specify the script file that I'm going to
execute.
Now where is that actually located? Well it's going to be located in the temp WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now where is that actually located? Well it's going to be located in the temp
directory and it's called my bash dot SH.
This is the path for the file on both of the target devices.
Now what I want to do here is to specify my counts.
So in my case here let's do with account of two.
And now I specify the unit. Well two what is it two hours two weeks two minutes
?
What is it? Well because as a demo we don't want to wait forever so I'll just
say two minutes time.
Now before I actually run this what I want to do is verify that the script has
not actually ran yet.
So let me go in as the Ansible user here and I'll type in the password Ansible. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So let me go in as the Ansible user here and I'll type in the password Ansible.
If I do a CD I'm now in the Ansible home directory.
If I do an LS we have not actually got any particular files right here.
However once the script has ran as per the scheduling that particular file is
short and the home directory of the Ansible user.
So I will just say Ansible Playbook play two dot yml and to enter.
We'll test the service as it running. The Playbook executes if I go back to my
yellow box.
Inside of this home directory if I do an LS the file has not yet been created
because the script has not executed. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
However in about nine seconds time the script will have been executed and it
will generate the file within this particular directory.
So again now we have executed if I do an LS now the file actually has been
generated.
Check this out and if I go over to the blue box if I just substitute user
become the Ansible user and just type in the password Ansible.
If I see you to the home directory and on LS we can also see the file has also
been generated within the home directory of the Ansible user on the blue box as
well.
And all of this was scheduled via the @utility via Ansible. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And all of this was scheduled via the @utility via Ansible.