Add a scheduled task
# ansible-playbook.yml
---
- name: Manage crontab
hosts: your_target_hosts
tasks:
- name: Add crontab entry
cron:
name: "rsync backup"
minute: "0"
hour: "2"
job: "/path/to/your/backup_script.sh"
your_target_hosts
is the list of target hosts you want to manage crontab on. The name
field is the name of the crontab entry, the minute
and hour
fields are the execution time of the scheduled task, job
The field is the script or command to be executed.
# 使用示例
ansible-playbook crontab.yml
Modify and delete crontab entries
# ansible-playbook.yml
---
- name: Manage crontab
hosts: your_target_hosts
tasks:
- name: Modify crontab entry
cron:
name: "My cron job"
minute: "30"
hour: "3"
job: "/path/to/your/updated_script.sh"
state: present
- name: Remove crontab entry
cron:
name: "My cron job"
state: absent
state: present
means modifying the crontab entry, state: absent
means deleting the crontab entry. Save the above content to a YAML file, then run the ansible-playbook
command to execute the playbook