Ansible Cron backup location
Is it possible to modify the backup file location when you have backup activated on cron?
- name: cronjob Test
cron:
name: Test
user: root
minute: "*/1"
job: "/SomeFile"
cron_file: /etc/cron.d/Test
backup: yes
Currently the backup is in /tmp/
and I would like it to be /etc/cron.d/Test.{timestamp}
.
Solution 1:
From the documentation:
Param: backup
type: boolean
Description: If set, create a backup of the crontab before it is modified. The location of the backup is returned in thebackup_file
variable by this module.
From there, basically:
- name: cronjob Test
cron:
name: Test
user: root
minute: "*/1"
job: "/SomeFile"
cron_file: /etc/cron.d/Test
backup: yes
register: mkcron
- name: move backup file
copy:
remote_src: yes
src: "{{ mkcron.backup_file }}"
dest: /path/to/my/backup
- name: remove original file
file:
path: "{{ mkcron.backup_file }}"
state: absent
If you really find those two task too much, your can reduce to one by using a simple mv
instruction in a command
task. But I really don't think the very little overhead justifies falling into bad practice here.
Note: you'll probably have to harden this code so that it does not fail trying to move a non existing file on first cron creation with condition and defaults. But I'll let you do that exercise.