# checking to see if cron entry exists

**URL:** <https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168>\
**Category:** Ansible Project\
**Created:** [June 28, 2023, 2:55pm UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168 "2023-06-28T14:55:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [June 28, 2023, 2:55pm UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168/1 "2023-06-28T14:55:36Z")

</div>

I am checking to see if a cron job exists and if it doesn’t, add it. Because we use random times (for both hours and minutes) I cannot search for the entire line. Here is what I have:

- name: UPDATE\_WAZUH  
lineinfile:  
path: /var/spool/cron/crontabs/root  
regexp: “find /var/log/nginx/nginx\_error.log”  
state: present  
register: remove\_nginx\_logs

- name: UPDATE\_WAZUH  
cron:  
name: “empty nginx logs”  
minute: “{{ 59|random }}”  
hour: “{{ 23|random }}”  
job: ‘find /var/log/nginx/nginx\_error.log -name “\*.gz” -type f -mtime +15 -delete’  
when: remove\_wazuh\_logs is defined

One of the errors I get is:

line is required with state=present

When I try adding line to lineinfile like this:

- name: UPDATE\_WAZUH  
lineinfile:  
path: /var/spool/cron/crontabs/root  
regexp: “find /var/log/nginx/nginx\_error.log”  
state: present  
line: ‘find /var/log/nginx/nginx\_error.log -name “\*.gz” -type f -mtime +15 -delete’  
register: remove\_nginx\_logs

It still fails. How can I accomplish this?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [June 28, 2023, 3:01pm UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168/2 "2023-06-28T15:01:23Z")

</div>

lineinfile wants to add the line if it is not “present”. You can collapse these two tasks into a single task using just linieinfile.

Remember that each task is describing an “desired state”. In a single task you can look for and add the line if missing. If it is present the task will make no change. if it is missing the task will add it.

Walter

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 28, 2023, 3:48pm UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168/3 "2023-06-28T15:48:30Z")

</div>

If you want to just see if things 'would change':  
`check_mode: true`

not all modules support it, but both cron and lininefile modules say they do

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [June 29, 2023, 10:35am UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168/4 "2023-06-29T10:35:04Z")

</div>

Both items make sense - thank you.

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [June 29, 2023, 12:59pm UTC](https://forum.ansible.com/t/checking-to-see-if-cron-entry-exists/38168/5 "2023-06-29T12:59:02Z")

</div>

Putting cron jobs in individual lines of a shared cron file has  
always been an iffy sort of task. Errors in ansible scripting can lead  
to putting in duplicate lines, and failing to clear slightly older  
formats of the same line. It's why I encourage using "/etc/cron.daily"  
files and similar hoptions. Those can also help prevent running your  
cron job in the midst of another interfering cron jobs, such as  
database backups and filesystem backups.
