How to manage cron tasks with ansible

Hi

I am very newbie with ansible. I have been learning ansible few weeks. I understand basics of ansible. However and maybe this for you is simply trivial, I don’t know how to manage the cron tasks of my servers.

Until now, I have tested ansible to make a automatic setup of LEMP stack in ubuntu servers (I mean install nginx, php-fpm, mysql) and also I have used ansible to setup automatically things like ntp, update system package, etc. Definitely thins there are common in most of servers.

But with crons is different. I just don’t know how to tell ansible the difference between

ubuntu server 1
ubuntu server 2

and setup different crontab (for root user ).

I have difficulty to decide how to approach this scenario.

Creating different templates?

cron.ubuntu_1.j2
cron.ubuntu_2.j2

stored in files folder?

Having individual playboooks? But these has no sense.

I really don’t know how to do it. My real scenario is

a bunch of centos physical servers
a buchf of ubuntu virtual private servers

Anyone have any idea?

Thanks!

Using variables for differences between systems can be desirable, but it is often better to incorporate cron behaviors into the automation of a particular application or system service.

Using the “cron” module or cron.d files with “template” are usually a better way than templating down the system crontab in one fell swoop.

Hi Michael

Firstable, thanks for reply.

I am not totally sure if I understand completely what you are telling me. Let me expose a short scenario, maybe other people like me who are newbie on ansible some day find this.

We have two servers, A and B, both pertain to a webservers group. We want to add some cron task in B server but no in A server.

Where part of ansible will be responsible for the decision to add the cron task in A or B? Example

# cat roles/cron/tasks/main.yml

For just one host…

# cat roles/cron/tasks/main.yml