# How to manage cron tasks with ansible

**URL:** <https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [February 17, 2014, 10:29am UTC](https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578 "2014-02-17T10:29:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruben\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@Ruben\_Ortiz](https://forum.ansible.com/u/Ruben_Ortiz)\
**Post date:** [February 17, 2014, 10:29am UTC](https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578/1 "2014-02-17T10:29:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 17, 2014, 3:21pm UTC](https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578/2 "2014-02-17T15:21:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ruben\_Ortiz](https://avatars.discourse-cdn.com/v4/letter/r/a9a28c/32.png) [@Ruben\_Ortiz](https://forum.ansible.com/u/Ruben_Ortiz)\
**Post date:** [February 17, 2014, 4:54pm UTC](https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578/3 "2014-02-17T16:54:29Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [February 17, 2014, 11:21pm UTC](https://forum.ansible.com/t/how-to-manage-cron-tasks-with-ansible/14578/4 "2014-02-17T23:21:36Z")

</div>

For just one host…

**# cat roles/cron/tasks/main.yml**
