Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible and crontabs

Tags:

ansible

I am very newbie with ansible. I have been writing some playbooks and well, its awesome. However I am stuck with a very simple admin task that every admin has to learn how to manage. Scenario is very common, lot of servers: ubuntu, centos, servers of differents customers, etc

My question is how I must organize my ansible structure to manage crontabs of the different servers?

I have wrote playbooks which can be applied to the most of the servers with some admin tasks: apt-get udpate, install ntp servers and synchronize, but I don't find the right way to make a ansible structure of files (main.yml, host_vars, files,templates,roles,etc) to manage every single cron of every server.

Do you have any example or any advice?

Thanks!!!

like image 666
Rubendob Avatar asked Feb 14 '14 19:02

Rubendob


People also ask

What is cron job in Ansible?

Ansible schedule a Cron Job task in Linux cron, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It's a module pretty stable and out for years and it works in a different variety of operating systems. It manages cron. d and crontab entries.

Is Cronjob and crontab same?

Crontab: Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. File location varies by operating systems. Cron job or cron schedule: Cron job or cron schedule is a specific set of execution instructions specifying day, time and command to execute.

How do I see crontab logs?

Stop searching for logs On Ubuntu, Debian and related distributions, you will find cron jobs logs in /var/log/syslog . Your Syslog contains entries from many operating system components and it's helpful to grep to isolate cron-specific messages. You will likely require root/sudo privileges to access your Syslog.


1 Answers

I know Im not an expert but If some ansible newbie has the same problem as me maybe this can help.

As I wrote few days ago, you are learning ansible, you have a lot of servers, you even can install nginx, php-fpm, mysql, etc in a very new ubuntu/centos box in a few minutes. What I am saying? Not one but servers at the same time, the dream. But oh oh now you have to install different cron tasks in lemp server 1 and lemp server 2.

Then, you have to play with ansible variables. When you are newbie in the ansible world the variables can be a little tricky.

I am not saying this is the better approach but, at least, you don't have to maintain individual cron files for every single server.

First, I check the ansible_hostname of lemp server 1

$ ansible lempserver1 -m setup|grep host
"ansible_hostname": "ns227962",

as you see, the hostname returned by ansible is ns227962. Then I have a ansible structure like:

-- main.yml |_ roles |_cron |_tasks |_main.yml

in the main of cron role I have

---
- name: add line to crontab's root - backup
  cron: name="backup openvz vps" hour="5" minute="1" weekday="7"  job="/root/scripts/openvz_backup_vps > /dev/null"
  when: "ansible_hostname == 'ns227962'"

Notice that is the when statement and ansible_hostname will do the trick.

Then I apply the main.yml and that's all

TASK: [cron | add line to crontab's root - backup] ***********
skipping: [lempserver2]
changed: [lempserver1]

It worked! :)

In other situations when you have the same file config but there are parts that are slightly different using templates will be helpful.

Now for me its ok, now I can centralize the manage of the crons servers only with one role.

Thanks

like image 95
Rubendob Avatar answered Nov 15 '22 10:11

Rubendob