So at this point we have learned that you could use Ansible Playbook to execute Bash Shell script at
a remote client locations.
Now in this video, we will learn how we could use those same script and schedule them through the current
job and setting that cron job can be done through Ansible playbook.
So in this video we will write a playbook and we'll name it cron job YAML.
And this playbook will include scheduling a job as a root user and that that cron will run every Thursday
at 10 a.m. and the job that it will kick off is slash home slash Afzal see file search.
That's the script.
It will kick off.
And by the way, this is the file that we created earlier.
And that file has to be executed, executed by root.
All right.
So let's look at our YAML file and we'll use a Vim editor.
Define the name of the YAML file.
Then dash, dash, dash name.
Create a cron job, then host.
Where do you want to run that or set that cron job on all hosts that are located in at see ansible and
host file.
All right.
Then we'll give a space to keep it neat and clean.
Define the task.
Then the name of the task scheduled cron.
Now we'll use the cron module to actually perform this ansible function.
Now the name this job is scheduled by Ansible.
This is the name or the description that will be added on top of the actual cron job inside the cron
job and you run cron tab.
Now the minute you want to run it at zero minute hour at 10 a.m., then the day this is going to be
the day of the month, every day of the month, then the month every month, then the week day you want
to run.
This is the day of the week which shows that run every Thursday.
Then you have the user run it as root and the job or script name that you want to run.
Sorry, schedule.
Then of course, once that playbook is created, then we will run this playbook through our ansible
playbook space cron job yaml file and it should create the cron tab entry in our remote client.
All right, so let's first go to our remote client.
This is our Linux one machine.
I am logged in as root and when you do cron tab minus l to list what is scheduled to be executed on
this machine by cron tab.
You run it, you don't get anything.
It means nothing has been scheduled on this machine.
Now let's go to our Ansible control node and write that playbook.
So which two?
Actually we are in?
We are etsi ansible playbook.
Good.
Where am I logged in as root.
Now create use VM editor to create cron job yaml.
That's the name I picked.
You could pick any name.
Define dash, dash, dash.
And the name of the playbook is create a cron job.
All right, hosts.
All right, then.
Tasks.
Then the name of the task schedule.
Cron.
All right.
The module that you wanted to use for this playbook or for this specific task and that is cron are the
name which is will be the description.
This job is scheduled by Ansible Mainnet.
Make sure you put double coat and zero.
Our double coat.
Sorry, colon.
Double coat, 10 a.m. de.
Every day.
Which is the day of the month.
You wanted to have it every day of the month.
Then a month.
Every month.
That's why we're putting in this asterisks, if you're familiar with how you could write a cron job.
I'm sure you know exactly what I am writing it up now.
Weekday.
He wanted to run every Thursday.
So the sun is zero, Monday one, Tuesday to Wednesday three and Thursdays.
For now, the user who do you wanted to run it as?
We wanted to run it as route.
All right.
So that is all done.
Now, what do you want to run the job?
I want you to run.
Slash home.
I have XL slash C file.
That's the script I want to run.
All right, all good.
Set in final save it now let's go ahead and run this ansible playbook and cron job.
And again, let's cross our fingers.
Let Ansible do the magic.
All right.
Play name is good.
Gathering information is good as task is good.
And it completed no error messages.
Excellent.
So it worked.
So let's confirm it by going to our.
For a more client or now when we run cross tab minus L and you will see right here this job is scheduled
by Ansible.
This is the name or the description that I was telling you that we defined right here, and by default
it will add this hash or pound sign with Ansible, which shows that it is created by Ansible, of course.
And look at this entry zero, which is right here, minute ten, which is the hour, then asterisk for
the day, asterisk for the month and the week.
Day is the fourth, which is.
Which is Thursday.
Yes.
So then the entry worked.
Which what do you want to do?
That's the script we wanted to execute.
All right.
So that's how you could schedule a job and schedule it on the cron tab using Ansible playbook.