[英]Remove cron using Ansible
I am trying to remove cron job from all nodes.我正在尝试从所有节点中删除 cron 作业。 Ansible script runs without any error but it doesn't remove the cron.
Ansible 脚本运行没有任何错误,但它不会删除 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.这是我在目标主机上的 cron。
[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
.我尝试将 name 替换为
None
、 Ansible: None
、 #Ansible: None
和clean
。 None of them worked.他们都没有工作。
How can I remove this cron?如何删除此 cron? Look like idempotency not working for cron module in ansible, because I ran put cron twice and it place cronjob twice.
看起来幂等性不适用于 ansible 中的 cron 模块,因为我运行了两次放置 cron 并且它放置了两次 cronjob。
This works for me (both adding cron entries as removing):这对我有用(都添加 cron 条目作为删除):
- 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.定义
env
您要求删除环境变量。
- name: disable cron
cron:
name: clean
state: absent
user: root
become: True
Look like idempotency not working for cron module in Ansible
看起来幂等性不适用于 Ansible 中的 cron 模块
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.
如果你有问题,你可以随时在 SO 上提出另一个问题,但你需要包含代码。
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我已经通过试运行检查过它: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.如果您有非常复杂的 crontab 条目,您也可以通过 ansible 的 shell 模块将其删除,如下例所示。
---
- 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.说明:- 您只需将第 8 行的
"mybot"
字符串替换为您的 crontab 条目的唯一标识。 that's it.而已。 for "
how to delete multiple crontab entries by ansible
" you can use multiple strings in grep
as shown below对于“
how to delete multiple crontab entries by ansible
”,您可以在grep
中使用多个字符串,如下所示
"crontab -l -u root |grep -v 'strin1\|string2\|string3\|string4' |crontab -u root -"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.