So in the previous video, we learned how we could do task scheduling, utilizing
the @ command via
answerable that was using the @ module. What we now want to do though, is we
want to learn how
we can invoke task scheduling on a repeated basis because recall the @ command
is for a one time
command in the future. If we want to do the same type of jobs repetitively, i.e
. maybe once every
hour, once a week, that type of thing, we want to use the cron utility. So that
really is the big
advantage of cron. We can get that same type of scheduling, but as i say, it's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
advantage of cron. We can get that same type of scheduling, but as i say, it's
not a one time
thing. So let's just do a very brief refresh on cron before we dive into the
module so that we're
very clear on what we're trying to do here. Now one of the main configuration
files for cron is the
@8cron tab file and it is in this particular file that you can specify cron
jobs on a system wide
basis. Now understand that we do have such a thing as a user specific cron job,
but for the purposes
of this particular video, we don't have to rehash all the details around cron. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
of this particular video, we don't have to rehash all the details around cron.
So like a sale,
let's just do a brief recap and then we'll begin looking at the module itself.
So if i go in and
say it's sudo vim, i'm going to @8cron tab right here and to enter. Check this
out right here,
here's the format we're looking at. So we have some very important components.
The first part here
actually denotes the scheduling. You see these five options right here. So the
first column here
denotes the minutes. The next one is the hour. Then we go with the day of the
month, then the month,
and then the day of the week. Now when we happen to see the asterisk here, that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then the day of the week. Now when we happen to see the asterisk here, that
pretty much denotes
the value any. Basically it's a wildcard. So inside of here, we can see we have
asterisk for
every single column. So what does that actually mean? Well it tells us that the
job that we want
to run executed by the particular user, which is in the next column, the sixth
column, and in the
seventh column we specify what it is we actually want to do. What is the job we
want to happen
on this repetitive basis? So this might actually be the root user. And let's
just say they want to
run the command echo hello. Not exactly the most useful command, just to be WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
run the command echo hello. Not exactly the most useful command, just to be
repetitively echoing,
but nevertheless you get the drift. Now how often do we want the root user to
echo this
particular command? Well as per the schedule right here, we have five asterisks
. So what it means is
that that particular command echo hello is going to be ran by the root user
every single minute,
because you have an asterisk on every single hour of every day of the month on
every month
and every day of the week. Pretty much it will be running every single minute WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
without stop. Now of
course we can modify these particular values if we so wish. Say for example if
we want to have a
task be executed at 20 past 12 in the minute column, we would have the value 20
. And then the
error column we would have the value 12. And then again we could just continue
on with our asterisks
that type of thing. Now like I say in the interest of time we're not going to
go through all of the
permutations and possibilities of scheduling with cron. What I will do though
is give you a good
little reference here. So if we go to Google and if you type in cron guru this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
little reference here. So if we go to Google and if you type in cron guru this
is a cron schedule
expression generator. So if I click this link here what you can do here is go
down to examples.
And if I scroll in a little bit here whatever schedule you want to do if you
click on one of the
options it's going to show you how to convert the schedule that you want into a
valid cron job. So
say for example we have one here saying every 15 minutes let's click this here.
This is the
expression we would do we would have the asterisks divided by 15 and then we
would have asterisks
for the remaining columns. So every single 15 minutes the job would run again WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
for the remaining columns. So every single 15 minutes the job would run again
we go back. If you
wanted to do every Friday at midnight in the case here we have the minute zero
and the hour zero
for midnight. We don't care the day so we have an asterisk we don't care the
month so again we
have an asterisk but we do care that it's on a Friday. So in the last column we
can specify the
keyword fri for Friday. Really good page to use when you're practicing your cr
on jobs with that
said. How about we go and actually look at the module itself then. So if I just
go for the
answerable cron module and I click this link here notice unlike the app module WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answerable cron module and I click this link here notice unlike the app module
this one is
actually built in. Okay so as we can see as per the synopsis this module is
used to manage cron
tab and environment variable entries. So let's scroll on down a little bit
further. Look at this
right here we have the day option pretty much just like we saw in the actual cr
on tab itself.
Notice the default value specified here is an asterisk. So if you don't
actually provide a value
it's going to be assumed that it will run every day. Now we can see here valid
values are things WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
values are things
like 1 to 31 so that could be say for example the first day of the month or
maybe the 10th day of
the month that type of thing and we can also do expressions such as everyday
divided by 2 so
every second day that type of thing. Now scrolling on down. Similarly we see
the hour option again
a 60 string the valid numbers can be from 0 to 23 that of course makes sense
because we have
24 hours in a day. So 0 would be at midnight 23 would be 11 p.m. Similarly we
can use our asterisks
and use expressions such as dividing by 2 so every second day or that type of WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and use expressions such as dividing by 2 so every second day or that type of
thing. Once again
the default value is an asterisk. Now here is an important value. We have the
job parameter. This
is what we want to use to specify the command that we want to actually execute.
So the task that
is going to be ran is specified via the job and then we control the frequency
of the job runs
via the other parameters such as minute and hour that type of thing. And again
scrolling on down
we have the minute parameter. Once again the options are 0 to 59 of course that WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we have the minute parameter. Once again the options are 0 to 59 of course that
makes sense.
We have 60 minutes with an hour and again we have the same type of expressions
we can do
as to x divided by 4 as to divided by 2 that type of thing and the default once
again as an
asterisk so that we will run every minute unless specified otherwise and of
course below again
we have the month and it says the valid values run from the first all the way
to the 12th.
Again that is from January the first month all the way to December the 12th and
final month.
With the option again to do all of our expressions so no surprises there.
Now one thing you'll notice here the name parameter actually is a require WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now one thing you'll notice here the name parameter actually is a require
parameter we need this
in order for the module to operate we're going to use this name parameter to
give a description
of the current tab entry itself or if you so wish the environment variable that
has been set.
Now one last one that I want to point out to you that is really quite helpful
is this option
special underscore time this really helps simplify your time specifications so
as opposed to having
to go through that particular option such as maybe say asterisk 1215 whatever
it may be and make WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
it may be and make
some complex expression that can be very handy if you have to be very precise
but in the case of
if you just want to do a simple repetitive task such as something daily or
something hourly we
have these particular keywords to invoke that frequency so we can see we have
available options
such as annually which is once a year daily once a day hourly every hour
monthly for once a month
on reboots so when the system reboots as well as weekly and yearly so if we
want to have a simple
task this can be a really good way to express that job so with that said let's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
task this can be a really good way to express that job so with that said let's
get hands on and
begin looking at a simple example then now just to highlight the contrast of
using cron as opposed
to the ad command what I want to do is to provide actual evidence that a
particular job
happened multiple times so here's an easy way to do this notice when I type the
date command we
actually get some time date information now if I just say echo and then say
dollar sign and then
within parentheses I say dates this is going to echo the same command now what
I can also do is
provide particular formatting here what I can do is I can say dates and then WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
provide particular formatting here what I can do is I can say dates and then
say percentage h dash
percentage m dash percentage capital s now what I get here is I'm actually
getting the date but
I'm parsing out the hour the minutes and the seconds so if I do this again I'm
going to get a different
value you see that so I'll just say pseudo vim it's a cron tab from now going
to insert mode and
I'll just manually say for every single minute I want the answerable user to
run the command and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
run the command and
just as we saw there echo and then I'll have my quotation marks or then I'll
say dollar sign
up parentheses date close parentheses and close my quotations and then I'll
just append that into
a file I'll just call this time test dot txt okay let's just save this if I go
into the temp directory
and do an ls notice we don't actually have that particular file quite just yet
if I check the date
in about 13 seconds that is going to change three more seconds if I do a date
again now we've passed
if I do an ls now we can see we have the time test now what I'm actually going WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
if I do an ls now we can see we have the time test now what I'm actually going
to do here is
cat this file we can see the actual time and I'm going to wait one more minutes
because in one more
minute the same command should run again and append a new value into the same
file thus proving that
cron is not just going to run one time only okay so I do a date again now in
minute has a lapse again
if I do an ls the file is still there but if I cat it again and I enter now we
have a second time
one minute later the command run again and then one minute time again it's WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
one minute later the command run again and then one minute time again it's
going to contain the
run and run and run so let's use cron to backup let's maybe say the contents of
a directory so
let's imagine if we go on to our managed devices if we go into the temp
directory I would just make
a directory called important stuff okay if I cdn to here let's imagine we had
some important files
let's say touch file one and file number two okay now of course again you would
not really have
important stuff within the temp directory but nevertheless let's just play
along so here we have this
particular directory let's do the same thing on our blue box so cdn to temp WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
particular directory let's do the same thing on our blue box so cdn to temp
again let's make a
directory called important stuff we'll cdn to this directory and let's just say
there is file one
dot txt and let's maybe say we'll also have file two let's maybe give this a
JSON extension and
let's maybe say file three has a cfg extension whatever it may be just so we've
got a little
variation so let's imagine that despite being in the temp directory everything
within these four
those the important stuff we want to be backed up so what we'll do here is we
'll go on to our control
nodes and we're going to write a simple bash script to perform that action and WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
nodes and we're going to write a simple bash script to perform that action and
then copy that
bash script from the control node onto the respective managed devices where
they can be scheduled to
run via cron so i'll just create a file called backup dot sh so we'll have our
shebang bin bash
and i'll specify my source directory and it's going to be in the temp directory
and then it's going
to be the important stuff directory within and let's just say the backup
directory is going to be in
the Ansible users home directory and we'll actually have a folder called backup
explicitly okay now WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
explicitly okay now
the actual file name what we're going to do is we're going to use what we just
saw before that
will be the dollar sign and then the date and what i will do here is i'll say
plus percentage
h for the hour and then percentage n for the minute and then percentage s for
the second again
this is not very realistic because what i'm going to do is run this every
minute that is far too
frequent for a real backup but for the purposes of the video we want to do
things very frequently
and then i'll just give the file the name backup dot tar dot gz so pretty much WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and then i'll just give the file the name backup dot tar dot gz so pretty much
we're going to be
creating a new file with the new hour minutes and seconds every time they
happen to be ran so what
i want to do first is i want to ensure that the directory happened to be
created so i'll do an
mkdir dash p which will allow me to create nested directories and all i will do
is i specify that i
want the backup directory to be created now if it already exists then we don't
have to worry and
then what i'm going to do is say tar dash czf and now i'm going to specify my
path so i'll have my
dollar sign for my interpolation i will say backup which will be this WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
dollar sign for my interpolation i will say backup which will be this
particular directory and then
i want to have a forward slash and then specify the actual file that is going
to be inside of that
directory which in this case here is going to be file underscore name again
with that dollar sign
for the interpolation now that is only going to specify the actual backup
directory now we want
to specify what it is that we want to copy over but we want to copy over
everything within the
important stuff directory so we can do that by opening our quotation marks once
again and saying
dollar sign source so if that's now done what we want to be able to do is you WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
dollar sign source so if that's now done what we want to be able to do is you
want to copy this
particular file over onto both machines so the first thing i want to do is to
copy over those files
so if i happen to open up play one that one is allowing me to copy files over
so i'll just copy
that over here copy play one i'll copy this into play number three so let's go
and edit this and
i'm going to change the source here to backup dot SH that is that file and i'll
change the
destination of the file to be backup dot SH once again we're going to give it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
destination of the file to be backup dot SH once again we're going to give it
executable permissions
so if i just save this right here and i run the playbook and to enter now we
should have copied
that particular script over to both devices so if i cd into the temp directory
to an ls we now have
the backup SH script available on the yellow box and sliding over to the blue
box once again we
also have it available also on this box here so now the file happens to be
present on both
devices we want to schedule its execution regularly utilizing cron via answer
able so let's create a
new playbook called play four dot yml going to insert mode have a three hyphens WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
new playbook called play four dot yml going to insert mode have a three hyphens
or just say play
four and i'll just say testing cron to backup file the host is going to be all
gather facts is once
again false and now we define the tasks so let's give this task a name i'll
just do backup important
stuff every minute like i say this is not a realistic example but it will allow
us to execute
quickly and now the module i'm going to use is going to be the answerable bel
ton cron module
and now we specify our information so we have to specify the name here that is
actually required WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
actually required
let's just say backup every minute now i'm going to specify particular values
now just remember
if i wanted to run this every hour for example i could do this in a very
readable way by using
the special time option and in that case here i could just provide the value
regularly okay
however because minutely it's not actually a valid value for that particular
parameter i'm
going to have to do things the traditional way so all i will do here is specify
the value minutes
or rather the parameter minutes and i'll just give the value asterisk which it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
or rather the parameter minutes and i'll just give the value asterisk which it
actually should be the
default value but i'm just being explicit similarly i'm going to specify the
hour and again it will
be explicit with the asterisk now just to be clear here perhaps i didn't want
to run every single
minute maybe i wanted to run it every 30 minutes the way we do that which
simply be to change the
value here to 30 and that is going to be running every 30 minutes on the hour
okay see how that works
so now what i will do is provide the user that's going to run under which is
going to be the
answerable user and now i'm going to specify the job that i want to run but it WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
answerable user and now i'm going to specify the job that i want to run but it
's going to be a
script and where is that script located well it's in the temp directory and it
is called backup.sh
so let's just provide that information so forward slash temp forward slash
backup.sh and that
should schedule the execution of this backup script every single minute and
create those archives so
let's just save this so with that now saved let's run the playbook and to enter
give it a moment
and now we can see we have scheduled the actual task itself so let's go over
here so again if we
go to the home directory so if i could do an LS notice we actually haven't got WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
go to the home directory so if i could do an LS notice we actually haven't got
the backups quite
just yet if i say date we should be scheduled for a run in six seconds so i now
say date again
with no run do an LS look at this right here we have this backup directory now
created if i go in
here doing LS look at this we actually have the time five minutes past five in
one second and then
the backup tar file the same thing will be true over on the blue box so let's
go to the blue box
go to the answerable home directory doing LS we have the backup file doing LS WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
go to the answerable home directory doing LS we have the backup file doing LS
again we have
that same backup now if i just say date here and around about 20 seconds we
should have another run
of backups that should appear within this directory so give it three more
seconds now a new run should
happen to an LS check this out right here now we have a second backup six
minutes past the hour and
again it should be comprised of everything within our important stuff directory
such a trying on zip
one for a moment just for testing so say tar exz vf let's need to go with a
more recent one or six WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
more recent one or six
and to enter check this out right here look at the content we have backups of
all of the information
that we sought to keep