# Setting an intial cron job and leaving it alone on later plays

**URL:** <https://forum.ansible.com/t/setting-an-intial-cron-job-and-leaving-it-alone-on-later-plays/32951>\
**Category:** Ansible Project\
**Created:** [May 17, 2020, 9:37am UTC](https://forum.ansible.com/t/setting-an-intial-cron-job-and-leaving-it-alone-on-later-plays/32951 "2020-05-17T09:37:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oloryn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/oloryn/32/7904_2.png) [@oloryn](https://forum.ansible.com/u/oloryn)\
**Post date:** [May 17, 2020, 9:37am UTC](https://forum.ansible.com/t/setting-an-intial-cron-job-and-leaving-it-alone-on-later-plays/32951/1 "2020-05-17T09:37:10Z")

</div>

I've got a playbook for preparing new Drupal websites. It doesn't  
itself go through the full installation (which is interactive), but  
preps what is needed to complete the installation - installing drupal  
code, setting up Apache2, creating database user and the database  
itself, etc.

One of the things I'm doing is prepping a cron job that can be completed  
and enabled after the manual part of the configuration is done. I'm  
doing this:

- name: Create cron job  
&nbsp;&nbsp;cron:  
&nbsp;&nbsp;&nbsp;&nbsp;name: Drupal cron job  
&nbsp;&nbsp;&nbsp;&nbsp;disabled: true  
&nbsp;&nbsp;&nbsp;&nbsp;minute: "\*/15"  
&nbsp;&nbsp;&nbsp;&nbsp;job: curl --silent --compressed url  
&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ username }}"

The idea is that the 'url' in the job will be manually replaced by the  
required url (obtainable from the Drupal Status Report screen, and  
containing many, many random characters), and enabled.

Of course, if I run this playbook again, the job is going to be changed  
back to 'curl --silent --compressed url', and of course, I don't want  
that, but I do want to be able to re-run this playbook against a site,  
to apply appropriate changes.

The cron module, unlike some other Ansible modules, doesn't seem to have  
a way to say "Don't replace this cron job if it is already in the  
crontab, even if it's changed" (as opposed to, say, template, which has  
a 'force: no' which will keep you from overwriting an already existing  
file). Having the name parameter will keep it from adding multiple  
copies of a cron job, but it doesn't prevent a modified version from  
being overwritten.

Am I missing something? Is there a not-too-hacky way to do what I want  
to do here?

Ben

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 17, 2020, 11:49am UTC](https://forum.ansible.com/t/setting-an-intial-cron-job-and-leaving-it-alone-on-later-plays/32951/2 "2020-05-17T11:49:46Z")

</div>

Get the list of jobs already present

&nbsp;&nbsp;&nbsp;&nbsp;- name: Get list of cron jobs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ username }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: NOT\_EXISTENT  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result

&nbsp;&nbsp;&nbsp;&nbsp;- name: Display list of cron jobs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.jobs

Then run the task if the job isn't present

&nbsp;&nbsp;&nbsp;&nbsp;- name: Create cron job  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: Drupal cron job  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute: "\*/15"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job: curl --silent --compressed url  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ username }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: "'Drupal cron job' not in result.jobs"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![oloryn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/oloryn/32/7904_2.png) [@oloryn](https://forum.ansible.com/u/oloryn)\
**Post date:** [May 17, 2020, 11:00pm UTC](https://forum.ansible.com/t/setting-an-intial-cron-job-and-leaving-it-alone-on-later-plays/32951/3 "2020-05-17T23:00:41Z")

</div>

> > "Don't replace this cron job if it is already in the crontab, even if it's  
> > changed"
> 
> Get the list of jobs already present
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Get list of cron jobs

\<snip\>

> Then run the task if the job isn't present
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Create cron job

\<snip\>

Excellent!

And I note that this isn't even hinted at in the cron module  
documentation. I'll file this under "It may be worthwhile to experiment  
and register the results of various modules to see when they produce  
when their output is registered". Also "Perhaps module documentation  
should document what is produced when you register their output".

Ben
