I am trying to remove cron job from all nodes. Ansible script runs without any error but it doesn't remove the cron.
Here is my playbook
---
- hosts: all
user: <user_name>
tasks:
- name: disable cron
cron:
name: clean
env: yes
state: absent
user: root
become: True
Here is my cron on destination host.
[root@host1]# crontab -l
#Ansible: None
*/5 * * * * /root/cleanup.sh
#Ansible: None
*/5 * * * * /root/cleanup.sh
#Ansible: clean
*/5 * * * * /root/cleanup.sh
I tried replacing name with None
, Ansible: None
, #Ansible: None
and clean
. None of them worked.
How can I remove this cron? Look like idempotency not working for cron module in ansible, because I ran put cron twice and it place cronjob twice.
This works for me (both adding cron entries as removing):
- name: Enforce cron entries in variable files
hosts: all
become: true
become_user: wls
tasks:
- name: "enforce crontab entries"
cron:
name: "{{ item.name }}"
minute: "{{ item.minute| default('') }}"
hour: "{{ item.hour| default('') }}"
job: "{{ item.job| default('') }}"
state: "{{ state | default('present') }}"
loop: "{{ crontabentries }}"
when:
- crontabentries is defined
- crontabentries| length > 0
- item.state|default('present') != 'absent'
- name: enforce absence of entries
cron:
name: "{{ item.name }}"
state: absent
loop: "{{ crontabentries }}"
when:
- crontabentries is defined
- crontabentries| length > 0
- item.state|default('present') == 'absent'
with variable file to remove:
crontabentries:
- name: "somecronjob"
state: absent
or variable file to add: (state = present by default so no need to mention)
crontabentries:
- name: "somecronjob"
minute: "0"
hour: "6"
job: "find /var/log/*.log -mtime +30 -type f -delete"
With env
defined you are asking to remove an environment variable.
- name: disable cron
cron:
name: clean
state: absent
user: root
become: True
Look like idempotency not working for cron module in Ansible
It's working ok, so check your code. If you have problems, you can always ask another question on SO, but you need to include the code.
I tried the below works for me :
---
- hosts: test
become: true
become_user: root
any_errors_fatal: false
tasks:
- name: disable cron
cron:
name: "{{ item }}"
state: absent
user: root
become: True
with_items:
- clean
- None
- None
I have checked it by dry run : ansible-playbook cronclean.yml --check
If you have very complicated crontab entries so you can also delete it by shell module of ansible as shown in below example.
---
- name: Deleting contab entry
hosts: ecx
become: true
tasks:
- name: "decroning entry"
shell:
"crontab -l -u root |grep -v mybot |crontab -u root -"
register: cronout
- debug: msg="{{cronout.stdout_lines}}"
Explanation:- You have to just replace "mybot"
string on line 8 with your unique identity of crontab entry. that's it. for " how to delete multiple crontab entries by ansible
" you can use multiple strings in grep
as shown below
"crontab -l -u root |grep -v 'strin1\|string2\|string3\|string4' |crontab -u root -"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.