# Remove CRON entry not created with Ansible

**URL:** <https://forum.ansible.com/t/remove-cron-entry-not-created-with-ansible/35992>\
**Category:** Ansible Project\
**Created:** [November 12, 2021, 5:30pm UTC](https://forum.ansible.com/t/remove-cron-entry-not-created-with-ansible/35992 "2021-11-12T17:30:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam1](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/adam1/32/7102_2.png) [@Adam1](https://forum.ansible.com/u/Adam1)\
**Post date:** [November 12, 2021, 5:30pm UTC](https://forum.ansible.com/t/remove-cron-entry-not-created-with-ansible/35992/1 "2021-11-12T17:30:13Z")

</div>

Via Ansible, is there any way to remove a CRON job entry that was not created by Ansible? For example, I have the entry below, but as you can see, it does not have the #ansible above it, so it does not have a “name” parameter to remove.

4 3 \* \* \* /sbin/shutdown -r now \>/dev/null 2\>&1

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 13, 2021, 9:42am UTC](https://forum.ansible.com/t/remove-cron-entry-not-created-with-ansible/35992/2 "2021-11-13T09:42:00Z")

</div>

No. You’d have to resort to some shell workaround, for example:

grep -v /sbin/shutdown | crontab -

---

<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:** [November 13, 2021, 1:45pm UTC](https://forum.ansible.com/t/remove-cron-entry-not-created-with-ansible/35992/3 "2021-11-13T13:45:54Z")

</div>

> \> Via Ansible, is there any way to remove a CRON job entry that was not  
> \> created by Ansible? For example, I have the entry below, but as you can  
> \> see, it does not have the #ansible above it, so it does not have a "name"  
> \> parameter to remove.  
> \>  
> \> 4 3 \* \* \* /sbin/shutdown -r now \>/dev/null 2\>&1

Put the entries into a dictionary and test it in scratch first

&nbsp;&nbsp;&nbsp;&nbsp;my\_cron:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron\_file: /scratch/crontab  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: admin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entries:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute: 4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job: '/sbin/shutdown -r now \>/dev/null 2\>&1'

Given the file

&nbsp;&nbsp;\> cat /scratch/crontab  
&nbsp;&nbsp;4 3 \* \* \* admin /sbin/shutdown -r now \>/dev/null 2\>&1

add the descriptions of the crontab entries, e.g.

&nbsp;&nbsp;&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ my\_cron.cron\_file }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '#Ansible: {{ item.name }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^#Ansible: {{ item.name }}$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertbefore: '{{ item.job }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_cron.entries }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: not item.disabled

The description was added to the crontab

&nbsp;&nbsp;\> cat /scratch/crontab  
&nbsp;&nbsp;#Ansible: shutdown  
&nbsp;&nbsp;4 3 \* \* \* admin /sbin/shutdown -r now \>/dev/null 2\>&1

Now, if you disable the entry

&nbsp;&nbsp;&nbsp;&nbsp;my\_cron:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron\_file: /scratch/crontab  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: admin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entries:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: shutdown  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute: 4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour: 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job: '/sbin/shutdown -r now \>/dev/null 2\>&1'

the \*cron\* module works as expected

&nbsp;&nbsp;&nbsp;&nbsp;- cron:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron\_file: "{{ my\_cron.cron\_file }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user: "{{ my\_cron.user }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: "{{ item.state|d('present') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;disabled: "{{ item.disabled|d(false) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.name }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job: "{{ item.job }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute: "{{ item.minute|d(omit) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour: "{{ item.hour|d(omit) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day: "{{ item.day|d(omit) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month: "{{ item.month|d(omit) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;weekday: "{{ item.weekday|d(omit) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_cron.entries }}"

and disables the entry in the crontab

&nbsp;&nbsp;\> cat /scratch/crontab  
&nbsp;&nbsp;#Ansible: shutdown  
&nbsp;&nbsp;#4 3 \* \* \* admin /sbin/shutdown -r now \>/dev/null 2\>&1

These two tasks are idempotent. You'll have to run such playbook  
twice, at least for the first time to add missing descriptions to  
enabled items. Then you can keep the \*lineinfile\* task in the playbook  
to make sure the descriptions are present.
